Consulting

Results 1 to 17 of 17

Thread: Solved: Setting a reference in Word via Automation

  1. #1
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location

    Solved: Setting a reference in Word via Automation

    I don't know whether I ever posted this here before (is there a way to get a list of all my postings/threads?).

    In any case, it appears impossible with Word 2000 and later to set a reference within a Word project and save the changed template IF done via a Word object instead of directly within th eactive Word session.

    See http://www.standards.com/index.html?...eInWordProject

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    To view all of your posts select the Posts drop down from the NavBar at the top of the page, then select My Threads.

    Alternately you can select the Search drop down and then select Advanced Search. Then fill in the information.

  3. #3
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by DRJ
    To view all of your posts select the Posts drop down from the NavBar at the top of the page, then select My Threads.

    Alternately you can select the Search drop down and then select Advanced Search. Then fill in the information.
    Thanx

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I can do it with Office 2002. What is your actual code

    [vba]Sub MakeRefInWord()
    Dim WordApp As Word.Application

    Set WordApp = CreateObject("word.application")
    WordApp.Documents.Open Filename:="c:\gerry\bookmarkRange2.doc"
    ActiveDocument.VBProject.References.AddFromFile _
    Filename:="c:\winnt\system32\scrrun.dll"
    ActiveDocument.Save
    WordApp.Quit
    Set WordApp = Nothing
    End Sub[/vba]

  5. #5
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by fumei
    I can do it with Office 2002. What is your actual code

    [vba]Sub MakeRefInWord()
    Dim WordApp As Word.Application

    Set WordApp = CreateObject("word.application")
    WordApp.Documents.Open Filename:="c:\gerry\bookmarkRange2.doc"
    ActiveDocument.VBProject.References.AddFromFile _
    Filename:="c:\winnt\system32\scrrun.dll"
    ActiveDocument.Save
    WordApp.Quit
    Set WordApp = Nothing
    End Sub[/vba]
    To replicate the behavior, you would have to use the WordApp instrance of Word, not the original instance of Word.

    It's easier to start with the code I posted.

    The code I am using is given at the link I posted in the original article in this thread.

  6. #6
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    The code from that document:

    [VBA]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 = "C:\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"
    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
    ' appWord.NormalTemplate.Saved = True
    Set tmpWord = Nothing
    Set docTemplate = Nothing
    Set ref = Nothing
    appWord.Quit
    Set appWord = Nothing
    End Sub[/VBA]

    Hope you don't mind, Howard, but not everybody wants to go downloading files.
    ~Anne Troy

  7. #7
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Quote Originally Posted by DRJ
    To view all of your posts select the Posts drop down from the NavBar at the top of the page, then select My Threads.

    Alternately you can select the Search drop down and then select Advanced Search. Then fill in the information.
    Jake: Those aren't Howard's threads!! They're MINE!!
    :rofl
    ~Anne Troy

  8. #8
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    I do mind.

    Such problems require examining the project references, etc.
    Those can be obtained only by using the actual downloads.

  9. #9
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    I'm not sure I understand that statement, Howard, since project references change depending on the computer on which you open it. Am I whacked? I open that doc---and I happen to be using 2002 right now--and the references are 10.0
    ~Anne Troy

  10. #10
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by Dreamboat
    I'm not sure I understand that statement, Howard, since project references change depending on the computer on which you open it. Am I whacked? I open that doc---and I happen to be using 2002 right now--and the references are 10.0
    In the general case, it is necessary to examine ALL the references used by a project.

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I am missing something. If the object is to add a reference to a template, then save it, then as long as you have the needed reference (VBA Extensibility) to add a reference - then you can. A template is also a document. If I have a template, a .DOT file, which does NOT have a reference to, say, Microsoft Scripting Runtime

    Sub AddReference ()
    ActiveDocument.VBProject.References.AddFromFile _
        FileName:="c:\winnt\system32\scrrun.dll"
    ActiveDocument.Save
    ActiveDocument.Close
    End Sub
    adds the reference and saves the file (still as a .DOT), then closes it.

    If the file reopened (as a .DOT) the reference has been added.
    If a document is created from the template it also has the reference.

    What am I missing???? The fact that it is a .DOT, a template is not relevant. The Project references are objects of VBProjects are do not care if it is a .DOT, or a .DOC.

    What am I missing???? It seems to not require anything other than AddFromFile.

    Word 2002.

  12. #12
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by fumei
    I am missing something. If the object is to add a reference to a template, then save it, then as long as you have the needed reference (VBA Extensibility) to add a reference - then you can. A template is also a document. If I have a template, a .DOT file, which does NOT have a reference to, say, Microsoft Scripting Runtime

    Sub AddReference ()
    ActiveDocument.VBProject.References.AddFromFile _
        FileName:="c:\winnt\system32\scrrun.dll"
    ActiveDocument.Save
    ActiveDocument.Close
    End Sub
    adds the reference and saves the file (still as a .DOT), then closes it.

    If the file reopened (as a .DOT) the reference has been added.
    If a document is created from the template it also has the reference.

    What am I missing???? The fact that it is a .DOT, a template is not relevant. The Project references are objects of VBProjects are do not care if it is a .DOT, or a .DOC.

    What am I missing???? It seems to not require anything other than AddFromFile.

    Word 2002.
    I stated that the problem occurs only when doing this via Automation, i.e., via a Word object in another app or in Word itself.

    Your examples have not been using Automation.

    Take a look at the code at the URL I gave, it gives an example of both cases.

    And, I have not tested whether the problem occurs using a document, the test case is using a template because that is whatis required in the particular app.

  13. #13
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Again, what am I missing? I do not see the need for all your code. You make an instance of Word, OR use the existing one - it does not make ANY difference - and open the template. Add the reference, and save the file.

    I think you missed my point with your last paragraph. if your app requires a template - fine, use a template. It does not make ANY difference.

    You want Automation? Here you go. You may notice that it is not all that much different from the last one, because you do not need all that stuff. The other things you are doing, looping through the references are all possible with this as well. These are procedures fired from Excel.

    [vba]Sub MakeNewReferenceInWordTemplate()
    Dim wordApp As Word.Application
    Set wordApp = CreateObject("Word.Application")
    wordApp.Visible = False
    wordApp.Documents.Open Filename:= _
    "C:\Documents and Settings\gerry.knight\Application Data\Microsoft\Templates\testref.dot"
    wordApp.ActiveDocument.VBProject.References.AddFromFile _
    Filename:="c:\winnt\system32\scrrun.dll"
    wordApp.ActiveDocument.Save
    wordApp.ActiveDocument.Close
    Set wordApp = Nothing
    End Sub

    Sub MakeAnotherRefInWordTemplate()
    Dim wordApp As Word.Application
    Set wordApp = New Word.Application
    wordApp.Visible = False
    wordApp.Documents.Open Filename:= _
    "C:\Documents and Settings\gerry.knight\Application Data\Microsoft\Templates\testref.dot"
    wordApp.ActiveDocument.VBProject.References.AddFromFile _
    Filename:="c:\winnt\system32\scrrun.dll"
    wordApp.ActiveDocument.Save
    wordApp.ActiveDocument.Close
    Set wordApp = Nothing
    End Sub[/vba]

    So OK, again, what am I missing? And yes, BTW, I have looked at your file.

  14. #14
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    The following demonstrates that the reference is not saved in Word 2003.
    Should be same result in Word 2000 and Word 2002.

     Sub DemonstrateRefIsNotSaved()
    	Const strTargetPath As String = "C:\Test\VB-Test"
    	Const strTemplate As String = strTargetPath & "\TargetForRef.dot"
    	Dim wordApp As Word.Application
    	
    	Dim ref As VBIDE.Reference
    	Dim strOutput As String
    	
    	Set wordApp = CreateObject("Word.Application")
    	With wordApp
    		.Visible = False
    		.Documents.Open FileName:=strTemplate
    		With .ActiveDocument
    '		    .VBProject.References.AddFromFile FileName:="C:\Windows\system32\scrrun.dll"
    '		    .VBProject.References.AddFromFile FileName:="F:\winnt\system32\scrrun.dll"
    '		    .VBProject.References.AddFromFile FileName:="G:\winnt\system32\scrrun.dll"
    		    .VBProject.References.AddFromFile FileName:="J:\winnt\system32\scrrun.dll"
    			.Save
    			strOutput = ""
    			For Each ref In .VBProject.References
    			    strOutput = strOutput & vbCrLf & ref.Name
    			Next ref
    			'Added ref will be in template
    			Debug.Print strOutput
    			.Close
    		End With
    		.Quit
    	End With
    	'Added ref is no longer in template
    	Documents.Open FileName:=strTemplate
    	strOutput = "----------------" & vbCrLf
    	For Each ref In ActiveDocument.VBProject.References
    		strOutput = strOutput & vbCrLf & ref.Name
    	Next ref
    	ActiveDocument.Close
    	Debug.Print strOutput
    	Set wordApp = Nothing
    	Set ref = Nothing
    End Sub

  15. #15
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Very interesting Howard.

    I had no problems in getting the references to persist...until after I opened your file. Now, I too can not get added references to persist - exactly duplicating your problem.

    I could before, but no longer can....at all.

    Further, I have now tested a file on 15 different machines, setting a new reference by automation. They all work exactly as they are supposed to. The reference added DOES persist.

    Now the kicker. If I have a file on my machine, my code, as stated does do what your does...it does not persist. The reference is there, but if you close the file and reopen, it is no longer there. If that file is sent to another machine, I can add the reference ON THAT MACHINE, save the file, close, open it...and the new reference is there. It persists. If THAT file, WITH the new reference is sent back to my machine.....the reference is NOT there.

    In fact, I can no longer add ANY references to any file, even manually in the VBE. I now can not add references whatsoever, by code or manually.

    Time for some cleanup.

    Oh, and yes, I can, on other machines, add references that persist using Automation. Can't on this machine any more.....can't add ANY references. I suggest you look at your system.

  16. #16
    VBAX Mentor
    Joined
    Sep 2004
    Location
    Nashua, NH, USA
    Posts
    489
    Location
    Quote Originally Posted by fumei
    Very interesting Howard.

    I had no problems in getting the references to persist...until after I opened your file. Now, I too can not get added references to persist - exactly duplicating your problem.

    I could before, but no longer can....at all.

    Further, I have now tested a file on 15 different machines, setting a new reference by automation. They all work exactly as they are supposed to. The reference added DOES persist.

    Now the kicker. If I have a file on my machine, my code, as stated does do what your does...it does not persist. The reference is there, but if you close the file and reopen, it is no longer there. If that file is sent to another machine, I can add the reference ON THAT MACHINE, save the file, close, open it...and the new reference is there. It persists. If THAT file, WITH the new reference is sent back to my machine.....the reference is NOT there.

    In fact, I can no longer add ANY references to any file, even manually in the VBE. I now can not add references whatsoever, by code or manually.

    Time for some cleanup.

    Oh, and yes, I can, on other machines, add references that persist using Automation. Can't on this machine any more.....can't add ANY references. I suggest you look at your system.
    I use a multiboot system, each OS has a different version of Office. In effect, each is a different system. Te problem exists for Word 2000 and up, but not for Word 97.

    I first posted this problem, in another forum, about 7 months ago. At that time, some others confirmed the problem. I reported this to MSFT, but got the expected non-response.

    Note that the problem appears to be Word specific, e.g., I can save references in Excel.

    This impacts some VB 6 installation software that needs to assure a correct reference in a Word template. It looks like I'll have to add the reference each time the program runs, rather than just once during the install.

  17. #17
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yeah, well maybe you can set references in Excel...but I can not now. Could before, but now while I can SET a reference, and while that file is still open, use the references, once closed, ALL new references have disappeared. In Word, AND Excel.

    Bloody pain.

Posting Permissions

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