PDA

View Full Version : Running a Word macro from Excel



arsaboo
10-24-2008, 11:15 AM
Hi,

I have created a macro in word to edit a file and saved the macro in the Normal.dot file. I can access the macro from within word.

Now, I have written another macro in Excel that basically opens each word file in a directory. Now I want to run the macro after opening the file. The macro name is FindWordCopySentence.

I have tried using appWD.run macroname:=FindWordCopySentence, but this does not work.

Is there a way I can run the macro or send keystrokes to Word using Sendkeys. The shortcut for the macro is ATL + 4. I tried using appWD.Sendkeys ("%4"), this doesn't work either.

Kindly advice.

Kenneth Hobs
10-24-2008, 12:16 PM
Did you open the DOT file before Run?

arsaboo
10-24-2008, 01:02 PM
This is my code.


Sub runeachfile()
'Edit this variable to start the name search from a different row:
StartRow = 1
'Edit this variable to specify in which column the names are:
StartColumn = 2


'Define an FSO object
Dim FSO As FileSystemObject
Set FSO = New FileSystemObject

'Create a variable to store the folder holding the files
Dim FilesDir As Folder
'Change the string here to look in a different folder (highly recommended :P)
Set FilesDir = FSO.GetFolder("f:\test\")


'Define a counter variable and set it zero
Dim i As Integer
i = 0

'Loop through all of the files in the folder
Dim CurFile As File
Dim appWd As Object
Set appWd = CreateObject("Word.Application")
appWd.Visible = True
appWd.Visible = xlMaximized
ChDir "f:\test\"

appWd.documents.Open Filename:="C:\Documents and Settings\abc123\Application Data\Microsoft\Templates\normal.dotm"
For Each CurFile In FilesDir.Files
appWd.documents.Open Filename:="f:\test\" & CurFile.Name
appWd.Run macroname:=FindWordCopySentence
i = i + 1
Next
End Sub

Kenneth Hobs
10-24-2008, 02:15 PM
You may need the macros to share in a doc file.

Is it in a Module? Try fully qualifying it as in http://support.microsoft.com/kb/177760

'Sub Procedure to Run an Existing Microsoft Word Macro

loadTOCNode(2, 'moreinformation'); 'The following Sub procedure assumes that the document WordDoc.Doc 'contains a macro called "WordMacro."

Sub WDTest()
Dim WD as Object

Set WD = CreateObject("Word.Application")

WD.Documents.Open "C:\My Documents\WordDoc.Doc"

' Note that the project name and module name are required to
' path the macro correctly.
WD.Run "Project.Module1.WordMacro"

End Sub