Vba 在Word文档中搜索表格内的文本并将其替换为图像
我需要用imageVba 在Word文档中搜索表格内的文本并将其替换为图像,vba,ms-word,Vba,Ms Word,我需要用imageD:\images\filename123.jpg替换Word文档中表中出现的每个filename123(每个标记内容都不同)。 我使用了下面的代码,从另一个代码复制而来,它很好地执行了search命令,但是我无法使replace行正常工作。怎么了 Sub Demo() Dim StrOut As String With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFo
D:\images\filename123.jpg
替换Word文档中表中出现的每个filename123
(每个标记内容都不同)。
我使用了下面的代码,从另一个代码复制而来,它很好地执行了search命令,但是我无法使replace行正常工作。怎么了
Sub Demo()
Dim StrOut As String
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\<tag\>*\</tag\>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .Information(wdWithInTable) = True Then
StrOut = Split(Split(.Text, ">")(1), "<")(0)
End If
.Collapse wdCollapseEnd
' the following line yields "Run-time error 9"
.InlineShapes.AddPicture FileName:= _
"D:\images\" & StrOut, LinkToFile:=False _
, SaveWithDocument:=True
.Text = ""
.Find.Execute
Loop
End With
End Sub
子演示()
像弦一样的暗弦
使用ActiveDocument.Range
和…一起找
.ClearFormatting
.Replacement.ClearFormatting
.Text=“\*\”
.Replacement.Text=“”
.Forward=True
.Wrap=wdFindStop
.Format=False
.MatchWildcards=True
.执行
以
找,找,找到
如果.Information(wdWithInTable)=True,则
StrOut=Split(Split(.Text,“>”)(1),“这应该适合您
- 我看到此标记将始终出现在表中。“.Information(wdWithInTable)”
- 我还将'split'更改为'replace',以避免使用索引
- 您的图像代码中缺少文件扩展名,因此将'.png'替换为任何格式的图像
子演示()
像弦一样的暗弦
使用ActiveDocument.Range
和…一起找
.ClearFormatting
.Replacement.ClearFormatting
.Text=“\*\”
.Replacement.Text=“”
.Forward=True
.Wrap=wdFindStop
.Format=False
.MatchWildcards=True
.执行
以
找,找,找到
如果.Information(wdWithInTable)=True,则
StrOut=Replace(替换(.Text,“,”),“,”)
如果结束
.倒塌
'下一行产生“运行时错误9”
.InlineShapes.AddPicture文件名:=_
“C:\Users\xyz\”&StrOut+“.png”,LinkToFile:=False_
,SaveWithDocument:=True
.Text=“”
.Find.Execute
环
以
端接头
Sub Demo()
Dim StrOut As String
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\<tag\>*\</tag\>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .Information(wdWithInTable) = True Then
StrOut = Replace(Replace(.Text, "<tag>", ""), "</tag>", "")
End If
.Collapse wdCollapseEnd
' the following line yields "Run-time error 9"
.InlineShapes.AddPicture FileName:= _
"C:\Users\xyz\" & StrOut + ".png", LinkToFile:=False _
, SaveWithDocument:=True
.Text = ""
.Find.Execute
Loop
End With
End Sub