简体   繁体   中英

Use a batch file to execute a VBA macro on all word docs in a folder

I have several .docm files in a folder. Each file has a VBA macro called SaveFormData which saves the file's legacy field forms into a .txt file and then closes the .docm. I'm trying to create a batch file that will execute the macros within each file, without having to directly call each file. Here's what I've got so far...

@echo off
set myPath=C:\Users\Long File Path with Spaces\

for /f %%f in (%myPath%) do echo ("C:\Some other path\WINWORD.EXE" /mSaveFormData)

I don't really have any experience creating batch files so I'm not sure where I'm going wrong. I've tried following what some of the other posts have done regarding using a batch file to act on multiple files in a folder, but I've had no success other than through calling each file individually like so, but I know that's not very efficient.

@echo off
set myPath=C:\Users\Long File Path with Spaces\

"C:\Some other path\WINWORD.EXE" /mSaveFormData "%myPath%DocName1.docm"
"C:\Some other path\WINWORD.EXE" /mSaveFormData "%myPath%DocName2.docm"

To iterate over all the files in a directory, read help for and then try this for command in the command line.

for %a in (*.docm) do @echo %~fa

to iterate recursively to all the directories and subdirectories

for /r %a in (*.docm) do @echo %~fa

Finally, replace echo with the appropiate command, and arrange a bit the syntax for a bat file

@echo off
pushd "C:\Users\Long File Path with Spaces"
set "winword=C:\Office path\WINWORD.EXE"
for /r %%a in (*.docm) do "%winword%" /mSaveFormData "%%~fa"
popd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM