Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch File to Open a word template file and run a macro in that template  (Read 12899 times)

0 Members and 1 Guest are viewing this topic.

ddjedidd

    Topic Starter


    Greenhorn

    • Experience: Beginner
    • OS: Windows 7
    I am trying to create a batch file to open a Word template file (.dotm) and run a macro file in that template.

    File:  D:Accounts\sales.doc
    Macros &template stored in C:\Users\%username%\AppData\Roaming\Microsoft\Word\STARTUP\salesmacros.dotm

    Batch File script:
    @echo off
     start /B "Word" "D:Accounts\sales.doc"
    /t"C:\Users\%username%\AppData\Roaming\Microsoft\Word\STARTUP\salesmacros.dotm"
    /m"C:\Users\%username%\AppData\Roaming\Microsoft\Word\STARTUP\salesmacros.dotm.Macro_sales"

    The script isn't working.  Any help??

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    What guide did  you use to crease the script?
    Dd you use this?
    https://support.microsoft.com/en-us/help/210565/how-to-use-startup-command-line-switches-to-start-word-2010,-word-2007,-word-2003,-word-2002,-and-word-2000

    Notice different versions of Word are in different directories.

    But why batch? Just to see if you can do it?
    OK,that is a good reason. Just not the best on
     Word is a Windows program, not a DOS program. Windows shortcuts  offer an easy way to start Word with automatic start-up settings.

    Also, you script hard to read. Can you tells us what it is supposed to do?


    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    You cannot pass command line arguments when you start a program by "executing" an associated document via the start command.

    Instead, you'd have to start WINWORD.EXE directly with the arguments and the target file you want to open.

    I was trying to dereference Null Pointers before it was cool.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    ddjedidd,
    Are you still there? Did you find a solution?
    When at the command prompt, the "START" command might not be  the right way to start a program the runs in Windows.
    "START" has its own set of command line options which differ from winword.exe, the Microsoft Word program.

    Example: There is a file 'test.doc' in the current directory. Here is a batch file to start Word and open that file and ignore other stuff.
    Code: [Select]
    REM invokde MS word program with test.doc
    "C:\Program Files\Microsoft Office\Office10\WINWORD.EXE" /a test.doc
     
    Is that almost what you want?
    « Last Edit: April 03, 2017, 12:36:13 PM by Geek-9pm »

    ddjedidd

      Topic Starter


      Greenhorn

      • Experience: Beginner
      • OS: Windows 7
      I am still here.  That is not quite what I am looking for.  The intent is for a Macro in a Global template to run automatically.  I do not want that to happen every time a user opens Word, so I was thinking a Batch script that calls up the Macro would be an idea.

      I have been playing with an Autorun Macro, but I cannot get it to call the Macro in the Global Template Module.

      I haven't given up yet.

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Please give some detail about what kind of business you are running. In general terms. Is this  a place where five people work on two computers and have to generate a report each week?
      Are all computers on the same network?
      Is the job done daily, weekly or monthly?
      What kind of errors would likely occur?
      Could other people run the batch?


      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Batch requests seem to always be nebelous...
      Describe completely what the goal of this Task is and what results you are expecting...
      " Anyone who goes to a psychiatrist should have his head examined. "

      ddjedidd

        Topic Starter


        Greenhorn

        • Experience: Beginner
        • OS: Windows 7
        Ok.  I work with several users who share a network.
        I have developed a Macro embed Global Template (.dotm file) that is stored on each computer. Located in:
        C:\Users\%username%\AppData\Roaming\Microsoft\Word\STARTUP\salesmacros.dotm

        My intent:  The user clicks on a button (or link) on the Shared Network that will:
        1. Open Word
        2. Find their template (on their C:Drive as noted above)
        3. Automatically run the top level macro (called "Macro_sales")

        I thought a clean way to accomplish those three steps in one, would be a Batch File.

        Just as FYI - The "Macro_sales" macro asks the user a series of questions (IF and IFELSE VBA) to populate a specific report template (based on the users answers), creates a network folder, and save the populated form and a .doc file.

        Again, thank you for any help
         

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        OK. That does help. So you have a number of users and there can be some variation in how each workstation is set up.
        Yes, it can be done from dos, but DOS does not have a way to syndicate  a number of commands to a program. All options must be on one line.
        Put another way, it is going to be a one line thing.

        Here is the general information about command line options in Word 2010 and up.
        https://support.microsoft.com/en-us/help/210565/how-to-use-startup-command-line-switches-to-start-word-2010,-word-2007,-word-2003,-word-2002,-and-word-2000
        MS suggests  using a link (shortcut) instead of batch. (But you can batch a link.)
        Quote
        Follow these steps to create a shortcut to start Word from the Windows desktop:
            Right-click the Windows desktop, point to
            New on the shortcut menu that appears, and then click
            Shortcut.
            In the Create Shortcut dialog box, click
            Browse.
            In the Browse dialog box, change the
            Look in box to the following folder:
            C:\Program Files\Microsoft Office\Office
            Note This location of this folder may be different on your system.
        ...
        Read over List of Word startup switches
        I think it says that you can open a file, set a template and auto run a macro on on one command line.

        I will try something after I think about this some more.  :-\
        Meanwhile, maya e a quick mind might jump in.  ;D

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        OK. A simple experiment.
        Now I can do a BAT fle and open a document and do a macro all none command line. I put the document in D:REPORT to reduce the long path name.
        Here is what works as a batch file:
        Code: [Select]
        "C:\Program Files\Microsoft Office\Office10\WINWORD.EXE" D:\rEPORT\report.doc /mmacro1
        The macro starts automatically and inserts some text then stops.
        I have not tired using a template.   :)

        Hackoo



          Hopeful
        • Thanked: 42
        • Experience: Expert
        • OS: Windows 10
        Hi  ;)
        This a starting batch to create a Winword shortcut with arguments on the desktop :
        So, give a try and modify it as you need
        Code: [Select]
        @echo off
        Title Creating a Winword shortcut with arguments on the desktop
        Mode con cols=70 lines=5 & color 9E
        REM Determine if the OS is (32/64 bits) to set the correct path of Program files.
        IF /I "%PROCESSOR_ARCHITECTURE%"=="x86" (
                Set "strProgramFiles=%ProgramFiles%"
            ) else (
                Set "strProgramFiles=%programfiles(x86)%"
        )

        set Key="HKEY_CLASSES_ROOT\Word.Application\CurVer"
        For /f "tokens=4 delims= " %%a in ('reg Query %Key% /ve ^| findstr /R "[0-9]"') do (
        for /f "tokens=3 delims=." %%b in ('echo %%a') do (
        Set "Ver=%%b"
        )
        )

        Rem The shortcut name with the .lnk extension
        Set "MyShortcutName=%userprofile%\desktop\%~n0.lnk"
        set "TargetPath=%strProgramFiles%\Microsoft Office\Office%ver%\Winword.EXE"
        Rem Here we put the arguments of the command line
        Set "Arguments=/q /a"

        If not exist "%MyShortcutName%" (
        Call :CreateShortcut
        ) else (
        Goto Main
        )
        Exit

        ::***********************************************************************
        :CreateShortcut
        echo(
        echo         Creating the shortcut on the desktop is in progress .....
        ::***********************************************************************
        Powershell ^
        $s=(New-Object -COM WScript.Shell).CreateShortcut('%MyShortcutName%'); ^
        $s.TargetPath='"%TargetPath%"'; ^
        $s.Arguments='%Arguments%'; ^
        $s.Save()
        Exit /b
        ::***********************************************************************
        :Main
        echo Hello
        pause