Solved

.bat file to open multiple word documents and run macro

Posted on 2013-11-07
9
2,549 Views
Last Modified: 2013-11-17
Hello,

I have created a MS Word macro that converts an XML document open in Word to a .doc format.

I have a set of XML documents that I would like to open and convert to MS Word, they are all named differently but still have the same XML file extension.  

I have this line of code that I put in a .bat file to open a XML file and run the macro, this works fine.

"C:\Program Files (x86)\Microsoft Office\Office14\winword" C:\Users\wlotzkan\Desktop\CC\*.xml /mSaveAsDoc

Open in new window


Now how can I get the .bat file to open and convert every XML file in the folder to a .doc file?  Here is what I have so far....but I cant get it to work.
FORFILES /M *.xml /C "C:\Program Files (x86)\Microsoft Office\Office14\winword" /mSaveAsDoc

Open in new window


Thanks!
0
Comment
Question by:navid86
  • 5
  • 2
  • 2
9 Comments
 
LVL 26

Expert Comment

by:pony10us
ID: 39630686
Try moving the closing quote to the end (after the WinWord macro)

FORFILES /m *.xml /C "C:\Program Files (x86)\Microsoft Office\Office14\winword /mSaveAsDoc"

Open in new window

0
 
LVL 2

Author Comment

by:navid86
ID: 39630924
Okay, I tried that, the CMD window comes up and it looks like it is executing it for every file, but then it doesnt do anything.  Doesnt open any of the documents.
0
 
LVL 2

Author Comment

by:navid86
ID: 39631110
I ran the bat file in the command prompt window and it seems like it cannot find the files in the folder at all.

ERROR: The system cannot find the file specified
0
 
LVL 26

Expert Comment

by:pony10us
ID: 39631179
Are you running this from within the folder the files are located?  If not then you may need to add the path:

FORFILES /p  <path> /m *.xml /C "C:\Program Files (x86)\Microsoft Office\Office14\winword /mSaveAsDoc"

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 2

Author Comment

by:navid86
ID: 39631592
Yes, I am running it from within the same folder, so the path isn't an issue.  I am starting to think that the FORFILES command is probably not the right thing to use in this case.  I removed all XML files from the folder, except for 2 of them and in command prompt when i execute the line of code above it shows like 50 of the same error message.

ERROR: The system cannot find the file specified.

Any other suggestions?
0
 
LVL 38

Accepted Solution

by:
BillDL earned 500 total points
ID: 39634042
Try this:

Directly from the command line after changing directory to the root of the folder containing the *.xml files:

for /r %A in (*.xml) do call "C:\Program Files (x86)\Microsoft Office\Office14\winword.exe" "%A" /mSaveAsDoc

As a batch file run from the root of the folder containing the *.xml files:

@echo off
for /r %%A in (*.xml) do call "C:\Program Files (x86)\Microsoft Office\Office14\winword.exe" "%%A" /mSaveAsDoc
pause
0
 
LVL 2

Assisted Solution

by:navid86
navid86 earned 0 total points
ID: 39641748
Thanks BillDL!  Worked perfectly.  All i did is add a /q to the code to hide the MS Word splash screen.

Thanks a lot for your help!

@echo off
for /r %%A in (*.xml) do call "C:\Program Files (x86)\Microsoft Office\Office14\winword.exe" /q "%%A" /mSaveAsDoc
pause

Open in new window

0
 
LVL 2

Author Closing Comment

by:navid86
ID: 39654302
The final solution for me was adding a /q to to the code.  However BillDL helped fix the majority issues.
0
 
LVL 38

Expert Comment

by:BillDL
ID: 39654610
Thank you Navid.  Glad it worked.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to count occurrences of each item in an array.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now