Consulting

Results 1 to 4 of 4

Thread: Running a Word macro from Excel

  1. #1

    Running a Word macro from Excel

    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.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Did you open the DOT file before Run?

  3. #3
    This is my code.

    [VBA]
    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[/VBA]

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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

    [vba]'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
    [/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •