【问题标题】:Editing anchor position in header of Microsoft Word using vba使用 vba 编辑 Microsoft Word 标题中的锚点位置
【发布时间】:2018-10-27 16:43:04
【问题描述】:

我正在开发一个 VBA 宏,我自己只编写了它的一部分,用于 MS-Word,它改变页面的方向,然后将前一页的页眉和页脚复制到新页面和其他一些东西:

Selection.PageSetup.Orientation = wdOrientLandscape
ActiveDocument.Sections.Last.PageSetup.DifferentFirstPageHeaderFooter = False

ActiveDocument.Sections(ActiveDocument.Sections.Last.index - 1).Headers(wdHeaderFooterPrimary).Range.Select
Selection.Copy

ActiveDocument.Sections.Last.Headers(wdHeaderFooterPrimary).Range.Select
Selection.Paste

ActiveDocument.Sections.Last.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
ActiveDocument.Sections.Last.Footers(wdHeaderFooterPrimary).LinkToPrevious = False

formatHeader wdHeaderFooterPrimary
formatHeader wdHeaderFooterFirstPage

TextBox 中有一个锚定到标题的文本。我现在要做的是更改其在“横向”方向的页面上的位置。

如何更改布局选项(见下图)?我找不到任何信息。

这是我的文档在将页面方向更改为“横向”后的样子:

如您所见,TextBox 中侧面的段落不在中间。所以我想把它移高一点。您还可以在此图像中看到锚点。

这就是我作为用户在 Word 中的操作方式:

【问题讨论】:

    标签: vba ms-word anchor word-2010


    【解决方案1】:

    关键是设置应从哪里进行测量 (RelativeHorizontalPosition),然后使用 Shape 的 Left 设置。相对于除了wdCharacter 之外的几乎任何东西,当文本被编辑时,页面上的水平位置将是静态的;在垂直方向上,wdLinewdParagraph 相当于使用“移动文本”。

    通过声明和使用最后一个部分和形状的对象,我已经简化了您发布的代码。

    此代码不是复制和粘贴,而是使用Range.FormattedText 将内容从一个标头复制传输到另一个标头。对于那些可以工作的情况(在任何两个单词范围之间),这比使用剪贴板更可取。

    Dim secLast as Word.Section
    Dim shp as Word.Shape
    
    Set secLast = ActiveDocument.Sections.Last
    secLast.PageSetup.Orientation = wdOrientLandscape
    secLast.PageSetup.DifferentFirstPageHeaderFooter = False
    secLast.Headers(wdHeaderFooterPrimary).Range.FormattedText = _
      ActiveDocument.Sections(secLast.index - 1).Headers(wdHeaderFooterPrimary).Range.FormattedText
    
    secLast.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
    secLast.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
    
    formatHeader wdHeaderFooterPrimary
    formatHeader wdHeaderFooterFirstPage
    
    Set shp = secLast.Range.Shapes(1)
    shp.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage 
    shp.Left = 33 ' Or you can use, for example CentimetersToPoints(1.4)
    shp.RelativeVerticalPosition = wdRelativeVerticalPositionPage
    shp.Top = CentimetersToPoints(14)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 2016-11-04
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多