PDA

View Full Version : Solved: Setting a reference in Word via Automation



Howard Kaikow
02-11-2005, 07:30 AM
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?SetReferenceInWordProject

Jacob Hilderbrand
02-11-2005, 08:37 AM
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.

Howard Kaikow
02-11-2005, 10:06 AM
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

fumei
02-15-2005, 09:06 AM
I can do it with Office 2002. What is your actual code

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

Howard Kaikow
02-16-2005, 09:27 AM
I can do it with Office 2002. What is your actual code

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

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.

Anne Troy
02-16-2005, 10:41 AM
The code from that document:

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

Hope you don't mind, Howard, but not everybody wants to go downloading files. :)

Anne Troy
02-16-2005, 10:43 AM
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

Howard Kaikow
02-16-2005, 12:09 PM
I do mind.

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

Anne Troy
02-16-2005, 12:15 PM
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

Howard Kaikow
02-16-2005, 12:28 PM
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.

fumei
02-17-2005, 11:23 AM
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.

Howard Kaikow
02-17-2005, 12:29 PM
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.

fumei
02-17-2005, 01:18 PM
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.

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

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

Howard Kaikow
02-17-2005, 11:50 PM
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

fumei
02-18-2005, 12:05 PM
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.

Howard Kaikow
02-18-2005, 12:21 PM
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.

fumei
02-18-2005, 12:57 PM
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.