从Excel更改单词表格式

发布于 2025-02-05 21:04:19 字数 525 浏览 3 评论 0原文

我在Excel中有一个宏,可以创建一个单词,其中一些Excel表被复制,并且我有此代码用于更改格式:

    Set WordTable = myDoc.Tables(i)
    With WordTable
        .AutoFitBehavior (wdAutoFitWindow)
        .Shading.Texture = wdTextureNone
        .Shading.BackgroundPatternColor = wdColorWhite
        .Range.Font.TextColor = wdColorBlack
        .Range.ParagraphFormat.SpaceAfter = 0
    End With

一切正常工作,除非未设置参考“ Microsoft Word Object”。在这种情况下,由于某种原因,阴影变成黑色。除了设置此参考之外,还有什么方法可以解决它?

问题在于,该宏是用户在自己的PC中安装的较大Excel程序的一部分,因此无法与VBA一起使用。

I have a macro in Excel that creates a Word where some Excel tables are copied, and I have this code for format changing:

    Set WordTable = myDoc.Tables(i)
    With WordTable
        .AutoFitBehavior (wdAutoFitWindow)
        .Shading.Texture = wdTextureNone
        .Shading.BackgroundPatternColor = wdColorWhite
        .Range.Font.TextColor = wdColorBlack
        .Range.ParagraphFormat.SpaceAfter = 0
    End With

Everything works properly except when the reference "Microsoft Word Object Library" is not set. In this case, the shading turns black for some reason. Is there any way to solve it, apart from set this reference?

The problem is that this macro is part of a bigger Excel program that the user installs in their own PCs, so shouldn't be able to work with VBA.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事风中埋 2025-02-12 21:04:19

问题是,如果您在Excel和中使用该代码,则未设置的“ Microsoft Word Object库”,Excel不知道该词常数wdautofitwindowwdtexturenone < /code>,wdcolorwhitewdcolorblack。取而代之的是,Excel将将它们视为变量,并且由于您没有初始化它们,每个变量都具有值0

确保使用optict equipit,以便如果使用未定义的内容,则会获得通知!

我建议始终激活epote equallicit
在VBA编辑器中,请转到 tools 选项需要变量声明

要解决问题,您需要用它们的实际值替换它们(您可以在此处找到枚举的值:枚举(word))或将它们定义为Excel中的常数。

Option Explicit

Public Sub Example
    Const wdAutoFitWindow As Long = 1
    Const wdTextureNone As Long = 0
    Const wdColorWhite As Long = 16777215
    Const wdColorBlack As Long = 0

    Set WordTable = myDoc.Tables(i)
    With WordTable
        .AutoFitBehavior wdAutoFitWindow
        .Shading.Texture = wdTextureNone
        .Shading.BackgroundPatternColor = wdColorWhite
        .Range.Font.TextColor = wdColorBlack
        .Range.ParagraphFormat.SpaceAfter = 0
    End With
End Sub

,如果您计划在多个过程/功能中使用它们,也可以在过程范围之外定义它们。确保在每个模块顶部使用epote epote确保正确声明所有变量,否则您将迅速遇到问题。

The issue is, if you use that code in Excel and "Microsoft Word Object Library" is not set, Excel does not know the Word constants wdAutoFitWindow, wdTextureNone, wdColorWhite and wdColorBlack. Instead Excel will treat them as variables and since you did not initialize them each of it has the value 0.

Make sure you use Option Explicit so you get notified if you use something that is not defined!

I recommend always to activate Option Explicit:
In the VBA editor go to ToolsOptionsRequire Variable Declaration.

To solve the issue, you need to either replace them with their actual value (you can find the values for the enumerations here: Enumerations (Word)) or define them as constants in Excel.

Option Explicit

Public Sub Example
    Const wdAutoFitWindow As Long = 1
    Const wdTextureNone As Long = 0
    Const wdColorWhite As Long = 16777215
    Const wdColorBlack As Long = 0

    Set WordTable = myDoc.Tables(i)
    With WordTable
        .AutoFitBehavior wdAutoFitWindow
        .Shading.Texture = wdTextureNone
        .Shading.BackgroundPatternColor = wdColorWhite
        .Range.Font.TextColor = wdColorBlack
        .Range.ParagraphFormat.SpaceAfter = 0
    End With
End Sub

You can also define them outside the scope of a procedure if you plan to use them in multiple procedures/functions. Make sure to use Option Explicit on top of every module to ensure all variables are declared properly or you will quickly run into issues again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文