所以我有一个包含图片和标题的文本框的文档。我编写了一个循环遍历对象的代码,检查对象是否是文本框,如果是,则复制标题的文本,清除文本框,用更新的图片替换图片,然后重新插入标题。
但是,该文档已经制作了引用标题的交叉引用。因此,当我重新插入标题时,交叉引用不再起作用。我想我可以通过仅删除每个文本框中的图片来解决这个问题,因此标题保持不变并且交叉引用有效。
我不知道如何在我的代码中引用图片。任何帮助将不胜感激!
For Each objShape In ActiveDocument.Shapes
If objShape.Type = msoTextBox Then
str = objShape.TextFrame.TextRange.Text
If InStr(str, "(") > 0 Then
captionTag = BetweenParentheses(str)
If captionTag = imageTag Then
If InStr(str, "Figure") > 0 Then
'problem area
Dim objPic As Word.InlineShapes
objPic.Delete
'does stuff
Dim firstTerm As String
Dim secondTerm As String
Dim caption As String
Dim caption2 As String
firstTerm = ":"
secondTerm = ")"
Dim startPos As Long
Dim stopPos As Long
Dim nextPosition As Long
nextPosition = 1
caption = objShape.TextFrame.TextRange
Do Until nextPosition = 0
startPos = InStr(nextPosition, caption, firstTerm, vbTextCompare) + 1
stopPos = InStr(startPos, caption, secondTerm, vbTextCompare) + 1
caption = Mid$(caption, startPos + Len(firstTerm), stopPos - startPos - Len(firstTerm))
nextPosition = InStr(stopPos, caption, firstTerm, vbTextCompare)
Loop
Set rng = objShape.TextFrame.TextRange
Set picture = rng.InlineShapes.AddPicture(fileName:=fullPath, LinkToFile:=False, SaveWithDocument:=True)
picture.ScaleHeight = 29.5
picture.ScaleWidth = 29.5
rng.InsertCaption Label:="Figure", Title:=": " & caption, position:=wdCaptionPositionBelow, ExcludeLabel:=False
With objShape.TextFrame
.TextRange.Font.Name = "Calibri Light"
.TextRange.Font.Size = 9
.TextRange.Font.Color = RGB(79, 129, 189)
End With
'Next objPic
End If
End If
End If
End If
Next objShape
答案 0 :(得分:0)
使用Shape.Fill属性
这将返回一个FillFormat对象,用于添加或删除图片。
'This will clear the existing picture
objShape.Fill.Solid
'This will set a picture using a path
objShape.Fill.UserPicture ("C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg")
参考:
答案 1 :(得分:0)
好的,所以解决方案非常简单......现在我不必重新插入标题,参考文献仍在那里。
Set rng = objShape.TextFrame.TextRange
rng.InlineShapes(1).Delete
Set picture = rng.InlineShapes.AddPicture(fileName:=fullPath, LinkToFile:=False, SaveWithDocument:=True)