Re: Can one save a reference added to project when automating Word
From: Howard Kaikow (kaikow_at_standards.com)
Date: 07/05/04
- Next message: Howard Kaikow: "Re: Can one save a reference added to project when automating Word"
- Previous message: Howard Kaikow: "Re: create shortcut to active document and place in a folder"
- In reply to: Howard Kaikow: "Re: Can one save a reference added to project when automating Word"
- Next in thread: Howard Kaikow: "Re: Can one save a reference added to project when automating Word"
- Reply: Howard Kaikow: "Re: Can one save a reference added to project when automating Word"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 5 Jul 2004 17:41:22 -0400
It does appear that if working thru the Word object, a copy of the template
is being used, so the reference fails because, although I've saved the
changed template, the subsequent creation of a new doc still is using the
old copy of the template.
I've even tried opening the template directly as a document, but still the
changed ref does not appear to get saved until the code finishes running.
The following isolates the relevant code even further than my previous
examples.
Option Explicit
' strReference must be set to the reference name.
' Private Const strReference As String = "MyReference"
' strDLLPath has to be set to path of DLL.
' Const strDLLPath As String = "SomePath\SomeDLL.dll"
Private blnUseAppWord As Boolean
' blnUseWordApp = True, create new Word object
' blnUseWordApp = False, do not create new Word object
Private appWord As Word.Application
Private docWord As Word.Document
Private ref As VBIDE.Reference
Private strTemplate As String
Private Sub Main()
' strTemplate has to be set to path of template to which reference is to
be added
' strTemplate = CurDir$ & "\TestMe.dot" ' For VB
' strTemplate = "APath\TestMe.dot" ' For VBA
Set appWord = New Word.Application
blnUseAppWord = False
Debug.Print "False"
Debug.Print RunTest1()
Debug.Print RunTest2()
blnUseAppWord = True
Debug.Print "True"
Debug.Print RunTest1()
Debug.Print RunTest2()
blnUseAppWord = False
Debug.Print "False"
Debug.Print RunTest1()
Debug.Print RunTest2()
Debug.Print "False"
Debug.Print RunTest1()
Debug.Print RunTest2()
Set docWord = Nothing
Set ref = Nothing
appWord.Quit
Set appWord = Nothing
End Sub
Private Function RunTest1() As String
Dim docNew As Word.Document
If blnUseAppWord Then
Set docNew = appWord.Documents.Add(NewTemplate:=False,
DocumentType:=wdNewBlankDocument, _
Template:=strTemplate)
Else
Set docNew = Documents.Add(NewTemplate:=False,
DocumentType:=wdNewBlankDocument, _
Template:=strTemplate)
End If
Set docWord = docNew.AttachedTemplate.OpenAsDocument
docNew.Close
With docWord
With .VBProject
On Error Resume Next
' Remove reference
.References.Remove .References(strReference)
Err.Clear
' Add reference
Set ref = .References.AddFromFile(strDLLPath)
End With
If Err.Number = 0 Then
RunTest1 = "Reference was added."
.Save
Else
RunTest1 = "Could not add reference: " & Err.Number & ", " &
Err.Description
End If
.Close
End With
Set docNew = Nothing
End Function
Private Function RunTest2() As String
Dim docOther As Word.Document
If blnUseAppWord Then
Set docOther = appWord.Documents.Add(NewTemplate:=False,
DocumentType:=wdNewBlankDocument, _
Template:=strTemplate)
Else
Set docOther = Documents.Add(NewTemplate:=False,
DocumentType:=wdNewBlankDocument, _
Template:=strTemplate)
End If
With docOther
' .AttachedTemplate = NormalTemplate.Name
' .AttachedTemplate = strTemplate
On Error Resume Next
Set ref = .AttachedTemplate.VBProject.References(strReference)
If Err.Number = 0 Then
RunTest2 = "Test Passed - Reference " & strReference & " was
found."
Else
RunTest2 = "Test Failed - Reference " & strReference & " was not
found: " & _
Err.Number & ", " & Err.Description
End If
.Saved = True
.Close
End With
Set docOther = Nothing
End Function
- Next message: Howard Kaikow: "Re: Can one save a reference added to project when automating Word"
- Previous message: Howard Kaikow: "Re: create shortcut to active document and place in a folder"
- In reply to: Howard Kaikow: "Re: Can one save a reference added to project when automating Word"
- Next in thread: Howard Kaikow: "Re: Can one save a reference added to project when automating Word"
- Reply: Howard Kaikow: "Re: Can one save a reference added to project when automating Word"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|