【问题标题】:adding text, table and page number to footer in excel vba在 excel vba 中将文本、表格和页码添加到页脚
【发布时间】:2019-05-30 18:39:06
【问题描述】:

我一直在尝试为页眉和页脚创建一个宏。 标题没有问题,我可以创建它。 问题在于页脚,我必须创建一个 2x1 表格(按照代码中的尺寸)以及 2 行文本和页码(格式为 xx 的第 1 页)。

我被卡住了,因为当我运行代码时,表格或文本都会弹出。 如何修改代码以便我可以同时拥有表格和文本以及页码。

谢谢!

Sub CreateWord()

Dim objWord As Object
Dim objdoc As Object
Dim objrange As Word.Range
Dim myTable As Table
Dim i As Long

Set objWord = CreateObject("Word.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 = wdToggle
       objrange.ParagraphFormat.Alignment = wdAlignParagraphCenter
       Set objrange = Nothing

       Set objrange = .Footers(wdHeaderFooterPrimary).Range
       objrange = "text1" & vbNewLine & "text2" & vbNewLine & " " & vbNewLine & " "
       .Footers(wdHeaderFooterPrimary).PageNumbers.Add FirstPage:=True
       objrange.Font.Name = "Arial"
       objrange.Font.Size = 9
       objrange.Font.Bold = wdToggle
       objrange.ParagraphFormat.Alignment = wdAlignParagraphLeft
       Set objrange = Nothing

       .Footers(wdHeaderFooterPrimary).PageNumbers.Add FirstPage:=True

   End With

     With objdoc
         Set myTable = .Tables.Add(.Sections(1).Footers(wdHeaderFooterPrimary).Range, 2, 1)
     End With

         With myTable

             .Cell(1, 1).Range.Text = "Employee"
             .Cell(2, 1).Range.Text = " " & vbNewLine & " "
             .Rows.SetLeftIndent LeftIndent:=395, RulerStyle:=wdAdjustFirstColumn
             .Borders.InsideLineStyle = wdLineStyleSingle
             .Borders.OutsideLineStyle = wdLineStyleSingle
         End With
Next

结束子

【问题讨论】:

  • 不清楚应该如何在页脚中布置内容。表格、文本和页码应该在哪里相互关联?如果您提供了所需结果的屏幕截图,或许可以?

标签: excel vba ms-word


【解决方案1】:

Word 比 Excel 复杂。你陷入了这样一种假设,即 Footer.Range 将与其文本和段落相同。事实上,页脚包含几个具有单独范围的段落,每个段落都有自己的文本,这确实是默认属性但并不相同,就像单元格的值与 Excel 中的单元格不同一样。 我已经在 Word 中测试了下面的代码。我认为它会在重新定义 objWord 对象后在 Excel 中运行。祝你好运!

Sub CreateWord()
    ' 04 Jan 2019

    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              'wdToggle
            objRange.ParagraphFormat.Alignment = wdAlignParagraphCenter
            Set objRange = Nothing

            ' you are setting only one header
            ' the code below sets all footers
            For f = wdHeaderFooterPrimary To wdHeaderFooterFirstPage
                Set objRange = .Footers(f).Range
                With objRange
                    .ParagraphFormat.Alignment = wdAlignParagraphLeft
                    With .Font
                        .Name = "Arial"
                        .Size = 9
                        .Bold = vbTrue                  'wdToggle
                    End With
                    .Text = "text1" & Chr(11) & _
                            "text2" & Chr(9) & "Page "
                    .Collapse wdCollapseEnd
                    .Fields.Add Range:=objRange, _
                                Type:=wdFieldEmpty, _
                                Text:="PAGE  \* Arabic ", _
                                PreserveFormatting:=True
                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

                ' vbNewLine = Chr(13) = hard return = new paragraph
                ' Chr(11) = soft return = new line
                With myTable
                    .Cell(1, 1).Range.Text = "Employee"
                    .Cell(2, 1).Range.Text = " " & Chr(11) & " "
                    ' you may want to set the left margin of the paragraph
                    ' rather than indenting the table:-
                    .Rows.SetLeftIndent LeftIndent:=39.5, RulerStyle:=wdAdjustFirstColumn
                    .Borders.InsideLineStyle = wdLineStyleSingle
                    .Borders.OutsideLineStyle = wdLineStyleSingle
                End With
            Next f
        End With
    Next i
End Sub

【讨论】:

  • 感谢您的帮助!通过这种做法,我对 VBA 有了更好的理解。 3 周前刚学会这个,所以我是新手。
  • 如果我的回答解决了您的问题,请将其标记为“已选择”。
猜你喜欢
  • 2019-01-07
  • 1970-01-01
  • 2020-12-24
  • 2018-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多