H
Howard Kaikow
Can one SAVE a reference added to a project when automating Word using a
Word object?
I want to do this from within VB 6, but I find that I cannot do this even
from within Word itself?
For example, using the code below (obvious pieces of code are omitted), one
gets the desired result, i.e., all of the following are executed.
Debug.Print "Found(1): " & ref.Name
Debug.Print "Found(2): " & ref.Name
Debug.Print "Found(3): " & ref.Name
However, if I instead use the Word object, say:
dim appWord as Word.Application
set appWord = New Word.application
and use, e.g.,
Set docNew = appWord.Documents.Add(NewTemplate:=False,
DocumentType:=wdNewBlankDocument, _
Template:=strTemplate)
instead of
Set docNew = Documents.Add(NewTemplate:=False,
DocumentType:=wdNewBlankDocument, _
Template:=strTemplate)
The 3rd test fails and the following is executed:
Debug.Print "Not Found(3): " & strReference, Err.Number,
Err.Description
Is this behavior documented?
Is there a way to SAVE a project after adding a Reference when automating
Word?
-----------------------------------------------
Dim docWord As Word.Document
Dim docNew As Word.Document
Dim ref As VBIDE.Reference
Set docNew = Documents.Add(NewTemplate:=False,
DocumentType:=wdNewBlankDocument, _
Template:=strTemplate)
Set docWord = docNew.AttachedTemplate.OpenAsDocument
With docWord
' add a reference here, then do the following
For Each ref In .VBProject.References
Debug.Print ref.Name
Next ref
If .VBProject.Saved Then
Debug.Print "Project was not modified"
Else
Debug.Print "Project was modified"
End If
On Error Resume Next
Set ref = .VBProject.References(strReference)
If Err.Number = 0 Then
Debug.Print "Found(1): " & ref.Name
Else
Debug.Print "Not Found(1): " & strReference, Err.Number,
Err.Description
End If
.Save
.Close savechanges:=wdSaveChanges
End With
With docNew
For Each ref In .AttachedTemplate.VBProject.References
Debug.Print ref.Name
Next ref
On Error Resume Next
Set ref = .AttachedTemplate.VBProject.References(strReference)
If Err.Number = 0 Then
Debug.Print "Found(2): " & ref.Name
Else
Debug.Print "Not Found(2): " & strReference, Err.Number,
Err.Description
End If
.Close
End With
Set docWord = Documents.Add(NewTemplate:=False,
DocumentType:=wdNewBlankDocument, _
Template:=strTemplate)
With docWord
For Each ref In .AttachedTemplate.VBProject.References
Debug.Print ref.Name
Next ref
On Error Resume Next
Set ref = .AttachedTemplate.VBProject.References(strReference)
If Err.Number = 0 Then
Debug.Print "Found(3): " & ref.Name
Else
Debug.Print "Not Found(3): " & strReference, Err.Number,
Err.Description
End If
.Close
End With
Word object?
I want to do this from within VB 6, but I find that I cannot do this even
from within Word itself?
For example, using the code below (obvious pieces of code are omitted), one
gets the desired result, i.e., all of the following are executed.
Debug.Print "Found(1): " & ref.Name
Debug.Print "Found(2): " & ref.Name
Debug.Print "Found(3): " & ref.Name
However, if I instead use the Word object, say:
dim appWord as Word.Application
set appWord = New Word.application
and use, e.g.,
Set docNew = appWord.Documents.Add(NewTemplate:=False,
DocumentType:=wdNewBlankDocument, _
Template:=strTemplate)
instead of
Set docNew = Documents.Add(NewTemplate:=False,
DocumentType:=wdNewBlankDocument, _
Template:=strTemplate)
The 3rd test fails and the following is executed:
Debug.Print "Not Found(3): " & strReference, Err.Number,
Err.Description
Is this behavior documented?
Is there a way to SAVE a project after adding a Reference when automating
Word?
-----------------------------------------------
Dim docWord As Word.Document
Dim docNew As Word.Document
Dim ref As VBIDE.Reference
Set docNew = Documents.Add(NewTemplate:=False,
DocumentType:=wdNewBlankDocument, _
Template:=strTemplate)
Set docWord = docNew.AttachedTemplate.OpenAsDocument
With docWord
' add a reference here, then do the following
For Each ref In .VBProject.References
Debug.Print ref.Name
Next ref
If .VBProject.Saved Then
Debug.Print "Project was not modified"
Else
Debug.Print "Project was modified"
End If
On Error Resume Next
Set ref = .VBProject.References(strReference)
If Err.Number = 0 Then
Debug.Print "Found(1): " & ref.Name
Else
Debug.Print "Not Found(1): " & strReference, Err.Number,
Err.Description
End If
.Save
.Close savechanges:=wdSaveChanges
End With
With docNew
For Each ref In .AttachedTemplate.VBProject.References
Debug.Print ref.Name
Next ref
On Error Resume Next
Set ref = .AttachedTemplate.VBProject.References(strReference)
If Err.Number = 0 Then
Debug.Print "Found(2): " & ref.Name
Else
Debug.Print "Not Found(2): " & strReference, Err.Number,
Err.Description
End If
.Close
End With
Set docWord = Documents.Add(NewTemplate:=False,
DocumentType:=wdNewBlankDocument, _
Template:=strTemplate)
With docWord
For Each ref In .AttachedTemplate.VBProject.References
Debug.Print ref.Name
Next ref
On Error Resume Next
Set ref = .AttachedTemplate.VBProject.References(strReference)
If Err.Number = 0 Then
Debug.Print "Found(3): " & ref.Name
Else
Debug.Print "Not Found(3): " & strReference, Err.Number,
Err.Description
End If
.Close
End With