none
Invoking Word Macro with arguments from command line RRS feed

  • Question

  • Hi,

    I want to invoke a Word Macro from command line. The word macro also takes arguments.

    To invoke without args, I use

    winword.exe /m<MacroName> <fileName>

    But, I don't see any option to pass arguments to the above command.

    We can also invoke Macro using "wordObj.Run <MacroName>". But, i don't see any way to pass args in this method.

    Also, using the "wordObj.Run" hangs my application. So, this is out of the equation.

    I want to make it work with the "winword.exe" way. Is it possible in the first place? 

    If not, what other alternatives I can resort to?

    Thanks,

    Ashok

    Wednesday, May 23, 2012 9:34 PM

Answers

  • You need to enter the args in the new Word file and then call that Word doc from the command line. There does not appear to be any other way of doing it.

    There is a Command$ function that is meant to return what's on the command line following a /cmd flag, but help specifically says it's not available for Office programs, only for VB programs.

    Your only other possibility is to write a VB or VB.Net program that accepts the arguments and then uses Office Automation to start Word, open the document and run the macro for you.


    Rod Gill

    The one and only Project VBA Book

    Rod Gill Project Management

    Thursday, May 24, 2012 9:27 PM

All replies

  • Doesn't look like anyone has a way. SO a workaround is to have a Word file that you run above, but it in turn runs the macro using the Application.Run command which does accept arguments.

    Rod Gill

    The one and only Project VBA Book

    Rod Gill Project Management

    Thursday, May 24, 2012 3:20 AM
  • Hi Gill, Thanks for your reply. I tried Application.Run and it worked. But, I do not want to have one more excel/word in between. I want my WordFile containing the macro to be run from the command line and want it to run independently. If I have a Word Macro, I cannot run it from command line(To run this from Command line, i want to pass args, which would again point to the same question). Thanks, Ashok
    Thursday, May 24, 2012 11:03 AM
  • You need to enter the args in the new Word file and then call that Word doc from the command line. There does not appear to be any other way of doing it.

    There is a Command$ function that is meant to return what's on the command line following a /cmd flag, but help specifically says it's not available for Office programs, only for VB programs.

    Your only other possibility is to write a VB or VB.Net program that accepts the arguments and then uses Office Automation to start Word, open the document and run the macro for you.


    Rod Gill

    The one and only Project VBA Book

    Rod Gill Project Management

    Thursday, May 24, 2012 9:27 PM
  • Do you have the opportunity to write the arguments to a file?  If so the file could be read by code invoked by the macro.
    Tuesday, May 29, 2012 6:24 AM
  • Hi,

    This is a little old but I am looking to do this as well.

    When you say write arguments to a file, do you mean as Text in the file?

    Or where would these arguments be saved in the file?

    Also, what code in the macro would read these arguments?

    I am quite new to this so if there is a better solution to this now, I'm all ears.

    Thanks!

    Wednesday, January 15, 2014 4:50 PM
  • I do this for a special case.  I have a webform that updates a database with info.  The user clicks a button and a PHP program on the webserver creates a text file containing parameters required for a Word addin to create a word doc/pdf.  The PHP program passes back a link to the doc/pdf.  The command line has the text file first then the addin (PHP call).  You need an AutoOpen sub in the addin.  Here is an example AutoOpen sub.

    Public Sub AutoOpen()
      Dim cmdDoc As Document, isJob As Boolean
    
      For Each cmdDoc In Documents
        If InStr(cmdDoc, ".txt") And InStr(cmdDoc, "Job") Then
          isJob = True
          Exit For
        End If
      Next cmdDoc
    
      If isJob Then
        Call ProcessCmd(cmdDoc)
        Call QuitWord
      End If
    End Sub
    

    The ProcessCmd sub gets the parameters from the text file.  It reads it as a text file not as a word doc.  ExecCmd creates the word doc/pdf.  Works quite well.

    Sub ProcessCmd(cmdDoc As Document)
      Dim cmd As String
      Dim val As String
      Dim dn As String
      Dim fn As String
      
      fn = cmdDoc.FullName
      cmdDoc.Close
      
      On Error GoTo Handler
      Open fn For Input As #1
      Input #1, cmd, val, dn, fn
      Close #1
      Call ExecCmd(cmd, val, dn, fn)
      Exit Sub
    Handler:
      Err.Clear
     
    End Sub
    

    PHP call to Word.

            $runWord = '"C:\Program Files\Microsoft Office\Office14\Winword.exe" "' . $job2 . '" "C:\userdata\addins\web.dotm"';
    		$WshShell1 = new COM("WScript.Shell");
    		$oExec1 = $WshShell1->Run($runWord, 7, true);
    

    Wednesday, January 15, 2014 6:17 PM