Solved

Launch word template in command line...

Posted on 2001-08-08
11
455 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 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