Solved

Launch word template in command line...

Posted on 2001-08-08
11
451 Views
Last Modified: 2008-02-01
I am trying to create a new document by calling a Word template, which has a macro, in a command line.
The actual command line is something like this:

"C:\program files\office\winword.exe"
/t"C:\temp\mytemplate.dot" /mMyMacro

However, due to a Microsoft Word problem, it doesn't work at all...That sucks! It seems MS decided not to fix it...


In MS web site, it posts a work around at

http://support.microsoft.com/support/kb/articles/Q181/7/83.ASP

The problem is...The sample code inside this article doesn't work either.  Here is the sample code :
 
Sub MyMacro()

      ' Create a new document based on your custom template.
      Documents.Add (ActiveDocument.AttachedTemplate)

      ' Close your custom template.
      Documents(ActiveDocument.AttachedTemplate).Close

      MsgBox "Other code goes here."
End Sub

Whenever I ran it, it posts a "type mismatch" error at the first line.  Here is what I did in creating the template and macro :

1. Create a new template in MS Word
2. Go to Tools->macro->macros and choose the new template
   I just created and create a macro called MyMacro in it.
3. Paste in the above code and save it.


Any Idea why it doesn't work or any other
work around for this MS word bug? Is there anything I did wrong in creating the macro?  Thanks a lot.

0
Comment
Question by:UTEK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6366022
try this:

C:\program files\office\winword.exe "C:\temp\mytemplate.dot"

And call your mMyMacro from thisworkbook_open event of template.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6366023
Don't forget double-quotes in path and filename.
0
 

Author Comment

by:UTEK
ID: 6367920
Could you show me the code how to do it?  This is my first time I have worked with Word template and macro
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6368132
Open your template (I mean OPEN, not new).
Go to Visual basic code editor.
Open project window and locate Thisdocument object, double-click it.
Go to code window and locate document object, locate open event and put code similar to this:


Private Sub Document_Open()
call mMyMacro   ' write arguments if it has....
End Sub
0
 

Author Comment

by:UTEK
ID: 6368533
There is no open event at all
Here is what I see in my original template project

TemplateProject (MyTemplate)
 |__Microsoft Word Objects
 |    |__ThisDocument
 |
 |_____Modules
        |__ AutoNew

Inside the code section of ThisDocument is empty. Inside the AutoNew, there is a module like this which I refer as "MyMacro" in the question

Public Sub MAIN()
   Dim FileRoot$
   Dim file_name$
   Dim i
   Dim Char

   'get the path
   Open "c:\path.loc" For Input As 1
   Input #1, FileRoot$
   Close 1
   
   'need to open the ASCII format of the NAMES file
   file_name$ = FileRoot$ + "WPM\K.FRM"
   Open file_name$ For Input As 2

   .......

End Sub

It is supposed to be working if I make a command like this
"C:\program files\office\winword.exe"
/t"C:\temp\mytemplate.dot"

However, as MS said, it is a bug in Word that new document is created but AutoNew or any macros won't be called at all

If I launch a command like this:
"C:\program files\office\winword.exe"
"C:\temp\mytemplate.dot"
Word will open the template itself instead of creating a new document...





0
 

Author Comment

by:UTEK
ID: 6368600
Actually, what I really need to know is how to create a new
document in a macro.  In MS solution, it suggested not to let the template to create a new document because all the AutoXXX macro won't run. In stead, use the macro to create a new doc.  Here is the code they provided:

' Create a new document based on your custom template.
Documents.Add (ActiveDocument.AttachedTemplate)

' Close your custom template.
Documents(ActiveDocument.AttachedTemplate).Close


I tried it, but it gives type mismatch error!
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 100 total points
ID: 6368883
TemplateProject (MyTemplate)
|__Microsoft Word Objects
|    |__ThisDocument

Do a double-click on thisdocument, when editor window is opened, display combo for objects and locate Document.
Display combo for events and locate Open

write what i posted inside open event.
Save your template as .dot

This could do the trick.
0
 

Author Comment

by:UTEK
ID: 6369188
I think I got it working...Thanks for your help and quick responses.
0
 

Author Comment

by:UTEK
ID: 6369289
Actually, after couple testings, your solution doesn't seem to be working...

When I put a macro call in the Document_Open() where the document is the template, it still suffers the same problem when it executes the command. If we do a command like this :
"C:\program files\office\winword.exe"
/t"C:\temp\mytemplate.dot"

It simply creates a new document but it doesn't execute the macro.

If we do a command like this:
"C:\program files\office\winword.exe"
"C:\temp\mytemplate.dot"
 
It does execute the macro but it doesn't create a new document...so the macro is executed on top of the template and it changes the template itself.

To solve that, I still need to add those 2 lines of code from MS.

Documents.Add (ActiveDocument.AttachedTemplate)
Documents(ActiveDocument.AttachedTemplate).Close

I still can't figure out why it throws a type mismatch error, but it works when I change these two lines into

Documents.Add
Documents("MyTemplate.dot").Close

And then launch it in a commandline likes this:
"C:\program files\office\winword.exe"
"C:\temp\mytemplate.dot" /mMyMacro




 







 


0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6369324
Strange thing:
By the way, why did you accept my comment?
I'm glad but...
0
 

Author Comment

by:UTEK
ID: 6369828
Oh well! well! well! Strange things always happen in this Microsoft dominant desktop world....
I give you the credit for giving me clue for a possible solution which leads to the answer...
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question