Prateep Nath
Prateep Nath

Reputation: 11

Run MS Word Macros via Java code

Can anyone please let me know what is the correct syntax to execute a MS Word Macro in Java. I tried below but it's throwing error:

Runtime rt = Runtime.getRuntime();
Process process = rt.exec("C:\\Program Files (x86)\\Microsoft Office\\Office16\\WINWORD.EXE  BatchConvertDocToPDF /w /m");

where BatchConvertDocToPDF is the name of the macro.

When I run the code, it tries to look for a .doc file with name BatchConvertDocToPDF.

Thanks in advance!

Upvotes: 1

Views: 1779

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25673

You have the correct commandline switch - /m - but the wrong commandline syntax. The macro name needs to immediately follow the switch, with no space. Putting it before doesn't work.

On the other hand, the name of the document containing the macro needs to be in the position where you've put the macro name. And in order to use this command line you MUST be opening a document. The command line cannot be used to simply run a macro in Word - no functionality of this type exists. So the correct commandline would be:

"C:\\Program Files (x86)\\Microsoft Office\\Office16\\WINWORD.EXE PathAndFileNameWithMacro.docm /w /mBatchConvertDocToPDF"

If Java can automate the Word interop then what you need in order to run a macro in an already opened document is the Word.Application.Run method. That would be something like this (code is C#):

wdApp.Run("'C:\\Test\\DocWithMacroForInteropAppRun.docm'!Test");

If you look at the object model Help topic you'll see that the path to the file name (or just the file name) is not strictly required, but results are more predictable (it's more likely to work) if you use it.

For the sake of completeness, a list of the valid command line switches in Word follows, take from https://support.microsoft.com/en-us/help/210565/how-to-use-startup-command-line-switches-to-start-word-2010--word-2007

List of Word startup switches The following Word startup (command-line) switches are listed in Word Help.

  • /a Starts Word and prevents add-ins and global templates (including the Normal template) from being loaded automatically.

    The /a switch also locks the setting files; that is, the setting files cannot be read or modified if you use this switch.

  • /laddin_path Starts Word and then loads a specific Word add-in.

  • /m Starts a new instance of Word without running any AutoExec macros.

  • /mmacro_name Starts Word and then runs a specific macro. The /m switch also prevents Word from running any AutoExec macros.

  • /n Starts a new instance of Word with no document open. Documents opened in each instance of Word will not appear as choices in the Window menu of other instances.

  • /safe Starts Word in Safe Mode.

  • /ttemplate_name Starts Word with a new document based on a template other than the Normal template.

  • /w Starts a new instance of Word with a blank document. Documents opened in each instance of Word will not appear as choices in the Window menu of the other instances.

  • (no switch) A new Word window is opened with a blank document using the existing instance of the Word program.

The following Word startup (command-line) switches are not listed in Word Help.

  • /c Starts a new instance of Word and then invokes NetMeeting.

  • /q Starts Word without displaying the Word splash screen.

  • /r Starts Word, starts Setup, makes changes in the Windows registry, and then quits. This switch forces a re-register of Word in the Windows registry.

  • /u Has no effect and does not start Word.

  • /x Starts a new instance of Word from the operating shell (for example, to print in Word). This instance of Word responds to only one DDE request and ignores all other DDE requests and multi-instances. If you are starting a new instance of Word in the operating environment (for example, in Windows), it is recommended that you use the /w switch, which starts a fully functioning instance.

  • /z This is similar to the /t switch. However, this switch raises the New event. For example: winword.exe /z mytemplate.dot.

  • pathname\file_name Starts Word with a specific document open.

    Note: To open multiple files at once, use pathname\filename.

    Example: "C:\Program Files\Microsoft Office\Office\Winword.exe" c:\filename1.doc c:\filename2.doc

  • (any other switch) Starts a new instance of Word. For example, if you start Word with just the / and no switch, or with any unlisted switch combination, Word just starts a new instance of Word with a new blank document.

  • /ffile_name Starts Word with a new document based on an existing file.

  • /h http:// file_name Starts Word and opens a read-only copy of a document that is stored on a Microsoft Windows SharePoint Services site. The site must be on a computer that is running Microsoft Windows SharePoint Services 3.0 or Windows SharePoint Services 2.0.

  • /t file_name Starts Word and opens an existing file.

  • /pxslt_name Starts Word and opens an existing XML document based on the specified Extensible Stylesheet Language Transformation (XSLT). Note XSLT: A file that 'used to transform XML documents into other types of documents, such as HTML or XML. It is designed for use as part of XSL.

Upvotes: 2

Related Questions