后期绑定wdGoToBookmark [英] Late binding wdGoToBookmark

查看:58
本文介绍了后期绑定wdGoToBookmark的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用后期绑定.如何替换以下内容?

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

感谢您急需的帮助

推荐答案

FWIW,我不会简单地创建常量并删除早期绑定.毕竟,我们早早地利用了代码的易于开发和编译时验证的能力.

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.

我强烈建议您编写代码,以便可以通过轻按一下开关进行切换.对于OP的特定代码,我们可以实现以下目标:

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:

Sub CopyCell(wd As Object, stringcell As String, BookMarkName As String)

#If LateBind Then
  Const wdGoToBookmark As Long = -1
#Else
  Debug.Assert wdGoToBookmark = -1
#End If
       'find Word bookmark
        wd.Selection.GoTo What:=wdGoToBookmark, Name:=BookMarkName

        wd.Selection.TypeText stringcell 
 End Sub

LateBind const可以在每个模块的声明部分中定义为: #Const LateBind = 1

The LateBind const can be either be defined on a per-module in its declaration section as: #Const LateBind = 1

或者可以通过转到Options-> <project name> Properties并将其放在Conditional Compilation Arguments中来为整个项目进行定义.

Or it can be defined for whole project by going to Options -> <project name> Properties and putting it in Conditional Compilation Arguments.

此方法可以扩展为其他方法.例如,要创建Word.Application,我们可以执行以下操作:

This approach can be expanded into other. For example, to create Word.Application, we can do something similar to this:

#If LateBind Then
  Dim app As Object
#Else 
  Dim app As Word.Application
#End If

Set app = CreateObject("Word.Application")

对于需要获取或返回对象的函数,可以具有两个头:

And for functions that needs to take or return objects can have two heads:

#If LateBind Then
Sub CopyCell(wd As Object, stringcell As String, BookMarkName As String)
#Else
Sub CopyCell(wd As Word.Document, stringcell As String, BookMarkName As String)
#End If
  '<rest of procedure body>
End Sub

为什么要编写更多代码?这样您就可以简单地添加/删除对库的引用,将LateBind常量更改为其他值,然后进行编译.现在,您可以轻松地在两种模式之间进行切换,更重要的是,您可以很轻松地在编译时验证代码,并可以合理地保证它在后期绑定模式下也可以正常工作.不一定总是正确的,但这比仅转储任何早期绑定的代码并希望获得最好的效果要好.运行时错误对开发人员来说是一个威胁,在开发过程中应尽可能避免.

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.

这篇关于后期绑定wdGoToBookmark的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆