通过VBS在Word中设置字体大小仅适用于某些文档

时间:2015-08-06 16:04:16

标签: vba vbscript ms-word formatting ms-office

我正在尝试通过VBScript以编程方式修改一组Word标头。

我采取的步骤如下(在伪代码中):

For each word-document in source-folder
    Open word-document
    Clear current headers 
    Clear current tabstops
    Set font to 9
    Insert prepared string
    Close word-document
End For

我遇到的问题是,一小组但一致的文档组已将所有步骤应用于,除了字体更改。

我用来执行字体更改的代码如下:

Sub setFont
    ' Select the header view
    WordApp.ActiveWindow.ActivePane.View.SeekView = 9 
    ' Set the font to 9
    WordApp.ActiveWindow.Selection.Font.Size = "9"
    ' Go back the the main body view
    WordApp.ActiveWindow.ActivePane.View.SeekView = 0 
End Sub

据我所知,脚本失败的批量文档没有任何独特之处。该文档集包含各种字体大小和制表位,并且该脚本适用于该集合中的70多个其他文档。

如果有人可以帮我确定为什么该代码不适用于某些文档,我将非常感激。

1 个答案:

答案 0 :(得分:0)

您可能需要设置let bit = sizeof(Int) == sizeof(Int64) ? 64 : 32 ,以便它包含整个标头。这是您添加了代码行的代码。警告:未经测试!

Selection

您可以在此代码中使用Sub setFont() ' Select the header view WordApp.ActiveWindow.ActivePane.View.SeekView = 9 ' Make the selection the entire header WordApp.ActiveWindow.Selection.WholeStory ' Set the font to 9 WordApp.ActiveWindow.Selection.Font.Size = "9" ' Go back the the main body view WordApp.ActiveWindow.ActivePane.View.SeekView = 0 End Sub 块:

With ... End With