【问题标题】:Word footer image alignment in VBAVBA中的Word页脚图像对齐
【发布时间】:2020-02-04 00:16:13
【问题描述】:

我尝试在 Word 文档的页脚中添加签名,但无法将其与页脚的右下角对齐。

另外,在我的页脚中有一行文字(即我的 Company Inc),并且签名必须正好在文字上方,如屏幕截图所示:

有什么帮助吗?

我的代码,除了定位之外,它都可以工作:


Sub Macro1()

Dim SHP as String


FIRMADOC = "C:\Users\user\Pictures\1.png"


    If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
        ActiveWindow.Panes(2).Close
    End If

    If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
        ActivePane.View.Type = wdOutlineView Then
        ActiveWindow.ActivePane.View.Type = wdPrintView
    End If


ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter

    Set SHP = Selection.InlineShapes.AddPicture(FileName:=FIRMADOC, LinkToFile:=False, SaveWithDocument:=True)
        With SHP
            'AJUSTA A "ENFRENTE DEL TEXTO"
            .ConvertToShape
            ' MANTIENE EL RATIO
            .LockAspectRatio = msoTrue
            'AJUSTA A ANCHO 1 inch
            .Width = InchesToPoints(1)
    '        .Alignment = ' need this code for bottom-right, PLEASE

        End With
   ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End sub

【问题讨论】:

    标签: vba image ms-word alignment


    【解决方案1】:

    因为Shape 对象“浮动”在页面上,所以可以轻松定位它们。它们也可以很容易(并且意外地)重新定位。 Shape 使用代码处理对象也很棘手。所以我使用的一个有用的经验法则是:如果InlineShape 有效,请使用它而不是Shape

    下面列出了三种可能性;两个用于 InlineShapes,一个用于 Shape。

    InlineShape 可以使用两种不同的方法(取决于它在段落中是否单独)与页面右对齐。

    1. 右对齐包含InlineShape 的段落。这适用于段落没有其他内容的情况。仅从问题中提取代码以处理此问题:
    Dim SHP as InlineShape
    
    Set SHP = Selection.InlineShapes.AddPicture(FileName:=FIRMADOC, _
                        LinkToFile:=False, SaveWithDocument:=True)
    SHP.Range.ParagraphFormat.Alignment = wdAlignParagraphRight
    
    1. 如果段落左侧有其他内容,则在InlineShape 之前带有制表符的右对齐制表符将起作用。默认情况下,页脚有两个制表位:一个居中对齐,第二个右对齐。

    为此,我将更改问题中的整个代码,以优化在页脚中的工作。 (同样的方法适用于 Header BTW)。宏记录器生成模拟用户操作的代码,因此它实际上使用ActiveWindowSelection 之类的东西打开了页脚(或页眉)。这些有些难以精确控制;使用实际的 Word 对象更可靠。

    Range 对象想象成一个不可见的选择。整个页脚区域分配给一个范围 (rng)。由于页脚已经有内容(“Company Inc”文本),因此有必要“折叠”Range。 (把它想象成按左箭头,这样新内容就不会替换选择。)

    然后添加两个TAB字符(rng.Text = vbTab &amp; vbTab)并添加签名。

    Sub Macro1()
      Dim FIRMADOC as String
      Dim SHP as InlineShape
      Dim rng as Word.Range
    
      FIRMADOC = "C:\Users\user\Pictures\1.png"
    
      Set rng = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range 
      rng.Collapse wdCollapseStart 
      rng.Text = vbTab & vbTab 'position at second, right-aligned tab in the footer)  
      Set SHP = rng.InlineShapes.AddPicture(FileName:=FIRMADOC, LinkToFile:=False, SaveWithDocument:=True, Range:=rng)
      With SHP
        ' MANTIENE EL RATIO
        .LockAspectRatio = msoTrue
        'AJUSTA A ANCHO 1 inch
        .Width = InchesToPoints(1)
      End With
    End sub
    
    1. 如果需要使用Shape 对象,则需要LeftRelativeHorizontalPosition 属性的组合。 wdShapePositionWdRelativeHorizontalPosition 枚举的成员指定这些特殊设置。

    请注意,可能还需要包含Top 属性才能将Shape 正确垂直放置到“Company, Inc”文本中。

    Sub Macro1()
      Dim FIRMADOC as String
      Dim SHP as InlineShape
      Dim rng as Word.Range
    
      FIRMADOC = "C:\Users\user\Pictures\1.png"
    
      Set rng = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range  
      rng.Collapse wdCollapseStart
      Set SHP = rng.InlineShapes.AddPicture(FileName:=FIRMADOC, LinkToFile:=False, SaveWithDocument:=True, Range:=rng)
      Set SHP = SHP.ConvertToShape
      With SHP
        ' MANTIENE EL RATIO
        .LockAspectRatio = msoTrue
        'AJUSTA A ANCHO 1 inch
        .Width = InchesToPoints(1)
        .Left = wdShapeRight '-999996
        .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin '0
      End With
    End sub
    

    【讨论】:

    • @G.Asins 太好了 :-) 很高兴我们能解决这个问题!我将对问题和答案进行一些小的编辑,以便将来遇到类似问题的访问者更清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 2017-12-12
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    相关资源
    最近更新 更多