WD2000: RefreshDocument Method Resets Document's Template to Normal.dot (274282)



The information in this article applies to:

  • Microsoft Word 2002
  • Microsoft Word 2000

This article was previously published under Q274282

SYMPTOMS

In a Microsoft Word macro, you can call the RefreshDocument method of the HTMLProject object to update a Word document. If the document is attached to a template other than the Normal.dot template, the document is then attached to the Normal.dot template instead.

CAUSE

This problem occurs if the original template is not in the Templates folder.

WORKAROUND

To work around this problem, use one of the following methods:
  • Place the template file (.dot) in the Templates folder, which is where Normal.dot is located. To determine the path of the Templates folder, click Options on the Tools menu. On the File Locations tab, click User templates. The path of the Templates folder is listed in the Location list box.
  • Re-attach the original template to the document after you call the RefreshDocument method. To use this workaround, you must have another document open that is based on the template. For an example of this workaround, see the "Steps to Reproduce Behavior" section.

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Start Microsoft Word. A blank document is created.
  2. From the File menu, click Save As. In the Save as type drop-down list box, click Document Template, and save to C:\Repro.dot.

    NOTE: Do not save the template to the Templates folder or you will not reproduce the problem.
  3. From the Tools menu, point to Macro, and then click Visual Basic Editor.
  4. With the Normal project selected, click Module on the Insert menu to add a new standard module to the Normal template.
  5. Copy and paste the following code into the new module:
    Sub AddDoc()
        If Application.Documents.Count > 0 Then
            Application.Documents.Close 'close all documents
        End If
        Application.Documents.Add Template:="c:\repro.dot"
        Selection.TypeText "Hello World"
    End Sub
    
    Sub EditHTML()
        Dim hProj As Office.HTMLProject
        Dim hProjItem As Office.HTMLProjectItem
        Dim s As String
        Set hProj = Application.ActiveDocument.HTMLProject
        Set hProjItem = hProj.HTMLProjectItems(1)
        s = hProjItem.Text
        hProjItem.Text = Replace(s, "Hello World", "<b><u>Hello World</u></b>")
        hProj.RefreshDocument True
        MsgBox Application.ActiveDocument.AttachedTemplate.Name
    End Sub
    					
  6. Run the AddDoc macro. A new document is created based on Repro.dot. The text "Hello World" appears in the document.
  7. Run the EditHTML macro. You receive a message box, which states that the document is attached to the Repro template. This is correct.
  8. Run the EditHTML macro again. A message box states that the document is attached to the Normal template. However, the document should be attached to the Repro template. In addition, notice that the Repro template has been closed.

Workaround

To work around the problem so that the document remains attached to the Repro template, replace the EditHTML macro with the following version:
Sub EditHTML()
    'New section:
    Dim tmpDoc As Word.Document
    Dim strTemplate As String
    Set tmpDoc = Application.Documents.Add(Template:="c:\repro.dot", Visible:=False)
    strTemplate = Application.ActiveDocument.AttachedTemplate.FullName
    
    Dim hProj As Office.HTMLProject
    Dim hProjItem As Office.HTMLProjectItem
    Dim s As String
    Set hProj = Application.ActiveDocument.HTMLProject
    Set hProjItem = hProj.HTMLProjectItems(1)
    s = hProjItem.Text
    hProjItem.Text = Replace(s, "Hello World", "<b><u>Hello World</u></b>")
    hProj.RefreshDocument True
    
    'New section:
    Application.ActiveDocument.AttachedTemplate = strTemplate
    tmpDoc.Close SaveChanges:=False
    Set tmpDoc = Nothing
    
    MsgBox Application.ActiveDocument.AttachedTemplate.Name
End Sub
				
The updated EditHTML macro ensures that another document that is based on the Repro template is open. In addition, the updated EditHTML macro re-attaches the Repro template after the RefreshDocument method. To test this, run the AddDoc macro first, and then run the new EditHTML macro several times.

REFERENCES

For additional information about how to insert HMTL into an Office document, click the article number below to view the article in the Microsoft Knowledge Base:

274326 HOWTO: Add HTML Code to the Clipboard by Using Visual Basic


Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbbug kbDHTML kbpending KB274282