2013-12-12 2 views
0

У меня есть следующий код VBA, который находит текст заполнителя (FindText) во всех активных документах и ​​заменяет текст изображением. Этот код отлично работает, когда текст находится в теле документа; Однако, если текст-заполнитель находится в заголовке документа, текст не заменяется изображением.Microsoft Word Macro VBA Заменить текст с изображением в заголовке документа

Мой вопрос: как заменить текст заполнителя изображением, если текст находится в заголовке документа?

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 

ответ

2

, вы хотите, чтобы открыть заголовок, чтобы заменить текст. Вы можете сделать это с помощью этой строки кода

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 
+1

спасибо. хорошо работал. Я просто закрыл представление, а также ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument Спасибо – user1783736

Смежные вопросы