2017-03-24 11 views
1

Excel VBAマクロを使用してWord文書に置き換えて検索したいExcelファイルにデータがあります。 2つの列があります:1つは探したいものと、もう1つは置換テキストです。それはうまく動作します。VBAヘッダー、フッター、脚注を含むExcelからWordに置き換えます

問題は、ヘッダー、フッタ、脚注、およびENDNOTES内でも置き換えたいということです。私はインターネットからのものでいくつかのコードを試しましたが、それはその使命を果たさない...どんな助け?

Sub ReplacetoWord() 

Dim rngStory As Word.Range 
Dim lngJunk As Long 
Dim oShp As Shape 
Dim name_book As String 
Dim x As Variant 
Dim Filename As String 
Dim wArch As String 

name_book = ThisWorkbook.FullName 
x = Split(name_book, Application.PathSeparator) 
Filename = x(UBound(x)) 

Sheets("Generate_Report").Select 
wArch = Range("C3").Text & Range("C2").Text & ".docx" 

Set objWord = CreateObject("Word.Application") 
objWord.Documents.Open wArch 
objWord.Visible = True 

Workbooks(Filename).Activate 
Worksheets("Generate_Report").Select 

'Fix the skipped blank Header/Footer problem 
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType 

For i = 1 To Range("I1").Value 'cell with the number of data to replace 

Workbooks(Filename).Activate 
Worksheets("Generate_Report").Select 

datos = Range("B" & i).Text 'what to look for 
reemp = Range("A" & i).Text 'what to replace with 

    For Each rngStory In ActiveDocument.StoryRanges 
    'Iterate through all linked stories 
    Do 
     With objWord.Selection.Find 
     .Text = datos 
     .Replacement.Text = reemp 
     .Wrap = wdFindContinue 
     .Execute Replace:=wdReplaceAll 
     End With 
     On Error Resume Next 
     Select Case rngStory.StoryType 
     Case 6, 7, 8, 9, 10, 11 
     If rngStory.ShapeRange.count > 0 Then 
      For Each oShp In rngStory.ShapeRange 
      If oShp.TextFrame.HasText Then 
       With objWord.Selection.Find 
       .Text = datos 
       .Replacement.Text = reemp 
       .Wrap = wdFindContinue 
       .Execute Replace:=wdReplaceAll 
       End With 
      End If 
      Next 
     End If 
     Case Else 
     'Do Nothing 
     End Select 
     On Error GoTo 0 
     'Get next linked story (if any) 
     Set rngStory = rngStory.NextStoryRange 
    Loop Until rngStory Is Nothing 
    Next 


Next i 

objWord.Activate 


End Sub 

答えて

0

脚注コレクションを使用します。

foreach (Microsoft.Office.Interop.Word.Footnote footnote in word.ActiveDocument.Footnotes) 
{ 
    Microsoft.Office.Interop.Word.Range range = footnote.Range; 
    range.Find.Text = "find me"; 
    range.Find.MatchWholeWord = true; 
    range.Find.Execute(Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 

    if (range.Find.Found == true) 
    { 
     // Whatever you need to do. 
    } 
} 
関連する問題