I have to use late binding. How do i replace the following?
Sub CopyCell(wd As Object, stringcell As String, BookMarkName As String)
'find Word bookmark
wd.Selection.GoTo What:=wdGoToBookmark, Name:=BookMarkName
wd.Selection.TypeText stringcell
End Sub
Thx for the much needed help
In Word go to the VB editor and press F2 to bring up the Object Browser - search for wdGoToBookmark to find its numeric value. Use that value in your code or define a Constant as suggested by Mat's mug.
I would d keep it where it is, but declare it as a constant at module level, possibly in its own
WordConstants
module:Or better, recreate the enum types:
You can find the enum definitions on MSDN
That way this code remains legal:
It just doesn't resolve to a constant that's declared in a referenced library anymore.
Alternatively you could hard-code a magic
-1
value on-the-spot, but then it becomes much harder to know what it stands for, so putting it in a comment isn't a bad idea:FWIW, I would not simply just create constants and remove early-binding. After all, we early-bound to leverage the ease of development and compile-time validations of our code.
I strongly suggest that you write code so that it can be switched with a flip of switch. For OP's specific code, we can achieve this:
The
LateBind
const can be either be defined on a per-module in its declaration section as:#Const LateBind = 1
Or it can be defined for whole project by going to
Options
-><project name> Properties
and putting it inConditional Compilation Arguments
.This approach can be expanded into other. For example, to create
Word.Application
, we can do something similar to this:And for functions that needs to take or return objects can have two heads:
Why write more code? So that you can simply add/remove the reference to library, change the
LateBind
constant to the other value, then compile. You now can easily switch between two modes and more importantly you make it very easy for your code to be validated at the compile-time and can be reasonably reassured that it will work equally in late-bound mode. That's not necessarily always true but that's still better than simply dumping any traces of early-bound code, and hoping for the best. Runtime errors are menaces to the developers and should be avoided as much as possible during the development.