Microsoft Word宏VBA将文档标题中的文本替换为图像
我有以下VBA代码,可以在所有活动文档中查找占位符文本(FindText),并用图像替换文本。当文本位于文档体中时,此代码工作正常;但是,如果占位符文本位于文档标题中,则不会将文本替换为图像 我的问题是,如果文本位于文档的标题中,如何用图像替换占位符文本Microsoft Word宏VBA将文档标题中的文本替换为图像,vba,ms-word,Vba,Ms Word,我有以下VBA代码,可以在所有活动文档中查找占位符文本(FindText),并用图像替换文本。当文本位于文档体中时,此代码工作正常;但是,如果占位符文本位于文档标题中,则不会将文本替换为图像 我的问题是,如果文本位于文档的标题中,如何用图像替换占位符文本 Sub InsertImagesAllDocuments() Dim n, c As Integer n = Application.Documents.Count c = 1 Dim r As range Windows(c).Acti
Sub InsertImagesAllDocuments()
Dim n, c As Integer
n = Application.Documents.Count
c = 1
Dim r As range
Windows(c).Activate
Do
Dim imageFullPath As String
Dim FindText As String
imageFullPath = "C:\Logo.jpg"
FindText = "TextPlaceholder"
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.text = FindText
' Loop until Word can no longer
' find the search string, inserting the specified image at each location
Do While .Execute
Selection.MoveRight
Selection.InlineShapes.AddPicture FileName:=imageFullPath, LinkToFile:=False, SaveWithDocument:=True
Loop
End With
End With
c = c + 1
On Error Resume Next
Windows(c).Activate
Loop Until c > n
End Sub
您需要打开标题以替换文本。您可以使用这行代码来执行此操作
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
'now the header is accessible, run your code
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.text = FindText
' Loop until Word can no longer
' find the search string, inserting the specified image at each location
Do While .Execute
Selection.MoveRight
Selection.InlineShapes.AddPicture FileName:=imageFullPath, LinkToFile:=False, SaveWithDocument:=True
Loop
End With
End With
非常感谢。效果很好。我刚刚也关闭了视图ActiveWindow.ActivePane.view.SeekView=WDSeekIndocument谢谢