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

VBA Word向主标题添加现有形状

  •  0
  • Kevin  · 技术社区  · 6 年前

    用于在头中创建形状的常规代码(添加和创建操作合并到一个API“Add shape”)中:

    Set hf = doc.Sections(1).Footers(wdHeaderFooterPrimary)
    Set shp = hf.shapes.AddShape(msoShapeRectangle, 0, 0, 50, 50)
    

    我宁愿这样做(伪代码):

    Set hf = doc.Sections(1).Footers(wdHeaderFooterPrimary)
    Set shp = MyFunctionForBuildingAShapeThatIAlreadyUseElseWhere
    hf.Shapes.Add(shp)
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   macropod    6 年前

    因为它是相同的形状,只需指向原始的锚并使用.FormattedText方法在头中复制它。例如:

    Dim doc As Document, Rng As Range
    Set doc = ActiveDocument
    With doc
      Set Rng = .Sections(1).Headers(wdHeaderFooterPrimary).Range
      Rng.Collapse wdCollapseStart
      Rng.FormattedText = .Shapes(1).Anchor.FormattedText
    End With