H
Howard Kaikow
The problem does not exist with Word 97, but exists for Word 2000, Word 2002
and Word 2003.
Without using Automation, I can delete a reference, then recreate the
reference in a Word template, and the resulting reference does get saved
with the changed template.
However, the reference does not get saved if I try to do this via automation
via a Word object in any of the following:
1. Word itself.
2. VB .NET 2003, VB 6 or Excel.
3. VSTO, using the built-n This Application object in VB .NET.
Note that the reference does get added, one can see this by stepping thru
the code, and examining the physical file.
However, the reference does not get saved with the template when the code
ends, implying that Word is somehow working with a temporary copy of the
template and not correctly updating the saved template.
Note that I tried an analogous problem using Excel and no such problem
exists when adding a reference to an Excel Workbook
Below you will find code that can be used to replicate the problem.
You can change the values for strDLLPath, strReference and strTargetPath.
SetReferenceInWordNotUsingWordObject will produce the expected result, i.e.,
the reference will be saved.
SetReferenceInWordUsingWordObject will NOT produce the expected result,
i.e., the reference will not be saved.
You will get the same result in Word 2000, Word 2002, Word 2003; or by using
the appropriate code to automate Word via, say, VB .NET 2003, VB 6 (SP 6),
VSTO and Excel 2003 (I've not tried this with other Excel versions).
Option Explicit
' strReference must be set to the reference name.
Private Const strReference As String = "EudoraLib"
' strDLLPath has to be set to path of library.
Private Const strDLLPath As String = "J:\Program
Files\Qualcomm\Eudora\Eudora.exe"
' strTargetPath has to be set to path where target template will be
created
Private Const strTargetPath As String = "C:\Test\VB-Test" ' For VBA
Private Const strTemplate As String = strTargetPath &
"\TargetForRef.dot"
Dim docTemplate As Word.Document
Dim ref As VBIDE.Reference
Dim strOutput As String
Private Sub SetReferenceInWordNotUsingWordObject()
On Error Resume Next
Kill strTemplate
Set docTemplate = Documents.Add(NewTemplate:=True,
Template:=NormalTemplate.FullName)
With docTemplate
.SaveAs FileName:=strTemplate, addtorecentfiles:=False
strOutput = "Using: " & .FullName & vbCrLf
With .AttachedTemplate
strOutput = strOutput & vbCrLf & "Template: " & .FullName &
vbCrLf
With .VBProject
On Error Resume Next
' Remove reference
.References.Remove (.References(strReference))
Err.Clear
' Add reference
Set ref = .References.AddFromFile(strDLLPath)
End With
.Save ' Save template
For Each ref In .VBProject.References
strOutput = strOutput & vbCrLf & ref.Name
Next ref
strOutput = strOutput & vbCrLf & vbCrLf & _
"If " & Chr(34) & strReference & Chr(34) & _
" appears in the above list, then the reference should have
been saved."
strOutput = strOutput & vbCrLf & "Finished"
End With
MsgBox (strOutput)
.Save
'Closing does not destroy Reference, which is expected behavior
.Close
End With
Set docTemplate = Nothing
Set ref = Nothing
End Sub
Private Sub SetReferenceInWordUsingWordObject()
Dim appWord As Word.Application
On Error Resume Next
Set appWord = New Word.Application
If Err.Number <> 0 Then
Exit Sub
End If
Kill strTemplate
With appWord
Set docTemplate = .Documents.Add(NewTemplate:=True,
Template:=.NormalTemplate.FullName)
End With
With docTemplate
.SaveAs FileName:=strTemplate, addtorecentfiles:=False
strOutput = "Using: " & .FullName & vbCrLf
With .AttachedTemplate
strOutput = strOutput & vbCrLf & "Template: " & .FullName &
vbCrLf
With .VBProject
On Error Resume Next
' Remove reference
.References.Remove (.References(strReference))
Err.Clear
' Add reference
Set ref = .References.AddFromFile(strDLLPath)
End With
.Save ' Save template
For Each ref In .VBProject.References
strOutput = strOutput & vbCrLf & ref.Name
Next ref
strOutput = strOutput & vbCrLf & vbCrLf & _
"If " & Chr(34) & strReference & Chr(34) & _
" appears in the above list, then the reference should have
been saved."
strOutput = strOutput & vbCrLf & "Finished"
End With
MsgBox (strOutput)
.Save
'Closing causes Reference to no longer be in template
.Close
End With
appWord.Quit
Set docTemplate = Nothing
Set ref = Nothing
Set appWord = Nothing
End Sub
and Word 2003.
Without using Automation, I can delete a reference, then recreate the
reference in a Word template, and the resulting reference does get saved
with the changed template.
However, the reference does not get saved if I try to do this via automation
via a Word object in any of the following:
1. Word itself.
2. VB .NET 2003, VB 6 or Excel.
3. VSTO, using the built-n This Application object in VB .NET.
Note that the reference does get added, one can see this by stepping thru
the code, and examining the physical file.
However, the reference does not get saved with the template when the code
ends, implying that Word is somehow working with a temporary copy of the
template and not correctly updating the saved template.
Note that I tried an analogous problem using Excel and no such problem
exists when adding a reference to an Excel Workbook
Below you will find code that can be used to replicate the problem.
You can change the values for strDLLPath, strReference and strTargetPath.
SetReferenceInWordNotUsingWordObject will produce the expected result, i.e.,
the reference will be saved.
SetReferenceInWordUsingWordObject will NOT produce the expected result,
i.e., the reference will not be saved.
You will get the same result in Word 2000, Word 2002, Word 2003; or by using
the appropriate code to automate Word via, say, VB .NET 2003, VB 6 (SP 6),
VSTO and Excel 2003 (I've not tried this with other Excel versions).
Option Explicit
' strReference must be set to the reference name.
Private Const strReference As String = "EudoraLib"
' strDLLPath has to be set to path of library.
Private Const strDLLPath As String = "J:\Program
Files\Qualcomm\Eudora\Eudora.exe"
' strTargetPath has to be set to path where target template will be
created
Private Const strTargetPath As String = "C:\Test\VB-Test" ' For VBA
Private Const strTemplate As String = strTargetPath &
"\TargetForRef.dot"
Dim docTemplate As Word.Document
Dim ref As VBIDE.Reference
Dim strOutput As String
Private Sub SetReferenceInWordNotUsingWordObject()
On Error Resume Next
Kill strTemplate
Set docTemplate = Documents.Add(NewTemplate:=True,
Template:=NormalTemplate.FullName)
With docTemplate
.SaveAs FileName:=strTemplate, addtorecentfiles:=False
strOutput = "Using: " & .FullName & vbCrLf
With .AttachedTemplate
strOutput = strOutput & vbCrLf & "Template: " & .FullName &
vbCrLf
With .VBProject
On Error Resume Next
' Remove reference
.References.Remove (.References(strReference))
Err.Clear
' Add reference
Set ref = .References.AddFromFile(strDLLPath)
End With
.Save ' Save template
For Each ref In .VBProject.References
strOutput = strOutput & vbCrLf & ref.Name
Next ref
strOutput = strOutput & vbCrLf & vbCrLf & _
"If " & Chr(34) & strReference & Chr(34) & _
" appears in the above list, then the reference should have
been saved."
strOutput = strOutput & vbCrLf & "Finished"
End With
MsgBox (strOutput)
.Save
'Closing does not destroy Reference, which is expected behavior
.Close
End With
Set docTemplate = Nothing
Set ref = Nothing
End Sub
Private Sub SetReferenceInWordUsingWordObject()
Dim appWord As Word.Application
On Error Resume Next
Set appWord = New Word.Application
If Err.Number <> 0 Then
Exit Sub
End If
Kill strTemplate
With appWord
Set docTemplate = .Documents.Add(NewTemplate:=True,
Template:=.NormalTemplate.FullName)
End With
With docTemplate
.SaveAs FileName:=strTemplate, addtorecentfiles:=False
strOutput = "Using: " & .FullName & vbCrLf
With .AttachedTemplate
strOutput = strOutput & vbCrLf & "Template: " & .FullName &
vbCrLf
With .VBProject
On Error Resume Next
' Remove reference
.References.Remove (.References(strReference))
Err.Clear
' Add reference
Set ref = .References.AddFromFile(strDLLPath)
End With
.Save ' Save template
For Each ref In .VBProject.References
strOutput = strOutput & vbCrLf & ref.Name
Next ref
strOutput = strOutput & vbCrLf & vbCrLf & _
"If " & Chr(34) & strReference & Chr(34) & _
" appears in the above list, then the reference should have
been saved."
strOutput = strOutput & vbCrLf & "Finished"
End With
MsgBox (strOutput)
.Save
'Closing causes Reference to no longer be in template
.Close
End With
appWord.Quit
Set docTemplate = Nothing
Set ref = Nothing
Set appWord = Nothing
End Sub