我的第一篇文章,对于任何错误,深表歉意。
我正在尝试替换文档标题中的图像。这将完成数千次。
这行代码:
Set shp = ActiveDocument.Sections(1).Headers.Shapes(strPic)
原因:
Compile error: Method or data member not found
和
Set shp = ActiveDocument.Shapes("strPic")
原因:
Run-time error '-2147024809(80070057): The item with the specified name wasn't found.
我已经用多种方式编写,但无法让它分配 shp
我觉得这是一个非常简单的问题,关于我如何访问 header ,或者也许整个子都是错误的。获取形状名称之前的 strPic
行很好。
Sub imagerepl()
With Selection
' \\ If we selected a InlineShape then convert to Shape
If .Type = wdSelectionInlineShape Then
.InlineShapes(1).ConvertToShape
End If
End With
strPic = Selection.ShapeRange.Name
Set shp = ActiveDocument.Sections(1).Headers.Shapes(strPic)
'Capture properties of exisitng picture such as location and size
With shp
t = .Top
l = .Left
h = .Height
w = .Width
End With
ActiveDocument.StoryRanges(wdPrimaryHeaderStory).ShapeRange(strPic).Delete
Set shp = ActiveDocument.Shapes.AddPicture("C:\Users\tk\Pictures\DFHlogo.png", msoFalse, msoTrue, l, t, w, h)
shp.Name = strPic
shp.ScaleHeight Factor:=1, RelativeToOriginalSize:=msoTrue
shp.ScaleWidth Factor:=1, RelativeToOriginalSize:=msoTrue
End Sub
最佳答案
您可以通过 Range.InlineShapes
集合访问标题图像。例如,以下例程将删除集合中的第一个图像,然后将新图像添加到标题中。
Dim sh As InlineShape
With ThisDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
' Get the first (possibly only, for you) image in the header...
Set sh = .InlineShapes(1)
' Delete it...
sh.Delete
' Add a new image to the header...
Set sh = .InlineShapes.AddPicture("c:\path\to\my.jpg")
' Set its properties...
sh.Width = 100
sh.Height = 100
...
End With
关于vba - 按标题中的名称访问形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32276939/