Vba 尝试将ms word文档设置为打印视图时出现运行时错误4198,命令失败

Vba 尝试将ms word文档设置为打印视图时出现运行时错误4198,命令失败,vba,macos,ms-word,Vba,Macos,Ms Word,我正在尝试将窗口视图设置为printView 我在word中使用了“录制宏”,以查看word如何建议我将某些内容设置为“打印视图”。代码如下: If ActiveWindow.View.SplitSpecial = wdPaneNone Then ActiveWindow.ActivePane.View.Type = wdPrintView Else ActiveWindow.View.Type = wdPrintView End If 每次执行都会停止并给出上述错误。调试指出

我正在尝试将窗口视图设置为printView

我在word中使用了“录制宏”,以查看word如何建议我将某些内容设置为“打印视图”。代码如下:

If ActiveWindow.View.SplitSpecial = wdPaneNone Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
Else
    ActiveWindow.View.Type = wdPrintView
End If
每次执行都会停止并给出上述错误。调试指出:

ActiveWindow.View.Type = wdPrintView
就像马车一样。我也试过:

If ActiveWindow.View.SplitSpecial = wdPaneNone Then
    ActiveDocument.ActiveWindow.View.Type = wdPrintView
Else
    ActiveWindow.View.SplitSpecial = wdPaneNone
    ActiveWindow.View.Type = wdPrintView
End If
当splitspecial为4(wdPanePrimaryFooter)时,问题似乎会发生。但是改变条件来解释这一点似乎不起作用。如果我注释掉视图类型行,则一切正常

有什么想法吗

先谢谢你

编辑,这是整个块,但我无法一半时间复制此错误:

Sub pageNumber()
    ActiveDocument.Sections(ActiveDocument.Sections.Count) _
        .Footers(wdHeaderFooterPrimary).Range.Select
    With Selection
        .ParagraphFormat.Alignment = wdAlignParagraphCenter
        .TypeText Text:="Page "
        .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "PAGE ", PreserveFormatting:=True
        .TypeText Text:=" of "
        .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "NUMPAGES ", PreserveFormatting:=True
        .Collapse
    End With
    ActiveDocument.Content.Select
    Selection.Collapse wdCollapseStart
    If ActiveWindow.View.SplitSpecial = wdPaneNone Then
        ActiveDocument.ActiveWindow.View.Type = wdPrintView
    Else
        ActiveWindow.View.SplitSpecial = wdPaneNone
        ActiveWindow.View.Type = wdPrintView
    End If
End Sub

问题中的代码类型是使用宏记录器的结果。这个工具确实很棒,但是因为它只模仿用户的动作,所以它创建的代码有时不是最优的。尤其是使用页眉和页脚,会使生活变得更加复杂。。。当代码选择页眉/页脚范围时,会触发旧Word 2.0“窗格”的显示,而旧Word 2.0“窗格”是编辑这些内容所必需的。Word 6.0引入了WYSIWYG,窗格已“退役”,仅在此上下文中出现

使用页眉和页脚时,通常首选
范围
对象,而不是使用
选择
。您可以将
范围
视为一个不可见的选择,其优点是:1。它不会移动实际选择。2.任务所需的
Range
对象可以有多个,但只能有一个选择

下面的代码示例获取页脚范围并向其中添加内容。因为它从不改变选择,所以屏幕更安静,窗格也不会显示(代码也更快)

在字段代码发挥作用之前,使用范围相对简单。然后,需要做一些工作才能获得新材料跟随某个字段的“目标”点

Sub pageNumber()
    Dim rngFooter As Word.Range
    Dim fld As Word.Field

    Set rngFooter = ActiveDocument.Sections(ActiveDocument.Sections.Count) _
        .Footers(wdHeaderFooterPrimary).Range
    With rngFooter
        .ParagraphFormat.Alignment = wdAlignParagraphCenter
        .Text = "Page "
        .Collapse wdCollapseEnd
        Set fld = .Fields.Add(Range:=rngFooter, Type:=wdFieldEmpty, Text:= _
            "PAGE ", PreserveFormatting:=False)
    End With
    Set rngFooter = fld.result
    With rngFooter
        'Move the end of the range outside the field
        .MoveStart wdCharacter, 1
        .InsertAfter " of "
        .Collapse wdCollapseEnd
        .Fields.Add Range:=rngFooter, Type:=wdFieldEmpty, Text:= _
            "NUMPAGES ", PreserveFormatting:=False
    End With
End Sub

问题中的代码类型是使用宏记录器的结果。这个工具确实很棒,但是因为它只模仿用户的动作,所以它创建的代码有时不是最优的。尤其是使用页眉和页脚,会使生活变得更加复杂。。。当代码选择页眉/页脚范围时,会触发旧Word 2.0“窗格”的显示,而旧Word 2.0“窗格”是编辑这些内容所必需的。Word 6.0引入了WYSIWYG,窗格已“退役”,仅在此上下文中出现

使用页眉和页脚时,通常首选
范围
对象,而不是使用
选择
。您可以将
范围
视为一个不可见的选择,其优点是:1。它不会移动实际选择。2.任务所需的
Range
对象可以有多个,但只能有一个选择

下面的代码示例获取页脚范围并向其中添加内容。因为它从不改变选择,所以屏幕更安静,窗格也不会显示(代码也更快)

在字段代码发挥作用之前,使用范围相对简单。然后,需要做一些工作才能获得新材料跟随某个字段的“目标”点

Sub pageNumber()
    Dim rngFooter As Word.Range
    Dim fld As Word.Field

    Set rngFooter = ActiveDocument.Sections(ActiveDocument.Sections.Count) _
        .Footers(wdHeaderFooterPrimary).Range
    With rngFooter
        .ParagraphFormat.Alignment = wdAlignParagraphCenter
        .Text = "Page "
        .Collapse wdCollapseEnd
        Set fld = .Fields.Add(Range:=rngFooter, Type:=wdFieldEmpty, Text:= _
            "PAGE ", PreserveFormatting:=False)
    End With
    Set rngFooter = fld.result
    With rngFooter
        'Move the end of the range outside the field
        .MoveStart wdCharacter, 1
        .InsertAfter " of "
        .Collapse wdCollapseEnd
        .Fields.Add Range:=rngFooter, Type:=wdFieldEmpty, Text:= _
            "NUMPAGES ", PreserveFormatting:=False
    End With
End Sub

谢谢这就做到了,我感谢你花时间解释范围与选择。非常有用的信息。谢谢。这就做到了,我感谢你花时间解释范围与选择。非常有用的信息。