В моем первом сообщении так жаль каких-либо ошибок.Форма доступа по имени в заголовке
Я пытаюсь заменить изображение в заголовке документа. Это будет сделано несколько тысяч раз.
Эта строка кода:
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
. Я чувствую, что это очень простая проблема с тем, как я обращаюсь к заголовку, или, может быть, весь этот суб-код просто ошибается. Линию 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
Это зафиксировал его: Set л.с = ActiveDocument.StoryRanges (wdPrimaryHeaderStory) .ShapeRange (strPic) –