Results 1 to 6 of 6
  1. #1
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cannot add reference to Word template project (Word 2000, 2002, 2003, NOT

    It does not seem possible to add a reference to a Word template project via Automation.
    This seems like a rather serious bug.

    Can any of you reproduce the problem described below?

    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).
    <pre>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 FilesQualcommEudoraEudora.exe"
    ' strTargetPath has to be set to path where target template will be created
    Private Const strTargetPath As String = "C:TestVB-Test" ' For VBA
    Private Const strTemplate As String = strTargetPath & "TargetForRef.dot"

    Private docTemplate As Word.Document
    Private ref As VBIDE.Reference
    Private strOutput As String
    Private tmpWord As Word.Template

    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
    Set tmpWord = Templates(strTemplate)
    With tmpWord
    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 tmpWord = Nothing
    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
    Set tmpWord = appWord.Templates(strTemplate)
    With tmpWord
    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
    Set tmpWord = Nothing
    Set docTemplate = Nothing
    Set ref = Nothing
    appWord.Quit
    Set appWord = Nothing
    End Sub
    </pre>


  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Cannot add reference to Word template project (Word 2000, 2002, 2003,

    I see the same result using Office 2002 (SP-2): adding a reference through Automation does not "stick". I tried some variants without success. It certainly seems to be a bug, but when does one really need to set a reference through Automation?

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Cannot add reference to Word template project (Word 2000, 2002, 2003,

    I too tried lots of variants.

    One needs to set a reference via Automation when using an installation program written in say, VB 6 or VB .NET or VSTO, otherwise the user has to manually set the reference.

    I was writing such a program to install a DLL, add toolbar/menu to a user chosen template, then I decided I might as well add the reference, instead of asking the user to manually do so.

    Alas, it does not work with other than Word 97.

    No analogous problem with Excel. Appears to be a Word botch.
    A few daze ago, I spoke to an acquaintance who used to work at MSFT on the file systems. He agreed that it was a bug.

    Of course, if I report the bug to MSFT, it is unlikely the bug would get fixed for other than Word 2003.
    This means that the reference still could not get added with Word 2000 and Word 2002.

    I find it hard to believe that:

    1. This bug got into Word 2000 without folkes at MSFT noticing.
    2. The bug has persisted for 3 Word versions.
    3. I am the first to notice the bug.

  4. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Cannot add reference to Word template project (Word 2000, 2002, 2003,

    I've posted http://www.standards.com/OhMyWord/SetRefer...ordProject.html.

    Now, if I can only get a chance to report the problem to the Mayor of
    Redmond/Bellevue.

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Cannot add reference to Word template project (Word 2000, 2002, 2003,

    Forgot to mention that I did send a report of the problem to Microsoft on 22 July 2004.

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Cannot add reference to Word template project

    I may have found a workaround to the problem I described in this thread.

    I was finding an identical problem trying to change a template project's name.
    in my example for changing a template project's name (code not posted here), I found the following.

    If you programmatically Add a Code module, then everything gets saved as expected.

    So, my first impulse was that this was because of the template being dirtied.

    Not knowing when to quit, I ran the code again, Adding, then deleting the module, figuring that too would dirty the template.

    Well, I figured wrong, the module has to be present for the project name change to stick.

    I am hoping the same workaround will apply when I try to set references.

    I consider this to be either a very bad bug or very stupid design in Word.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •