IceDarkness
New member
- Local time
- Today, 12:00
- Joined
- May 25, 2020
- Messages
- 13
Hi, what I'm trying to do is open a word template from Access, populate some information and then attach it to an email. This is working fine in Office 2019 at home but our work computers are running office 2010 and it seems that after every two/three attempts, it fails with Object Required error. It's this line that's failing:
oWord definitely contains the Word application when it's failing so I don't really understand why it's doing it after 3/4 attempts. The CreateWord function is at the bottom of the code. Before I was just using CreateObject("Word.Application") but wasn't sure if I should be using GetObject so I found a function to combine both.
CreateWord function:
Code:
Set oDoc = oWord.Documents.Add("F:\whatever.dotx")
oWord definitely contains the Word application when it's failing so I don't really understand why it's doing it after 3/4 attempts. The CreateWord function is at the bottom of the code. Before I was just using CreateObject("Word.Application") but wasn't sure if I should be using GetObject so I found a function to combine both.
Code:
Private Sub Command154_Click()
Dim RecordID As Integer, OrgName As String, RecordComment As Variant, ContactName As String, ContactEmail As String, CName As Variant, ContractEndD As Variant
Dim ContactFirst1, ContactFirst, oWordTbl As Object, newrow As Integer, rowNew As Object, i As Integer, wdDoNotSaveChanges, myAttachments, ContractEnd, newfilename As String
OrgName = Me.OrganisationName
ContactName = Me.ContactName
ContactEmail = Me.Email_1
CName = Me.CName1
RecordID = Me.CommID
RecordComment = ContactName & " - " & ContactEmail
ContractEndD = Me.ContractEndDate
If IsNull(ContractEndD) Then ContractEndD = "<span style='background:yellow;mso-highlight:yellow'>[DD/MM/YYYY]</span>"
If Not IsNull(ContactName) Then
ContactFirst1 = Split(ContactName)
ContactFirst = ContactFirst1(0)
End If
Dim oWord As Object, iRecCount As Integer, iFldCount As Integer, j As Integer
Set oWord = CreateWord
oWord.Visible = True
Dim oDoc As Object
Set oDoc = oWord.Documents.Add("F:\whatever.dotx")
Dim cnStr As String
Dim cn As ADODB.Connection
Dim cnRs As New ADODB.Recordset
Set cn = CurrentProject.Connection
cnRs.Open "SELECT CName1, CEmail1,TypeOfContact FROM Comms WHERE ContactID = " & Me.ContactID & ";", cn, adOpenKeyset
With cnRs
If .RecordCount <> 0 Then
.MoveLast 'Ensure proper count
iRecCount = .RecordCount 'Number of records returned by the table/query
.MoveFirst
iFldCount = .Fields.Count 'Number of fields/columns returned by the table/query
End If
Debug.Print iRecCount
Set oWordTbl = oDoc.Tables(1)
For newrow = 1 To iRecCount
Set rowNew = oWordTbl.Rows.Add(BeforeRow:=oWordTbl.Rows(3))
Next newrow
'Build our data rows
For i = 1 To iRecCount
oWordTbl.Cell(i + 2, 2) = Nz(cnRs![CName1], "")
oWordTbl.Cell(i + 2, 3) = Nz(cnRs![CEmail1], "")
oWordTbl.Cell(i + 2, 4) = Nz(cnRs![TypeOfContact], "")
.MoveNext
Next i
End With
With oDoc
.SaveAs "H:\whatever.docx"
.Close SaveChanges:=wdDoNotSaveChanges
End With
If oWord.Documents.Count = 0 Then oWord.Quit
'----------------------------
Dim objOutlook As Object, objEmail As Object, EmailTemplate As String
Set objOutlook = CreateObject("Outlook.application")
EmailTemplate = "F:\whatever.oft"
Set objEmail = objOutlook.CreateItemFromTemplate(EmailTemplate)
With objEmail
Set myAttachments = .Attachments
myAttachments.Remove 1
myAttachments.Add "H:\whatever.docx"
.To = Nz(ContactEmail)
.Display
End With
newfilename = Format(Now(), "yyyy-mm-dd-hh-mm-ss") & " - " & OrgName & ".docx"
Name "H:\whatever.docx" As "H:\whatever\" & newfilename
Set objOutlook = Nothing
Set objEmail = Nothing
Set oWord = Nothing
Set oDoc = Nothing
End Sub
CreateWord function:
Code:
Private Function CreateWord(Optional bVisible As Boolean = True) As Object
Dim oTempWD As Object
On Error Resume Next
Set oTempWD = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Err.Clear
On Error GoTo ERROR_HANDLER
Set oTempWD = CreateObject("Word.Application")
End If
oTempWD.Visible = bVisible
Set CreateWord = oTempWD
On Error GoTo 0
Exit Function
ERROR_HANDLER:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & vbCr & _
" (" & Err.Description & ") in procedure CreateWord."
Err.Clear
End Select
End Function
Last edited: