Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Launch word template in command line...

Posted on 2001-08-08
11
Medium Priority
?
466 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 400 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…
Suggested Courses

688 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