Solved

Launch word template in command line...

Posted on 2001-08-08
11
435 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
  • 6
  • 5
11 Comments
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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
Comment Utility
Don't forget double-quotes in path and filename.
0
 

Author Comment

by:UTEK
Comment Utility
Could you show me the code how to do it?  This is my first time I have worked with Word template and macro
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:UTEK
Comment Utility
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
Comment Utility
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
Comment Utility
I think I got it working...Thanks for your help and quick responses.
0
 

Author Comment

by:UTEK
Comment Utility
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
Comment Utility
Strange thing:
By the way, why did you accept my comment?
I'm glad but...
0
 

Author Comment

by:UTEK
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now