从Excel更改单词表格式
我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,如果您在Excel和中使用该代码,则未设置的“ Microsoft Word Object库”,Excel不知道该词常数
wdautofitwindow
,wdtexturenone < /code>,
wdcolorwhite
和wdcolorblack
。取而代之的是,Excel将将它们视为变量,并且由于您没有初始化它们,每个变量都具有值0
。确保使用
optict equipit
,以便如果使用未定义的内容,则会获得通知!要解决问题,您需要用它们的实际值替换它们(您可以在此处找到枚举的值:枚举(word))或将它们定义为Excel中的常数。
,如果您计划在多个过程/功能中使用它们,也可以在过程范围之外定义它们。确保在每个模块顶部使用
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
andwdColorBlack
. Instead Excel will treat them as variables and since you did not initialize them each of it has the value0
.Make sure you use
Option Explicit
so you get notified if you use something that is not defined!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.
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.