先打开一个WORD,加入页脚页码,关闭它。再新建一个WORD,却显示“远程服务器不存在或不能使用”,不知原因。我想做的是在二个WORD都加上页码。代码如下:Private Sub Command1_Click()
Dim Ap As Word.Application
Dim newDoc As Document
Set Ap = CreateObject("word.application")
Ap.Visible = True
Set newDoc = Ap.Documents.Add'添加页码
Windows(newDoc).Activate
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then ActiveWindow.Panes(2).Close
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then ActiveWindow.ActivePane.View.Type = wdPrintView
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
  Else
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Font.Size = 9
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocumentnewDoc.SaveAs App.Path & "\AAAAAA.doc"
newDoc.Close
Ap.Quit
Set Ap = Nothing '消灭对象变量
Set newDoc = NothingDim objWDApp As New Word.Application
Dim objDoc As New Word.Document
Dim strTitle As String
Set objDoc = objWDApp.Documents.Add'添加页码
Windows(objDoc).Activate
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then ActiveWindow.Panes(2).Close
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then ActiveWindow.ActivePane.View.Type = wdPrintView
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
  Else
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
End If
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldPage
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Font.Size = 9
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocumentobjDoc.SaveAs App.Path & "\BBBBBB.doc"
objDoc.Close
objWDApp.Quit
Set objWDApp = Nothing '消灭对象变量
Set objDoc = Nothing
End Sub

解决方案 »

  1.   

    If Ap.Selection.Information(wdNumberOfPagesInDocument) > 1 Then 'WORD页面大于1时,才加入页码
      If Ap.ActiveWindow.View.SplitSpecial <> wdPaneNone Then Ap.ActiveWindow.Panes(2).Close
      If Ap.ActiveWindow.ActivePane.View.Type = wdNormalView Or Ap.ActiveWindow.ActivePane.View.Type = wdOutlineView Then Ap.ActiveWindow.ActivePane.View.Type = wdPrintView
      Ap.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
      If Ap.Selection.HeaderFooter.IsHeader = True Then
          Ap.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
        Else
          Ap.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
      End If
      Ap.Selection.EndKey Unit:=wdLine, Extend:=wdExtend
      Ap.Selection.Fields.Add Range:=Ap.Selection.Range, Type:=wdFieldPage
      Ap.Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
      Ap.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
      Ap.Selection.Font.Size = 11
      Ap.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
    End If