I need to run a macro that will facillitate a mail merge in Microsoft Word. I can open the doc I want the merge to occur in, but can't actually fire the macro. Any ideas? I'll include my current code for how I declare the objects. I'm still working in 6.5 I think so please be specific with code advice. Here goes...
' =================================================================================
' Global variable declarations
Global g_HostSettleTime%
Global g_szPassword$
'Option Explicit
Sub Main()
'--------------------------------------------------------------------------------
' Get the main system object
Dim Sessions As Object
Dim System As Object
Set System = CreateObject("EXTRA.System")
If (System is Nothing) Then
Msgbox "Could not open Session A. Stopping macro."
STOP
End If
Set Sessions = System.Sessions
If (Sessions is Nothing) Then
Msgbox "Could not create SessionA. Stopping macro."
STOP
End If
'--------------------------------------------------------------------------------
' Set the default wait timeout value
g_HostSettleTime = 500 ' milliseconds
OldSystemTimeout& = System.TimeoutValue
If (g_HostSettleTime > OldSystemTimeout) Then
System.TimeoutValue = g_HostSettleTime
End If
' Get the necessary Session Object
Dim Sess0 As Object
Set Sess0 = System.ActiveSession
If (Sess0 is Nothing) Then
Msgbox "Could not create the Session object. Stopping macro playback."
STOP
End If
If Not Sess0.Visible Then Sess0.Visible = TRUE
'-----------------------------------------------------------------------------
'Declare Object variables
Dim objApp as Object, objDoc as Object, objRange as Object, WordPath as String
'Tell the Macro to keep running if an error is encountered
On Error Resume Next
'Try to grab a reference to an open instance of Word
Set objApp = GetObject(, "Word.Application")
If objApp Is Nothing Then
Set objApp = CreateObject("Word.Application")
If objApp Is Nothing Then
MsgBox ("Could not open Microsoft Word program.")
Stop
End If
End If
'Place the path to your document
xlPath = "C:\Program Files\E!PC\Sessions\TaxClear.xls"
WordPath= "C:\Progra~1\E!PC\Sessions\FTB2574Letter.doc"
Set objDoc = objApp.Documents.Add (WordPath)
If (objDoc is Nothing) Then
MsgBox "The Word template did not open."
Stop
End If
'Make Word visible to the user
objApp.Visible = True
Everything works until this line:
Set objDoc = objApp.Documents.Run(PrintLetter)
I've tried variations such as:
objApp.Documents.Run(PrintLetter)
but get same results.
objDoc.MailMerge.MainDocumentType = wdNotAMergeDocument
objDoc.Close (wdDoNotSaveChanges)
objDoc.Close (wdDoNotSaveChanges)
objDoc.Application.Quit
End Sub
' =================================================================================
' Global variable declarations
Global g_HostSettleTime%
Global g_szPassword$
'Option Explicit
Sub Main()
'--------------------------------------------------------------------------------
' Get the main system object
Dim Sessions As Object
Dim System As Object
Set System = CreateObject("EXTRA.System")
If (System is Nothing) Then
Msgbox "Could not open Session A. Stopping macro."
STOP
End If
Set Sessions = System.Sessions
If (Sessions is Nothing) Then
Msgbox "Could not create SessionA. Stopping macro."
STOP
End If
'--------------------------------------------------------------------------------
' Set the default wait timeout value
g_HostSettleTime = 500 ' milliseconds
OldSystemTimeout& = System.TimeoutValue
If (g_HostSettleTime > OldSystemTimeout) Then
System.TimeoutValue = g_HostSettleTime
End If
' Get the necessary Session Object
Dim Sess0 As Object
Set Sess0 = System.ActiveSession
If (Sess0 is Nothing) Then
Msgbox "Could not create the Session object. Stopping macro playback."
STOP
End If
If Not Sess0.Visible Then Sess0.Visible = TRUE
'-----------------------------------------------------------------------------
'Declare Object variables
Dim objApp as Object, objDoc as Object, objRange as Object, WordPath as String
'Tell the Macro to keep running if an error is encountered
On Error Resume Next
'Try to grab a reference to an open instance of Word
Set objApp = GetObject(, "Word.Application")
If objApp Is Nothing Then
Set objApp = CreateObject("Word.Application")
If objApp Is Nothing Then
MsgBox ("Could not open Microsoft Word program.")
Stop
End If
End If
'Place the path to your document
xlPath = "C:\Program Files\E!PC\Sessions\TaxClear.xls"
WordPath= "C:\Progra~1\E!PC\Sessions\FTB2574Letter.doc"
Set objDoc = objApp.Documents.Add (WordPath)
If (objDoc is Nothing) Then
MsgBox "The Word template did not open."
Stop
End If
'Make Word visible to the user
objApp.Visible = True
Everything works until this line:
Set objDoc = objApp.Documents.Run(PrintLetter)
I've tried variations such as:
objApp.Documents.Run(PrintLetter)
but get same results.
objDoc.MailMerge.MainDocumentType = wdNotAMergeDocument
objDoc.Close (wdDoNotSaveChanges)
objDoc.Close (wdDoNotSaveChanges)
objDoc.Application.Quit
End Sub