代码之家  ›  专栏  ›  技术社区  ›  dieKoderin

使用vba编辑Microsoft Word标题中的定位点位置

  •  3
  • dieKoderin  · 技术社区  · 6 年前

    我正在为MS Word编写一个VBA宏,我自己只编写了其中的一部分,它可以更改页面的方向,然后将以前页面的页眉和页脚复制到新页面和其他一些内容:

    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
    

    文本框中有一个文本,定位到标题。我现在想做的是用“横向”方向改变它在页面上的位置。

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

    这是将页面方向更改为“横向”后我的文档的外观:

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

    以下是我在Word中作为用户所做的:

    enter image description here enter image description here

    1 回复  |  直到 6 年前
        1
  •  2
  •   Cindy Meister    6 年前

    关键是设置测量的位置( RelativeHorizontalPosition )然后使用形状的 Left 背景相对于几乎任何东西 wdCharacter 编辑文本时,页面上的水平位置将是静态的;垂直地 wdLine wdParagraph 相当于使用“随文本移动”。

    通过为最后一节和形状声明和使用对象,我简化了您发布的代码。

    此代码使用 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)
    
    推荐文章