Thread: changing tempate location in MS Word?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    39

    Question changing tempate location in MS Word?

    Hi all,

    We need to change the template files (.dot files) location in a lot of word documents.

    Does MS Word exposes any interface for doing this programmatically?

    Any other ideas on how to automate this would be appreciated.

    TIA.
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Open Word.

    Tools-->Macros-->Visual Basic Editor.

    View-->Object Browser.

    Enter 'template' into the search box and click the search button.

    Among the entries you will find the Document.AttachedTemplate property.

    So:

    Code:
    Set wdApp = CreateObject("Word.Application")
    Set wdDoc = wdApp.Documents.Open("path_to_my_doc.doc")
    wdDoc.AttachedTemplate = "my new template.dot"
    wdDoc.Save
    wdDoc.Close
    If you really want a way to do this in C/C++ post back.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    39
    Thanks!

    Basically, I was thinking of making a commandline based exe to change the template files location in all the files in a particular folder.

    I am not much of a VB chap, can you provide a similar explanation for C or C++.
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    See DispHelper Thread.

    Code:
    void SetTemplate(LPCTSTR szDoc, LPCTSTR szTemplate) {
    
    	DISPATCH_OBJ(wdApp);
    
    	dhCreateObject(L"Word.Application, NULL, &wdApp);
    	dhCallMethod(wdApp, L".Documents.Open(%T)", szDoc);
    	dhPutValue(wdApp, L".ActiveDocument.AttachedTemplate = %T", szTemplate);
    	dhCallMethod(wdApp, L".ActiveDocument.Save");
    	dhCallMethod(wdApp, L".Quit");
    
    	SAFE_RELEASE(wdApp);
    }
    You should reuse the one instance of word if you are altering several documents.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Monitor a running instance of MS Word
    By BobS0327 in forum C# Programming
    Replies: 0
    Last Post: 07-18-2008, 12:40 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. MS Word and Visual C++
    By Al Pennyworth in forum Windows Programming
    Replies: 0
    Last Post: 04-26-2002, 08:55 AM
  4. Using text in MS Word
    By nate11000 in forum Windows Programming
    Replies: 2
    Last Post: 03-14-2002, 04:25 PM
  5. collaborating with MS Word
    By nate11000 in forum C++ Programming
    Replies: 0
    Last Post: 03-12-2002, 08:11 PM