在数字之间添加文本

时间:2019-01-21 09:09:14

标签: excel vba

我正在尝试使用VBA添加页脚。

我希望页脚看起来像这样:“第2页,共11页”。
我尝试对其进行编码,但是它一直给我“ Page 211”

是否可以在不损害当前代码的情况下添加“”?

谢谢!

Sub ExtractContractA1()
    Dim objWord As Object
    Dim objDoc As Object
    Dim objRange As Word.Range
    Dim myTable As Table
    Dim i As Long
    Dim f As Long

    Set objWord = CreateObject("Word.Application")
    'Set objWord = Application
    objWord.Visible = True
    Set objDoc = objWord.Documents.Add()
    objDoc.PageSetup.OddAndEvenPagesHeaderFooter = False

    For i = 1 To objDoc.Sections.Count
        With objDoc.Sections(i)    
            Set objRange = .Headers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range    
            objRange = "PRIVATE AND CONFIDENTIAL"    
            objRange.Font.Name = "Arial"    
            objRange.Font.Size = 11    
            objRange.Font.Bold = vbTrue    
            objRange.ParagraphFormat.Alignment = wdAlignParagraphCenter   
            Set objRange = Nothing

            For f = wdHeaderFooterPrimary To wdHeaderFooterFirstPage    
                Set objRange = .Footers(f).Range
                With objRange    
                    .ParagraphFormat.Alignment = wdAlignParagraphRight

                    With .Font   
                        .Name = "Arial" 
                        .Size = 9
                        .Bold = vbFalse
                    End With

                    .Text = "Contract"   
                    .Collapse wdCollapseEnd
                End With

                Set objRange = .Footers(f).Range.Paragraphs(1).Range
                With objRange
                    .Paragraphs.Add
                    .Collapse wdCollapseEnd
                    Set myTable = .Tables.Add(objRange, 2, 1)
                End With
                With myTable
                    .Cell(1, 1).Range.Text = "Employee"
                    .Cell(2, 1).Range.Text = " " & Chr(11) & " "
                    .Rows.SetLeftIndent LeftIndent:=395, RulerStyle:=wdAdjustFirstColumn
                    .Borders.InsideLineStyle = wdLineStyleSingle
                    .Borders.OutsideLineStyle = wdLineStyleSingle
                End With

                VersionText = ActiveSheet.Range("$G$2")

                Set objRange = .Footers(f).Range.Paragraphs(6).Range
                With objRange
                    .Paragraphs.Add
                    .ParagraphFormat.Alignment = wdAlignParagraphRight

                    With .Font
                        .Name = "Arial"
                        .Size = 9
                        .Bold = vbFalse
                    End With

                   .Text = VersionText & Chr(11) & Chr(11) & "Page "
                   .Collapse wdCollapseEnd
                   .Fields.Add Range:=objRange, _
                               Type:=wdFieldEmpty, _
                               Text:="NUMPAGES \of", _
                               PreserveFormatting:=True
                   .Text = " of "
                   .Fields.Add Range:=objRange, _
                               Type:=wdFieldEmpty, _
                               Text:="PAGE  \* Arabic ", _
                               PreserveFormatting:=True
                End With
            Next f
        End With
    Next i
End sub

1 个答案:

答案 0 :(得分:0)

NUMPAGES \of中,“ \ of”不是有效的开关。言语似乎无视它。您想要执行的操作类似于{NUMPAGES * \ Arabic}的{PAGE * \ Arabic},其中“ of”位于两个字段之间。

下面的代码将大致实现上述想法。请对其进行修改以用于您的项目。

Sub AddFields()

    Dim Rng As Range

    Set Rng = Selection.Range
    Rng.Collapse wdCollapseEnd
    ActiveDocument.Fields.Add Range:=Rng, _
                              Type:=wdFieldEmpty, _
                              Text:="PAGE  \* Arabic ", _
                              PreserveFormatting:=True
    Set Rng = Rng.Paragraphs(1).Range
    Rng.Collapse wdCollapseEnd
    Rng.InsertAfter " of "
    Rng.Collapse wdCollapseEnd
    ActiveDocument.Fields.Add Range:=Rng, _
                              Type:=wdFieldEmpty, _
                              Text:="NUMPAGES  \* Arabic ", _
                              PreserveFormatting:=True
End Sub