Late binding and early binding

S

scorpion53061

It seems the safer way to go prior to deployment is to change my early
binding to late binding to prevent lots of issues occuring.

such as

Public WordApp As Object = CreateObject("Word.Application")
Public oDoc As Object = CreateObject("Word.Document")

instead of

'Public WordApp As New Word.Application
'Public oDoc As Word.Document

Does this mean I can remove the references to the Word object library and
such?

I have run into one issue concerning this.

When I tried to do

Public dlg as Object = Creatobject("Word.Dialog")

instead of

'Public dlg As Word.Dialog

it says it cannot create the active x component. How shall I use late
binding with this?

I much appreciate your answers
 
T

Tom Winter

scorpion53061 said:
It seems the safer way to go prior to deployment is to change my early
binding to late binding to prevent lots of issues occuring.

such as

Public WordApp As Object = CreateObject("Word.Application")
Public oDoc As Object = CreateObject("Word.Document")

instead of

'Public WordApp As New Word.Application
'Public oDoc As Word.Document

Does this mean I can remove the references to the Word object library and
such?

I have run into one issue concerning this.

When I tried to do

Public dlg as Object = Creatobject("Word.Dialog")

instead of

'Public dlg As Word.Dialog

it says it cannot create the active x component. How shall I use late
binding with this?

I much appreciate your answers

Binding doesn't (necessarily) have anything to do with how you CREATE the
object. It has to do with how you REFERENCE the object. The only time you
use CreateObject() is when YOU are CREATING the object, as in the case of
the Word.Application. Word.Document objects are NOT created by YOU, they are
created by Word. You simply reference them. The key to late binding is that
you DIM your variables AS OBJECT. Here is some sample code:

Public WordApp as Object
Public oDoc as Object

Set WordApp = CreateObject("Word.Application")

Set oDoc = WordApp.Documents(1)

As for the Dialog problem, from the Word 2003 Help file: "You cannot create
a new built-in dialog box or add one to the Dialogs collection." You are
trying to CREATE the object, which you cannot do, Word does this. You simply
reference it:

Public dlg as Object

Set dlg = WordApp.Dialogs(80) ' 80=wdDialogFileOpen

Once you have switched to all late binding, you can remove the reference to
Word and Office type libraries.

BUT, this might be going too far. If you really understand how late/early
binding works and how to reference the Word/Office type libraries, you
shouldn't have problems using early binding. Here is an article that
explains it real well:

INFO: Writing Automation Clients for Multiple Office Versions
http://support.microsoft.com/default.aspx?scid=kb;EN-US;244167

-Tom
 
S

scorpion53061

BUT, this might be going too far. If you really understand how late/early
binding works and how to reference the Word/Office type libraries, you
shouldn't have problems using early binding.

Hi Tom,

thank you much for your repsonse.

This is the error that occured with my client and below that the link that
told me what to do about it.

Do you have any thoughts on how I could keep early binding in place and
resolve this? The client is not wanting to mess with their machines.

http://support.microsoft.com/default.aspx?scid=kb;en-us;292744

BUG: Automation Client Receives Error or Crashes Calling Word's Find Object

RESOLUTION
To resolve this problem, do one of the following:
a.. Modify your code to use late binding when you call methods or
properties on any of the above listed Word interfaces. -or-


b.. Reregister the Word type library on the system on which the problem
occurs.
The recommended solution is to use late binding. Because both type libraries
describe interfaces that implement the IDispatch interface, calls to any of
the IDispatch methods work regardless of which library was last registered
on the system. This is the only way to be sure that your code does not
encounter this error on systems that you do not administer.
 
T

Tom Winter

scorpion53061 said:
Hi Tom,

thank you much for your repsonse.

This is the error that occured with my client and below that the link that
told me what to do about it.

Do you have any thoughts on how I could keep early binding in place and
resolve this? The client is not wanting to mess with their machines.

http://support.microsoft.com/default.aspx?scid=kb;en-us;292744

BUG: Automation Client Receives Error or Crashes Calling Word's Find Object

RESOLUTION
To resolve this problem, do one of the following:
a.. Modify your code to use late binding when you call methods or
properties on any of the above listed Word interfaces. -or-


b.. Reregister the Word type library on the system on which the problem
occurs.
The recommended solution is to use late binding. Because both type libraries
describe interfaces that implement the IDispatch interface, calls to any of
the IDispatch methods work regardless of which library was last registered
on the system. This is the only way to be sure that your code does not
encounter this error on systems that you do not administer.

That's an odd one. I would go ahead and use late binding here for the Find
object. One thing to keep in mind is that you can mix early and late binding
in the same project, even with the same object! Here's a sample:

Dim oDocumentEarlyBind as Word.Document

Dim oFindEarlyBind as Word.Find
Dim oFindLateBind as Object

Set oDocumentEarlyBind = oWordApp.ActiveDocument ' oWordApp setup
somewhere else

Set oFindEarlyBind = oDocumentEarlyBind.Content.Find

oFindEarlyBind.Text = "sample" ' These calls are early bound
oFindEarlyBind.Font.Name = "Arial"

Set oFindLateBind = oFindEarlyBind

oFindLateBind.Font.Size = 10 ' These calls are late bound
oFindLateBind.Execute

That's contrived, but you get the idea. You can use early binding for all of
your project except when you have to deal with the Find object. Put the Find
object into a late bound variable and then access it as you need:

Dim oDocument As Word.Document

Dim oFind As Object

Set oDocument = oWordApp.ActiveDocument ' oWordApp setup somewhere
else

Set oFind = oDocument.Content.Find

oFind.Text = "sample"
oFind.Execute

That should do it!

-Tom
(e-mail address removed)
 
S

scorpion53061

yep that is what I did.

I was concerned about mixing bindings but you confirm I can do both and it
is okay to do so....Thank you!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top