Run-time error 4198, command failed, when trying to set ms word doc to print view

醉酒当歌 提交于 2020-06-28 04:45:40

问题


I am trying to set the window view to printView.

I've used the "record macro" in word, to see how word suggests I set something to print view. Here's the code:

If ActiveWindow.View.SplitSpecial = wdPaneNone Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
Else
    ActiveWindow.View.Type = wdPrintView
End If

Each time, the execution stops and gives me the above error. The debug points out:

ActiveWindow.View.Type = wdPrintView

as the buggy line. I've also tried:

If ActiveWindow.View.SplitSpecial = wdPaneNone Then
    ActiveDocument.ActiveWindow.View.Type = wdPrintView
Else
    ActiveWindow.View.SplitSpecial = wdPaneNone
    ActiveWindow.View.Type = wdPrintView
End If

The issue seems to happen when the splitspecial is 4 (wdPanePrimaryFooter). But changing the conditional to account for that doesn't seem to work. If I comment out the view type line, everything goes fine.

Any ideas?

Thank you in advance.

Edit, here is the entire block, but I cannot replicate this error half the time:

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

回答1:


The kind of code in the question is the result of using the macro recorder. This tool is really great, but because it only mimics the user actions, the code it creates is sometimes not optimal. Working with headers and footers, especially, can make life more complicated than it ought to be... When code selects a header/footer range that triggers the display of the old Word 2.0 "panes" that were necessary for editing these. Word 6.0 introduced WYSIWYG and the panes were "retired" and only show up in this context.

When working with headers and footers a Range object is usually preferable than using Selection. You can think of a Range as a invisible selection, with the advantages: 1. It doesn't move the actual selection. 2. There can be as many Range objects as required for the task, while there can be only one selection.

The following code sample gets the Footer range and adds content to it. Since it never changes the selection, the screen is quieter and the pane never shows up (and the code is faster).

Working with ranges is relatively straight-forward, until field codes come into play. Then it takes a bit of work to get the "target" point for new material to follow a field.

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


来源:https://stackoverflow.com/questions/57376722/run-time-error-4198-command-failed-when-trying-to-set-ms-word-doc-to-print-vie

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!