VBA在Word 2016中编译通过,但在Word 2010中未能通过编译。

3
自Word 2010以来,微软推出了一项新功能,即LayoutColumns FootnoteOptions。因此,下面这行代码在Word 2016中编译:ActiveDocument.Range.FootnoteOptions.LayoutColumns,但在Word 2010中不能(我没有在Word 2013中测试过)。条件编译语句似乎没有帮助……除了包括Word 2010的VBA7之外,没有任何应用程序版本。

https://msdn.microsoft.com/VBA/Language-Reference-VBA/articles/compiler-constants

所以这在Word 2010中无法编译:
Sub testWd10()
#If Win64 And VBA7 Then
    ActiveDocument.Range.FootnoteOptions.LayoutColumns
#End If
End Sub

Compiler error - Method or data member not found


似乎是“Office 2013及更高版本”https://msdn.microsoft.com/zh-cn/library/microsoft.office.interop.word.footnoteoptions.layoutcolumns.aspx - Slai
3个回答

5
编译指令无法帮助您,您需要确定版本并针对那些在旧版本Word中没有的成员调用使用后期绑定。
Sub testWd10()
  If Application.Version > 15 Then 'e.g. 15 is Word 2013, change as necessary
      Dim myRange As Object 'As Range
      Set myRange = ActiveDocument.Range
      myRange.FootnoteOptions.LayoutColumns 'Late-bound call
  End If
End Sub

啊...这比我刚才想出来的要好。 - SlowLearner

3

我有点晚了,但还有几种更晚绑定的替代方案:

Dim o As Object
Set o = ActiveDocument.Range.FootnoteOptions
On Error Resume Next
o.LayoutColumns = 3
On Error GoTo 0

稍微简短和缓慢一些:
On Error Resume Next
CallByName ActiveDocument.Range.FootnoteOptions, "LayoutColumns", vbSet, 3
On Error GoTo 0

或者:

On Error Resume Next
CVar(ActiveDocument.Range.FootnoteOptions).LayoutColumns = 3
On Error GoTo 0

很酷,谢谢,我也会看看它们 - 有多种选择总是好的! - SlowLearner
1
你所说的CObj是什么?VBA.CVar(ActiveDocument.Range.FootnoteOptions).LayoutColumns = 3在2013年可以使用。 - ThunderFrame
感谢@ThunderFrame。不确定VBA是否像VB.Net一样有CObj,而这个页面似乎表明它确实有http://www.functionx.com/vbaexcel/functions/builtin.htm - Slai

0

这个答案不如ThunderFrame的好,因为我认为将范围设置为对象可能比整个应用程序更有效率,但是还是来试一下:

Sub testWd10()
    Dim wdApp As Object
    Set wdApp = Application

    If wdApp.Version > 14 Then
        wdApp.Documents(ActiveDocument.FullName).Range.FootnoteOptions.LayoutColumns
    End If
End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接