Microsoft KB Archive/274282

= WD2000: RefreshDocument Method Resets Document's Template to Normal.dot =

Article ID: 274282

Article Last Modified on 10/23/2006

-

APPLIES TO


 * Microsoft Word 2002 Standard Edition
 * Microsoft Word 2000 Standard Edition

-



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 &quot;Steps to Reproduce Behavior&quot; section.



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



Steps to Reproduce Behavior
 Start Microsoft Word. A blank document is created. 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. From the Tools menu, point to Macro, and then click Visual Basic Editor. With the Normal project selected, click Module on the Insert menu to add a new standard module to the Normal template.  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:=&quot;c:\repro.dot&quot; Selection.TypeText &quot;Hello World&quot; 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, &quot;Hello World&quot;, &quot; Hello World &quot;) hProj.RefreshDocument True MsgBox Application.ActiveDocument.AttachedTemplate.Name End Sub </li> Run the AddDoc macro. A new document is created based on Repro.dot. The text &quot;Hello World&quot; appears in the document.</li> Run the EditHTML macro. You receive a message box, which states that the document is attached to the Repro template. This is correct.</li> 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.</li></ol>

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:=&quot;c:\repro.dot&quot;, 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, &quot;Hello World&quot;, &quot; Hello World &quot;) 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.

<div class="references_section">