Microsoft.Office.Interop.Word.Fields.Add error invalid command

We add a domain and then use Office recorded macro code:

Sub 宏1()
'
' 宏1 宏
'
'
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "PAGE  \* Arabic ", PreserveFormatting:=True
        
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "NUMPAGES  \# ""/0"" \* Arabic ", PreserveFormatting:=True
End Sub

Then when we operate Office also add domain by corresponding Api:

  var pageSelection = centerRange.Application.Selection;
  var allPageSelection = centerRange.Application.Selection;
  centerRange.Fields.Add(pageSelection.Range, WdFieldType.wdFieldEmpty, @"PAGE  \* Arabic", true);
  centerRange.Fields.Add(allPageSelection.Range, WdFieldType.wdFieldEmpty, @"NUMPAGES  \# ""/0"" \* Arabic ", true);

Office can generate normal debugging locally, but on the server when the incorrect reportThis command is invalid
Then by querying the last code changed so

  object oMission = Missing.Value;
  Object currentPageFiled = WdFieldType.wdFieldPage;
   var selection = centerRange.Document.ActiveWindow.Selection;
  Object totalPageFiled = WdFieldType.wdFieldNumPages;
  centerRange.Document.ActiveWindow.Selection.TypeText(string.Empty);
 centerRange.Fields.Add(selection.Range, ref currentPageFiled, ref oMission, ref oMission);
 centerRange.Document.ActiveWindow.Selection.TypeText("/");
centerRange.Fields.Add(selection.Range, ref totalPageFiled, ref oMission, ref oMission);

Text parameters then the server will run properly, the cause of the error occurs, it should be is not identify Add () method of deliveryNUMPAGES # “”/0"" * Arabic , That is, this field codes.

Published 13 original articles · won praise 2 · Views 472

Guess you like

Origin blog.csdn.net/qq_41809137/article/details/104061052