Search code examples
delphims-wordole-automation

How to remove, or change an attached template via OleAutomation


My company has a large selection of templates which are used to generate customer correspondence. I need to modify the existing processes so that copies of generated files (template + data) are saved for later editing.

My problem is that when I open one of these saved MSWord documents, edit, then close, MSWord is insisting that changes have been made to the template (the one selected in the generation process).

I am not really sure why this is happening, but it may be that the generated document contains a reference to the template upon which it was based, but that because the template is in a remote location, MSWord is attempting to generate a new local file.

If that diagnosis is correct, then I need a method to remove the template reference from the document.

If the diagnosis is incorrect then what is the likely explanation/solution?


I have found that BOTH resultant files contain a reference to the template.

Note: Manual editing in Word has no issue. If I let the letter generate and save to disk from Winword, I can open it and manipulate it quite happily. Somewhere in the automation steps the problem is being created.


Interestingly - I have changed the save format to '.rtf' and the problem remains.

Further - it doesn't matter if I say 'Yes' to saving changes to the template, it continues to prompt me each time I open and close the document (whether I edit or not)


I have discovered that by saving the document as wdFormatXML I can see the reference to the letter template and edit it. If I do that the problem goes away.

I am now attempting to achieve the same result via automation, but with no success;

  WordApp.ActiveDocument.Set_AttachedTemplate(tmplt);

Does not work for values of tmplt 'Normal.dot', varNull, 'c:\progra~1\etc\Simple.dotx' and so on. The function call tells me it cannot find the template for the first 2 of those values, or merely hangs.

I am back to my original question - how does one clear the attached template ?


Solution

  • I eventually figured it. My problem was down to late-binding in some way. I found that the following code worked

    var
      docpath : OleVariant;
      fmt     : OleVariant;
      tmplt   : OleVariant;
      WordApp : WordApplication;
      WordDoc : WordDocument;
    begin
      docpath := SaveLoggedDocToDisk(GetCurrentFileName());
    
      WordApp := CoWordApplication.Create;
      try
        fmt     := EDITABLE_FORMAT;
        tmplt   := '';
    
        WordDoc := WordApp.Documents.Open(docpath, EmptyParam, EmptyParam,  EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, fmt, EmptyParam, EmptyParam );
        WordDoc.Set_AttachedTemplate(tmplt);
    

    The problem I had with earlier versions of this code was that

    WordApp.ActiveDocument.SetAttachedTemplate(tmplt);
    

    although it appears equivalent, was not behaving. By creating a variable of type WordDocument the routine sprang into life.