Programatically set Reference

CEH

Curtis
Local time
Today, 09:55
Joined
Oct 22, 2004
Messages
1,187
Having a bit of a problem... Not sure if it is me or Access 2007 Runtime. I have a DB that does Job Bids, after a form is complete there is a button that opens a Word Document template and fills in the info from the form. I designed this in Access 2003, converted that over to 2007 and used it as my "Runtime" testing DB... What I am coming up with are errors when you click the button to open the Word template. It looks like it is looking for a reference that isn't there. OF course by using the 2007 Runtime there is no way to open it and set the reference. Also I might have 2 different versions of Word I am working with. In the past this hasn't been a problem... I thought Access simply found the correct reference.... For example if it was set for Word 11 at home on another computer and opened at work where Word 10 was installed it found and changed the reference and worked fine.......
So the question is...How do I make it find the new or correct reference to the correct version of Word being used?
code on the button starts as.....
Public Function MergetoWord()
' This method creates a new document in MS Word using Automation.
On Error Resume Next

Dim WordObj As Word.Application
Dim TemplateChoice As String

Set WordObj = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordObj = CreateObject("Word.Application")
End If


WordObj.Visible = True
'Code here to allow choice of Template
Dim strMyTemplatePath As String
strMyTemplatePath = "C:\ContractTemplate.dot"
............so on..........
 
Curtis:

Instead of using early binding:
Dim WordObj As Word.Application

use late binding instead:
Dim WordObj As Object

and then you won't be stymied by the references. Of course, the drawback to this is that you then can't use intellisense for other objects as Access doesn't know until you run it that you are creating a Word object.
 
Use late binding in stead.

This would mean the following declaration

Dim WordObj As Object ' Word.Application

and, you'd need remove all the wd<constantname> used around and replace with the number they represent. I e in stead of wdRow use 10, in stead of wdColumn use 9 (everything found through Debug.Print wd<constantname>, then also remove the reference to Word.
 
Just so Curtis knows, there is a performance penalty when using late binding. If Word acts too slow to your liking, you may want to do it the hard way.
 
Just so Curtis knows, there is a performance penalty when using late binding. If Word acts too slow to your liking, you may want to do it the hard way.

So who gets the power play when you are in the performance penalty box? :D :D
 
Access itself gets the power play but all you get are H-E-double-hockey-sticks until you can get the puck out of there.
 
Anybody tested how "bad" this performance degradation is on todays computers?
 
Anybody tested how "bad" this performance degradation is on todays computers?

Could be wrong, but I think this is more dependent on how we're dealing with objects, not the hardware; if we're doing some simple things, then big whoops. But if you're trying to cure the cancer with the procedures, then the difference will be more noticeable.
 
Somethins wrong here........
I only saw one place to change the code...... But now it is not inserting the bookmarks.... Opens the Word template...but then nothing. What I'm I missing??
(As told I also removed the reference to Word)

Public Function MergetoWord()
' This method creates a new document in MS Word using Automation.
On Error Resume Next

Dim WordObj As Object
Dim TemplateChoice As String

Set WordObj = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set WordObj = CreateObject("Word.Application")
End If


WordObj.Visible = True
'Code here to allow choice of Template
Dim strMyTemplatePath As String
strMyTemplatePath = "C:\ContractTemplate.dot"

WordObj.Documents.Add Template:=strMyTemplatePath, NewTemplate:=False
WordObj.Selection.GoTo what:=wdGoToBookmark, Name:="BkMark1"
'If IsNull([BusinessName]) Then
'WordObj.Selection.TypeText ""
'WordObj.Selection.TypeBackspace
'Else
WordObj.Selection.TypeText [ContactBusinessName]
'End If

WordObj.Selection.GoTo what:=wdGoToBookmark, Name:="BkMark2"
WordObj.Selection.TypeText [ContactAddress]

WordObj.Selection.GoTo what:=wdGoToBookmark, Name:="BkMark3"
WordObj.Selection.TypeText [ContactCity]

WordObj.Selection.GoTo what:=wdGoToBookmark, Name:="BkMark4"
WordObj.Selection.TypeText [ContactState]


WordObj.Selection.GoTo what:=wdGoToBookmark, Name:="BkMark5"
WordObj.Selection.TypeText [ContactZip]


WordObj.Selection.GoTo what:=wdGoToBookmark, Name:="BkMark6"
WordObj.Selection.TypeText [BidDate]


WordObj.Selection.GoTo what:=wdGoToBookmark, Name:="BkMark7"
WordObj.Selection.TypeText [txtBidAnnual]



WordObj.Selection.GoTo what:=wdGoToBookmark, Name:="BkMark8"
WordObj.Selection.TypeText [BidInformation]

WordObj.Selection.GoTo what:=wdGoToBookmark, Name:="BkMark9"
WordObj.Selection.TypeText [SalesmanName]


DoEvents
WordObj.Activate
'Sets cursor position?
WordObj.Selection.MoveDown wdLine, 2 ' Number = Number of Lines to move
' Set the Word Object to nothing to free resources
Set WordObj = Nothing

DoCmd.Hourglass False

Exit Function

TemplateError:
Set WordObj = Nothing
Exit Function

End Function
 
As I said, you must replace the constants with the number it represents, i e in stead of

WordObj.Selection.GoTo what:=wdGoToBookmark, ...

use

WordObj.Selection.GoTo what:=-1, ...

and wdLine with 5 further down.

There might be other issues, too, but try that first.
 
OK Guys, Thank you! Only had to do one other thing... enclose the fields in parentheses .... ([SalesmanName])
Works fine....... Now tomorrow we'll see if it works when changing Word versions.
I do need a little education here.... What is the difference between early binding and late binding.......

Thanks again Guys...........
 
Last edited:
In programming terms, "binding" means "establishing the location of something" and it can be done at any time before the first reference to that location.

In early binding, you take steps to establish the location using static pointers, names, or generally fixed elements.

In late binding, you take steps to establish the location only after you know you are ging to use it IN THE CURRENT INCARNATION of whatever you are running.

The practical difference is that with early binding you know enough about the object to establish properties, offsets, and other qualifying items. It is early enough that intellisense technology can auto-complete VBA elements as you are coding them.

In late binding, you don't know where something actualy is located because you have to use more complex dynamic pointers to same. These pointers have to be established at the last minute, which means that the code you wrote probably didn't work with intellisense very well.

If you know compiler theory, then early binding is compiled code with local addresses for everything. Late binding is using an address variable into a data structure that is created only after the program is started - and as a result, you end up with a lot of relative references rather than absolute ones.

On modern computers, this is less of a problem than it used to be when you ran big, whompin' programs for which any level of indirection was an effort. But even now, late binding can cause some functions to not work properly.
 

Users who are viewing this thread

Back
Top Bottom