Solved

Launch word template in command line...

Posted on 2001-08-08
11
436 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LINQ return type for nested group query 6 71
MS Access - Capture pressed key onclick 4 28
Determine Range to Select 5 42
SLMGR Switches Are Not Working On KMS Host 3 63
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

919 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

12 Experts available now in Live!

Get 1:1 Help Now