0

我正在尝试使用以下方法替换 MS Word 标题中的图像:

For Each tmp In ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes
    If tmp.Type = msoPicture Then
        Set dShape = tmp
    End If
Next
Dim w, h, t, l As Single
Dim lic As Long
Dim rhp As WdRelativeHorizontalPosition
Dim rvp As WdRelativeVerticalPosition

dShape.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
dShape.RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin

With dShape
    w = .Width
    h = .Height
    t = .Top
    l = .Left
    lic = .LayoutInCell
End With

Dim shp As Shape
Set shp = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes.AddPicture("C:\aa.jpg", False, True, l, t, w, h)
shp.RelativeVerticalPosition = rvp
shp.RelativeHorizontalPosition = rhp
shp.LayoutInCell = lic

dShape.Select
dShape.Delete

但是新图像不会放在以前的图像位置!
问题是什么?如何将新图像准确地放置在以前的图像位置上?

谢谢

4

1 回答 1

1

要遵循的一些步骤:

当您尝试添加新图像时,您的旧图像仍在原位......所以删除旧图像......;)

  • 在删除之前保存旧图像的位置、大小和其他属性(inline with text, square..etc)
  • 删除旧图像
  • 插入新图像top, left, width, height以及与旧图像的值匹配的其他属性

代码更改:

Dim shp As Shape

'--other codes including the ones that saves properties
dShape.Delete '--hey you don't need to select to delete :)

Set shp = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)_ 
         .Shapes.AddPicture("C:\aa.jpg", False, True, l, t, w, h)
shp.RelativeVerticalPosition = rvp
shp.RelativeHorizontalPosition = rhp
shp.LayoutInCell = lic
于 2013-01-05T13:38:18.387 回答