call word macro from batch file

Microsoft Office word 2007
June 23, 2010 at 10:54:34
Specs: Windows Vista

I am trying to run a word macro that saves an RTF file as a Unicode plain text file via a macro I created in Word. I'm stuck on how to execute the macro. Here is what I have:

For %%x in (*.RTF) Do Start /Wait "C:\Program Files (x86)\Microsoft Office\Office12\WinWord.exe" "%%x" /mmacroSaveAsUnicodeText

The Macro I want to call is named: SaveAsUnicodeText

Any help much appreciated!

the macro itself looks like this:

Sub SaveAsUnicodeText()
' SaveAsUnicodeText Macro

ChangeFileOpenDirectory ActiveDocument.Path
flname = ActiveDocument.Name
ActiveDocument.SaveAs FileName:=flname & ".txt", _
FileFormat:=wdFormatText, LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False, Encoding:=1200, InsertLineBreaks:=False, AllowSubstitutions:=False _
, LineEnding:=wdCRLF

End Sub

Office 2007
Win Vista Ultimate, SP2, x64


See More: call word macro from batch file

Report •

June 23, 2010 at 12:27:45
I don't think you have your quotes right...

For %%x in (*.RTF) Do Start /Wait "C:\Program Files (x86)\Microsoft Office\Office12\WinWord.exe "%%x" /mmacroSaveAsUnicodeText"

Also I thought Macros had to be saved as templates which end with .DOT .

Report •

June 23, 2010 at 13:39:25
Untested, but I would assume:
For %%x in (*.RTF) Do Start "" /Wait winword "%%x" /mSaveAsUnicodeText

NOTE: Your macro doesn't quit Word; your script won't progress unless the user manually closes Word. The fix would be:

Application.Quit wdDoNotSaveChanges

Report •

June 23, 2010 at 13:50:45
Thanks both for the replies!

Razor - that worked perfectly. There is one other issue with my macro: It saves the output file named as "<FileName>.rtf.txt"

Ideally, it would overwrite the original extension (rtf) with the new one (txt). I tried playing with Trim and Left functions to remove the ".rtf" part, but got stuck. Any suggestions?

Report •

Related Solutions

June 23, 2010 at 14:10:45
flname = Split(ActiveDocument.Name, ".")
flname(UBound(flname)) = "txt"
flname = Join(flname, ".")

Report •

June 23, 2010 at 14:48:13
Again, that worked perfectly.

Report •

Ask Question