Computer Hope

Microsoft => Microsoft DOS => Topic started by: ddjedidd on April 02, 2017, 05:35:53 PM

Title: Batch File to Open a word template file and run a macro in that template
Post by: ddjedidd on April 02, 2017, 05:35:53 PM
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??
Title: Re: Batch File to Open a word template file and run a macro in that template
Post by: Geek-9pm on April 02, 2017, 10:00:19 PM
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?

Title: Re: Batch File to Open a word template file and run a macro in that template
Post by: BC_Programmer on April 02, 2017, 11:09:32 PM
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.

Title: Re: Batch File to Open a word template file and run a macro in that template
Post by: Geek-9pm on April 03, 2017, 12:14:34 PM
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?
Title: Re: Batch File to Open a word template file and run a macro in that template
Post by: ddjedidd on April 04, 2017, 06:51:28 AM
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.
Title: Re: Batch File to Open a word template file and run a macro in that template
Post by: Geek-9pm on April 04, 2017, 12:28:54 PM
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?

Title: Re: Batch File to Open a word template file and run a macro in that template
Post by: patio on April 04, 2017, 12:37:05 PM
Batch requests seem to always be nebelous...
Describe completely what the goal of this Task is and what results you are expecting...
Title: Re: Batch File to Open a word template file and run a macro in that template
Post by: ddjedidd on April 04, 2017, 02:12:53 PM
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
 
Title: Re: Batch File to Open a word template file and run a macro in that template
Post by: Geek-9pm on April 04, 2017, 05:43:19 PM
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
Title: Re: Batch File to Open a word template file and run a macro in that template
Post by: Geek-9pm on April 04, 2017, 11:51:33 PM
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.   :)
Title: Re: Batch File to Open a word template file and run a macro in that template
Post by: Hackoo on April 05, 2017, 09:21:11 AM
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