如何插入页面页脚,文件路径和图像? [英] How to insert page footer with page numbers, file path and image?

查看:168
本文介绍了如何插入页面页脚,文件路径和图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试格式化页脚,使页面右上角有#页面(x出y),然后图像位于下方。我最后为页面#编写了算法,然后使用inlineshapes插入上面的图像。问题是文本在图像下方,图像不居中。任何帮助,将不胜感激。

I'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


推荐答案

我已经解决了一些问题。它比我认为它会变大。我相信它可以让你开始想要达到的目标。

I've worked out something. Its bigger then I thought it would become. I sure it gets you started with what you want to reach.

expert-exchange.com提供了一些帮助,他们的解决方案是
VBA在Word页脚中插入修改后的页面x。我在代码中提到它,我用它来将测试转换成字段。

There was some help involved from experts-exchange.com with their solution on "VBA to insert a modified Page x of y in a Word Footer". I've mentiond it in the code where I use it to convert test into fields.

如你在其他问题中提到的那样如何在不影响页脚/标题的情况下启用页码我按照方法使用带有空白边框的表格。它们允许您非常精确地放置内容。
这就是为什么下面的代码会插入一个包含三列的表:

As mentioned in your other question "How to enable page numbers without affecting footers/headers" I follow the approach to use tables with empty borders. They allow you to place content very exact. That's why the code below will insert a table with three columns:

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

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

Below find the code. The main method InsertFooter you'll want to call from your code. It will do what you desire:

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

这篇关于如何插入页面页脚,文件路径和图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆