How to write late-binding code in XL VBA for Word objects?

E

Ed from AZ

I'm trying to port working Word VBA code into an Excel VBA module.
Because I don't know which version of Word my users have, I'm writing
it as late-binding code (everything Word is Dim'd as Object). I think
'most everything is going okay so far (we'll find out when I smoke
test!), but I'm wondering about certain Word objects that use methods
and properties not found in Excel.

My question is: when the Word Application object is set and the other
Word objects (docs and ranges) are set, will they be able to access
all the properties and methods of the Word object as if they were
early-bound?

For instance:
rngDoc.Find.Execute _
FindText:="MyText"
Excel Find does not have the Execute method nor the FindText property.

and

rngDoc.MoveEnd Unit:=wdParagraph
likewise has no Excel equivalent.

Should this compile and run okay? Or am I going to have to find new
ways to write this that are compatible with a late-binding routine?

Ed
 
J

Jim Thomlinson

The properties and methods should be just fine. Once bound to the object at
run time all of the properties and methods of the object will be exposed.
Where you can have an issue is with constants like wdBorderLeft which will
not exist. You will need to replace those constants with their numeric
equivalent (-2 in this case) or you can declare the constants publicly in a
module

Public Const wdBorderLeft as Long = -2

(I kinda like that option as it makes things a bit more readable but to each
his own)
 
E

Ed from AZ

Thanks, Jim. I appreciate the boost.

The next question, then, is how to find the constants? Is there an on-
line reference, perhaps? Or do I just have to hunt-and-peck each one
in Word VBA?

Ed
 
J

Jim Thomlinson

Make sure that you have Option Explicit declared at the top of each code
module so that VBA won't declare those as varaibles for you on the fly. Now
your code will not compile if any of those constants exist. Once you have
found them go into Word VBA and hit F2 to bring up a listing of all of the
objects and constants and such. When you find the constants you are looking
for select them and you will be given the value associated with that constant.
 
E

Ed from AZ

Nice!! Thanks much, Helmut.

I wonder what code he used to get that? I'd like to do that for
Excel.

Ed
 
E

Ed from AZ

I looked over that Fox wiki site, but I must have missed that one.
Thanks for the link.

I followed some link that told me how to use the Object Browser to
view the constants (open the VBA Object browser, select the library to
search, type in the two-letter constant prefix - like "wd" or "xl" -
and hit search), but you have to select each one individually and read
the numerical constant at the bottom of the screen. A bit much for me
on my current life schedule! 8>)

Thanks again for all the help.

Ed
 
J

Jay Freedman

From Word, start the VBA editor, display the Object Browser (shortcut:
F2) and select the various classes whose names start with wd (for
Word). For Excel, run the editor from Excel and display the classes
that start with xl.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
P

Peter Jamieson

Probably directly from the .olb or .tlb - e.g. you can get the readable IDL
using the OLE Viewer in Visual Studio and look for typedefs, and there are
doubtless other tools out there that can help.
 

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