Re: Can one save a reference added to project when automating Word

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Howard Kaikow (kaikow_at_standards.com)
Date: 07/05/04


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



Relevant Pages

  • Re: Counting how many time a template is accessed
    ... This template has been loaded onto everyone's ... >>>Public Sub AutoExec() ... >>>Private Const mcIncrementSize As Long = 10 ... >>>Private mstrCounterFile As String ...
    (microsoft.public.word.vba.general)
  • Count Template Usage
    ... I need to count how many times each template is used. ... Dim strUserName As String, cCounterFileFullPath As String ... Private Const mcIncrementSize As Long = 10 ...
    (microsoft.public.word.vba.general)
  • Re: Counting how many time a template is accessed
    ... An AddIn is a template that is generally loaded Automatically when Word starts. ... Public Sub AutoExec() ... Private Const mcIncrementSize As Long = 10 ... Private mstrCounterFile As String ...
    (microsoft.public.word.vba.general)
  • Re: Can one save a reference added to project when automating Word
    ... > changed template, the subsequent creation of a new doc still is using the ... Private blnUseAppWord As Boolean ... Private appWord As Word.Application ... Debug.Print RunTest1() ...
    (microsoft.public.word.vba.general)
  • Re: Can one save a reference added to project when automating Word
    ... >> changed template, the subsequent creation of a new doc still is using ... Private blnUseAppWord As Boolean ... Private appWord As Word.Application ... Debug.Print RunTest1() ...
    (microsoft.public.word.vba.general)