Run mulitple Word macro's

Dear forum members,

I am very new to Powershell, and I am curious about the vast opportunities it can give me.
However: I have much to learn.

My 1st quest is to

  • join multiple txt - files into 1 larger file
  • migrate the txt - files contents to a Word file
  • Run (from Powershell) several Word - macro’s
  • Migrate the contents of the Word file to Excel.

I have figured out the 1st 2 steps. (thanks Google)
I have figured out how to run a Word macro (there is only 1 word file in the directory)

For 1 macro it works.

I use the code:

$Word = new-object -comobject Word.application
$WordFiles = Get-ChildItem -Path D:\PowershellTest\Dir2Twee -Include *.doc -Recurse   
               <# maybe the recurse can be left out since there are no subdirectories #>
Foreach($file in $WordFiles)
{
   $Doc = $Word.Documents.open($file.fullname)
   $workDocument = $Doc.workDocument   #.item(1)
   $Word.Run("Preben_01_JaarTabs")
   $Doc.save()
   $Doc.close()
}
$Word.quit()

This macro basically is a repeated find and replace action and works

The 2nd macro doesn’t work.
This should perform

  • portait to landscape orientation
  • change font
  • insert tabes etc

Does this macro need a different way, or code to activate it, if it works on settings in Word?
It works perfectly fine if activated from within Word (as do all the other macro’s)

To run all 4 macro’s in 1 go I have added the same syntax as in “$Word.Run(“Preben_01_JaarTabs”)” for the others.
I have put them each on a new line, following the line …Preben_01_jaartabs

Thanks in advance for your help

Kind regards
Hein

Would begin with reviewing the documentation: Application.Run method (Word) | Microsoft Learn

Traditionally, when something is run, the behavior is important. Some commands will run and wait, so the macro would need to complete and then the next line is executed in Powershell. Other commands will just initiate the macro and move to the next line immediately. If it is the latter, then you would need to place a Sleep in the Powershell to give it time to complete. The question is if the macro is\is not running or if you are running 4 macros at once. Might want to create simple macros like macro 1 writing ‘test 1’, and macro 2 writes ‘test 2’ and understand the behavior. Outside of that, not sure if the macros expect you to be in certain context like a certain part of the doc selected. In your code, do not see you setting $word.visible = $true so that can see what is happening with your script as well.

1 Like

Hi Rob,

Thanks you for your reply.

I think (but I may be mistaken) that the application.run method you suggested seems more a Word method than a Powershell command
At least when I try it I get the message “The term ‘application.Run’ is not recognized as the name of a cmdlet, function, script file, or operable program.”
The same happens when I use expression.run

I will follow your suggestion to “start slowly” and start by using simple macro’s, to see what happens, and build from that.

Thanks again
Hein

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.