Excel VBA将图像添加到Word文档并转换为形状



我正在使用URL添加图像到Word文档模板。我需要将内联形状转换为形状,以便更改包装和位置。我的代码如下:

Sub AddImg()
Dim objWord
Dim Template As String
Dim Img As InlineShape
Dim Shp As Shape
'Get Word template
Template = "..examplePathTemplate.docx"
Set objWord = CreateObject("word.Application")
objWord.Visible = True
'Copy template
Set objDoc = objWord.Documents.Add(Template:=Template, NewTemplate:=False, DocumentType:=0)
'Get image by URL
objWord.Application.Selection.Find.Execute "{Image}"
Set Img = objWord.Selection.InlineShapes.AddPicture(Filename:="www.example.com/example.png", _
LinkToFile:=False, _
SaveWithDocument:=True)
Set Shp = Img.ConvertToShape
With Shp
.LockAspectRatio = True
.WrapFormat.Type = wdWrapSquare
.Width = CentimetersToPoints(4)
End With
End Sub

正常工作,但Set Shp = Img.ConvertToShape行抛出错误"Type mismatch"为什么我很难正确地做到Set Shp?

我必须将Dim Shp As Shape更改为Dim Shp As Object

最新更新