【问题标题】:Having troubles creating and formatting Word tables from Excel VBA从 Excel VBA 创建和格式化 Word 表格时遇到问题
【发布时间】:2019-06-20 08:22:27
【问题描述】:

我是 MS Word VBA 的新手,在处理 Excel 中的 Word 文档时遇到了麻烦。

目前最大的问题是:在 Word VBA 中工作的代码在 Excel 中不工作。非常奇怪和令人沮丧。

以下是代码:

Sub abc()
Dim MSWordApp As Object, MSWordDoc As Object

    Set MSWordApp = CreateObject("Word.Application")
    Set MSWordDoc = MSWordApp.Documents.Add
    MSWordApp.Visible = True
    With MSWordDoc
        With .PageSetup
            .TopMargin = Application.CentimetersToPoints(0.51)
            .BottomMargin = Application.CentimetersToPoints(0.51)
            .LeftMargin = Application.CentimetersToPoints(0.51)
            .RightMargin = Application.CentimetersToPoints(0.51)
        End With
        .Tables.Add Range:=.Range(0, 0), NumRows:=3, NumColumns:=2
        With .Tables(1)
            .Rows.Alignment = wdAlignRowCenter
            .Rows.HeightRule = wdRowHeightExactly
            .Rows.Height = Application.CentimetersToPoints(9.55)
            .Columns.PreferredWidthType = wdPreferredWidthPoints
            .Columns.PreferredWidth = Application.CentimetersToPoints(9.9)
        End With
    End With

    MSWordApp.Activate
    Set MSWordApp = Nothing
    Set MSWordDoc = Nothing
End Sub

这些代码在 MS Word 中完美运行(当然,当我在 MS Word 中使用它们时,我已经更改了对象等的名称)。

然而,奇怪的事情发生在 Excel 中:

1) ".Rows.Alignment = wdAlignRowCenter" 根本不起作用。 Word 表的 Rows.Alignment 保持默认。

2) ".Columns.PreferredWidthType = wdPreferredWidthPoints" 导致 Excel 出错。它在 Word 中运行良好;虽然在 Excel 中,每次调用此属性时都会弹出一个空的错误消息。不知道为什么...

【问题讨论】:

  • 您是否在 excel VBA 模块中添加了对 Word VBA 库的引用?
  • @Greg Viers ...上帝。你说的正是原因。愚蠢的新手错误
  • 我会为其他出现的新手提交正确的答案。 :)

标签: excel vba ms-word


【解决方案1】:

从 Excel VBA 控制 Microsoft Word 时,您需要添加对 Microsoft Word 对象库的引用。为此,请确保您位于 VBA 窗口中的模块上,然后单击 Tools 然后单击 References...。在弹出窗口中,向下滚动以找到“Microsoft Word XX.X 对象库”。版本 # 将根据您安装的内容而有所不同。

如果它没有显示在列表中,您可以在硬盘上找到它,方法是单击“浏览...”并导航到安装了 MS Word 的程序文件文件夹,然后选择名为“MSWORD”的文件.OLB"。

【讨论】:

    【解决方案2】:

    由于您的代码是为后期绑定而编写的,因此您不应添加对 Word 的引用。相反,您需要定义或替换您正在使用的 Word 常量。例如,而不是:

    .Rows.Alignment = wdAlignRowCenter
    .Rows.HeightRule = wdRowHeightExactly
    

    你可以使用:

    .Rows.Alignment = 1  '1=wdAlignRowCenter
    .Rows.HeightRule = 2 '2=wdRowHeightExactly
    

    或者,之后:

    Dim MSWordApp As Object, MSWordDoc As Object
    

    你会插入:

    Const wdAlignRowCenter as Long = 1: Const wdRowHeightExactly as Long = 2
    

    否则,如果您要设置对 Word 的引用,则应使您的代码始终与早期绑定保持一致。例如,而不是:

    Dim MSWordApp As Object, MSWordDoc As Object
    
        Set MSWordApp = CreateObject("Word.Application")
        Set MSWordDoc = MSWordApp.Documents.Add
    

    你可能会使用:

    Dim MSWordApp As New Word.Application, MSWordDoc As Word.Document
    
        Set MSWordDoc = MSWordApp.Documents.Add
    

    【讨论】:

    • 感谢您的建议!我在发布帖子后意识到您所说的,进行了许多更改,并且我正在处理的代码现在与 OP 完全不同:P
    • 顺便说一句,将 MSWordDoc 声明为 Word.Document 然后再添加一行以添加新文档和将 MSWordDoc 声明为新 Word.Document 之间有什么区别,因此不需要另一行?对我来说,它们的工作方式类似,我不确定哪个更可取。
    • 我发布的代码没有将 MSWordDoc 声明为新 Word.Document - 它将 MSWordApp 声明为新 Word.Application。两者完全不同。
    • 是的,我知道。我想说的是,在我正在处理的文件中,我只是将 MSWordDoc 声明为新 Word.Document 并且不添加“Set...MSWordApp.Documents.Add”行。虽然我不确定我的代码和您的示例之间是否有任何区别,但它工作正常。
    • OP 中发布的代码已经修改了很多 :P 所以我在之前的 cmets 中问的只是关于 1) 将 MSWordDoc 声明为 Word.Document 然后再添加一行添加新文档,以及; 2) 将 MSWordDoc 声明为新 Word.Document 并且不要添加“Set...MSWordApp.Documents.Add”行。
    猜你喜欢
    • 1970-01-01
    • 2021-07-24
    • 2023-02-08
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多