Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fire a Microsoft Word Macro from an Extra Macro

Status
Not open for further replies.

JeaShe

Programmer
Mar 9, 2004
89
US
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



 
Have you considered calling the macro from the open method of the file?

Looks like the code should work to me also.

Also, are you getting any info from Extra? Why are you running this in EB instead of VBA where it will likely run better.

calculus
 
Question 1: It doesn't work when I use Open.

Question 2: Having trouble with a server calling the macro (within Word) so thought I could take care of it through the Extra macro instead.
 
So the macro in Word is not firing at all with code then.

Sounds like a security issue. You can check this under Tools>Macro>Security. This is the part where a user presses enable/disable macros. It sounds like they are being disabled in code. Try moving this setting to Low and rerun the code.

I've never tried anything on a server, so I don't know if that could be having an effect.

calculus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top