如何插入包含页码、文件路径和图像的页脚?

2024-03-20

enter image description hereI'm trying to format the footer so it has the page # (x out of y) on the top right of the footer, and then the image centered below. I ended up writing an algorithm for the page # and then used inlineshapes to insert the image above. The problem is the text is below the image and the image is not centered. Any help would be appreciated.

.ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).range.Paragraphs.Alignment = wdAlignParagraphCenter 'Centers Header'
.ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).range.InlineShapes.AddPicture ("X:\EQP\Residential Maintenance Agreement\Archived RMA templates\AA Logo Swoops cropped 2.JPG") 'Calls for image header'
.ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).range.Paragraphs.Alignment = wdAlignParagraphCenter 'Centers Footer'
.ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).range.InlineShapes.AddPicture ("X:\EQP\Residential Maintenance Agreement\Footer Template.PNG")
With wdapp.ActiveDocument.Sections(1).Footers(1).range.Paragraphs(1)
    .range.InsertAfter vbCr & "Page "
    Set r = .range
    E = .range.End
    r.Start = E
    .range.Fields.Add r, wdFieldPage
    .range.InsertAfter " of "
    E = .range.End
    r.Start = E
    .range.Fields.Add r, wdFieldNumPages
    .Alignment = wdAlignParagraphRight
    '.Alignment = wdAlignParagraphCenter
    '.range.InlineShapes.AddPicture ("X:\EQP\Residential Maintenance Agreement\Footer Template.PNG")
End With

我已经解决了一些事情。它比我想象的要大。我确信它可以帮助您开始实现您想要达到的目标。

Experts-exchange.com 的解决方案提供了一些帮助 ”VBA 在 Word 页脚中插入修改后的第 x 页(共 y 页) https://www.experts-exchange.com/questions/23467589/VBA-to-insert-a-modified-Page-x-of-y-in-a-Word-Footer.html#discussion“。我在代码中提到过它,我用它来将测试转换为字段。

正如您在其他问题中提到的“如何在不影响页脚/页眉的情况下启用页码 https://stackoverflow.com/a/45353009/1306012“我遵循使用带有空边框的表格的方法。它们允许您非常精确地放置内容。 这就是为什么下面的代码将插入一个包含三列的表格:

 ___________________ ________________________ ___________
|_Your footer text__|_Center part if needed__|_Page X/Y__|

下面找到代码。主要方法InsertFooter你会想从你的代码中调用。它会做你想做的事:

Sub InsertFooter()

Dim footer As HeaderFooter
Dim footerRange As range
Dim documentSection As Section
Dim currentView As View
Dim footerTable As table
Dim pictureShape As Shape

On Error GoTo MyExit

' Disable updating to prevent flickering
Application.ScreenUpdating = False

For Each documentSection In ActiveDocument.Sections
    For Each footer In documentSection.Footers
        If footer.Index = wdHeaderFooterPrimary Then
            Set footerRange = footer.range
            ' add table to footer
            Set footerTable = AddTableToFooter(footerRange)
            ' Make table border transparent
            SetTableTransparentBorder footerTable
            ' Insert page X out of Y into third column in table
            InsertPageNumbersIntoTable footerTable
            ' Insert file path
            InsertFilePathIntoTable footerTable
            ' Add picture to footer
            AddPictureToFooter footerRange, "C:\Pictures\happy.jpg", 3
        End If
    Next footer
Next documentSection

MyExit:
' Enable updating again
Application.ScreenUpdating = True
Application.ScreenRefresh

End Sub

Sub AddPictureToFooter(range As range, filePath As String, pictureHeightInCm As Single)
    Set pictureShape = range.InlineShapes.AddPicture(FileName:=filePath, LinkToFile:=False, SaveWithDocument:=True).ConvertToShape
    pictureShape.WrapFormat.Type = wdWrapFront
    pictureShape.height = CentimetersToPoints(pictureHeightInCm)
    pictureShape.Top = 0
End Sub

Sub InsertPageNumbersIntoTable(tableToChange As table)
    ' Attention no error handling done!

    ' inserts "Page {page} of {pages}" into the third column of a table
    Dim cellRange As range
    Set cellRange = tableToChange.Cell(1, 3).range
    cellRange.InsertAfter "Page { PAGE } of { NUMPAGES }"
    TextToFields cellRange
End Sub


' Credits go to
' https://www.experts-exchange.com/questions/23467589/VBA-to-insert-a-modified-Page-x-of-y-in-a-Word-Footer.html#discussion
Sub TextToFields(rng1 As range)
    Dim c As range
    Dim fld As Field
    Dim f As Integer
    Dim rng2 As range
    Dim lFldStarts() As Long

    Set rng2 = rng1.Duplicate
    rng1.Document.ActiveWindow.View.ShowFieldCodes = True

    For Each c In rng1.Characters
        DoEvents
        Select Case c.Text
            Case "{"
                ReDim Preserve lFldStarts(f)
                lFldStarts(f) = c.Start
                f = f + 1
            Case "}"
                f = f - 1
                If f = 0 Then
                    rng2.Start = lFldStarts(f)
                    rng2.End = c.End
                    rng2.Characters.Last.Delete '{
                    rng2.Characters.First.Delete '}
                    Set fld = rng2.Fields.Add(rng2, , , False)
                    Set rng2 = fld.Code
                    TextToFields fld.Code
                End If
            Case Else
        End Select
    Next c
    rng2.Expand wdStory
    rng2.Fields.Update
    rng1.Document.ActiveWindow.View.ShowFieldCodes = False
End Sub

Sub InsertFilePathIntoTable(tableToChange As table)
    ' Attention no error handling done!

    ' inserts "Page {page} of {pages}" into the third column of a table
    Dim cellRange As range
    Set cellRange = tableToChange.Cell(1, 1).range
    cellRange.InsertAfter "{ FILENAME \p }"
    TextToFields cellRange
End Sub

Sub SetTableTransparentBorder(tableToChange As table)
    tableToChange.Borders(wdBorderTop).LineStyle = wdLineStyleNone
    tableToChange.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
    tableToChange.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
    tableToChange.Borders(wdBorderRight).LineStyle = wdLineStyleNone
    tableToChange.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
    tableToChange.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
    tableToChange.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
End Sub

Function AddTableToFooter(footerRange As range) As table
    Dim footerTable As table
    Set footerTable = ActiveDocument.Tables.Add(range:=footerRange, NumRows:=1, NumColumns:=3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed)
    ' Algin third column to right
    footerTable.Cell(1, 3).range.ParagraphFormat.Alignment = wdAlignParagraphRight
    Set AddTableToFooter = footerTable
End Function
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何插入包含页码、文件路径和图像的页脚? 的相关文章

  • CSS - 将文本保留在图像下方

    我正在尝试创建一个简单的图片库 有人告诉我使用 float left 但是当我这样做时 页脚中的所有文本都会射到第一张图像 我已经搜索了大约一个小时试图找到解决方案 但我找不到任何东西 我尝试过使用边距 边框 不同的对齐方式和各种不同的小东
  • 如何在ListView中添加页脚?

    我正在开发一个应用程序 在我的应用程序中 我使用 Listview 使用 dom 解析显示数据 我想在列表视图中添加页脚 当我单击页脚时将更多数据添加到列表视图中 我附加了图像 我想要该设计和流程 请参考image1和imgae2 我在红色
  • VBA:删除数组项后减少循环迭代?

    在 Excel 的 VBA 中 For i 0 To UBound artMaster For j i To UBound artMaster If i lt gt j And artMaster i VDN artMaster j VDN
  • Android 简单 TextView 动画

    我有一个 TextView 我想倒计时 3 2 1 发生了事情 为了使其更有趣 我希望每个数字都以完全不透明开始 然后淡出至透明 有没有一种简单的方法可以做到这一点 尝试这样的事情 private void countDown final
  • 在 VBA 循环中导出查询以根据字符串值选择数据

    我有一个名为 TEST 的表 下面的代码根据 Territory 列中的唯一值循环导出查询 该代码应该根据 Territory 列中的唯一值将数据导出到 Excel 文件 因此每个 Territory 值都有它自己的文件 我在设置 sql
  • 调整回形针大小以适合矩形框

    我有一个矩形图像 例如 30x800 像素 如何用回形针缩放它以保留 100x100 像素图像的纵横比 并用边框填充空白区域 一个例子 http www imagemagick org Usage thumbnails pad extent
  • 使用 TCPDF PHP 库横向显示的图像

    我正在使用 TCPDF PHP 库生成包含照片的 PDF 文档 由于某种原因 某些照片在我的计算机和网络上正确显示 但当我将该图像放入 PDF 中时 它似乎是横向的 这只发生在某些图像上 大多数图像显示正确 下面是在 PDF 中横向显示的示
  • Linux 文本文件操作

    我有一个格式的文件 a href a href a href a href 我需要选择 之后但 之前的文本 并将其打印在行尾 添加后 例如 a href http www wowhead com search Su a a a a a
  • 在 Android 版 Glide 中离线时加载已获取的图像

    我正在使用 Glide 版本 4 8 0 为了加载图像我这样做 GlideApp with HomePageFragment this load remoteURL diskCacheStrategy DiskCacheStrategy A
  • 自定义函数错误:“表达式不能在计算列中使用”

    在 Access 2010 中 我尝试在计算列中使用自定义 VBA 函数 我得到 表达式不能在计算列中使用 这是我的步骤 启动 Access 2010 创建一个新的数据库 DB 创建一个包含文本列 Column1 的表 Table1 在 C
  • VBA rand 如何使用上限和下限生成随机数?

    所以也许这是多余的 也许这就像问为什么大多数人生来就有 5 个手指 最后的简短答案总是 因为事情就是这样 而且它就是这样工作的 但我讨厌这个答案 该死的我想知道怎么做VBA 中的 Rnd 函数有效 Ms Office Excel 的 MSD
  • 如何更改焦点/按下时图像按钮的色调

    我有一个ImageButton在我的应用程序中 当按钮打开时我需要更改图像的色调pressed focused 我有ImageButton设置为获取其src来自 XML 文件 如下所示
  • 从项目文件加载图像

    我正在尝试获取 png 图像 这是我的资源文件夹 我测试了这里写的解决方案 将图像添加到列表框 c Windows Phone 7 https stackoverflow com questions 9348766 add images t
  • 使用 Python 在 OpenOffice/Microsoft Word 中格式化输出

    我正在开发一个需要格式化 可编辑输出的项目 Python 由于最终用户不会精通技术 因此输出需要采用文字处理器可编辑的格式 格式很复杂 要点 段落 粗体等 有没有办法使用Python生成这样的报告 我觉得应该有一种方法可以使用 Micros
  • 在 PowerPoint 中查找文本并替换为 Excel 单元格中的文本

    我正在尝试查找 PowerPoint 幻灯片中的单词列表并将其替换为 Excel 文件中单元格中的值 我在 PowerPoint 中运行 VBA 但出现此错误 运行时错误 2147024809 80070057 指定的值超出范围 代码似乎停
  • MS Access VBA:通过 Outlook 发送电子邮件 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 如何使用 MS Access VBA 通过帐户发送电子邮件 我知道这个问题很模糊 但是很难在网上找到在某种程度上还没有过时的相关信息 编辑
  • 使用 SSL 的 Xamarin.Forms Image.Source

    我正在使用一个在线商店来存储通过我们的应用程序上传的用户图像 并受 SSL 保护 上传工作一切顺利 因为我使用的是带有附加证书的 WebClient 但是当我尝试使用 Xamarin Forms Image 组件时 例如将源设置为 http
  • 使用 scikit-image 和 Transform.PolynomialTransform 进行图像变形

    我附上一个压缩档案 https drive google com file d 0B6EnJ Vh6zs1MkVCRlNhZkJsOEk view usp sharing包含说明和重现问题所需的所有文件 我还没有上传图片的权限 我有一个带有
  • 从剪贴板获取图像 Awt 与 FX

    最近 我们的 Java FX 应用程序无法再从剪贴板读取图像 例如 用户在 Microsofts Paint 中选择图像的一部分并按复制 我不是在谈论复制的图像文件 它们工作得很好 我很确定它过去已经有效 但我仍然需要验证这一点 尽管如此
  • 了解客户端文件的对象 URL 以及如何释放内存

    我在用createObjectURL获取本地图像文件的引用 URL 当我完成文件 图像后 我打电话revokeObjectURL释放该内存 一切对我来说都很好 但我只是想确保我释放了我能释放的所有内存 我检查后出现了我的担忧chrome b

随机推荐