简体   繁体   English

VBA从Excel中替换Word,包括页眉,页脚和脚注

[英]VBA Replace in Word from Excel including headers, footers and footnotes

I have some data on an Excel file which I want to search-replace into a Word document through an Excel VBA macro. 我有一些Excel文件的数据,我想通过Excel VBA宏搜索替换为Word文档。 There are two columns: one with the things I want to look for, and another one for the replacement text. 有两列:一列包含我想要查找的内容,另一列包含替换文本。 That works fine. 这很好。

The problem is that I want to replace also inside the HEADERS, FOOTERS, FOOTNOTES and ENDNOTES. 问题是我想在HEADERS,FOOTERS,FOOTNOTES和ENDNOTES中更换。 I've tried some code with things from the Internet, but it does not fulfill its mission... Any help? 我已经尝试了一些来自互联网的代码,但是它没有完成它的任务......任何帮助?

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

Use the Footnotes collection. 使用Footnotes集合。

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.
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM