如何将 Word.Range 保存到文档中

How to save Word.Range into document

我正在尝试从一个 Word 文档中复制所有文本并替换到另一个 Word 文档中。 在找到目标文档中必须替换的文本(使用 Word.Range)后,我打开源文档中的所有文本并将其复制到 Word.Range 中,并将已创建的范围替换为源文档中的范围:

 'Find and Replace in destination Document
  Dim wordRangeFind As Word.Range = destWordDoc.Content
  wordRangeFind.Find.Execute(findText)
  wordRangeFind = wordRange

在调试中,我看到 wordRangeFind 已更改为 wordRange,但 destWordDoc.Content 保持原样。在 destWordDoc.Save() 我看到文件没有变化。 你能给我写信吗,我怎样才能将 Word.Range 保存到 Word 文档中。 这是我的功能:

  'Imports Word = Microsoft.Office.Interop.Word

  Dim missing As Object = System.Reflection.Missing.Value
  Dim sourceWordApp As Word.Application = New Word.Application()
  sourceWordApp.Visible = False
  'Open source Document
  Dim sourceWordDoc As Word.Document = sourceWordApp.Documents.Open(sourceWordFile, missing, False,
                                      missing, missing, missing,
                                      missing, missing, missing,
                                      missing, missing, missing,
                                      missing, missing, missing, missing)
  'Open destination Document
  Dim destWordApp As Word.Application = New Word.Application()
  destWordApp.Visible = False
  Dim destWordDoc As Word.Document = destWordApp.Documents.Open(destWordFile, missing, False,
                                      missing, missing, missing,
                                      missing, missing, missing,
                                      missing, missing, missing,
                                      missing, missing, missing, missing)

  destWordDoc.Activate()
  'Get all text from source Document
  Dim wordRange As Word.Range = sourceWordDoc.Range(missing, missing)

  'Find and Replace in destination Document
  Dim wordRangeFind As Word.Range = destWordDoc.Content
  wordRangeFind.Find.Execute(findText)
  wordRangeFind = wordRange

  'Save and close dest Document
  destWordDoc.Save()
  destWordDoc.Close(missing, missing, missing)
  sourceWordDoc.Close(missing, missing, missing)

谢谢。

我用其他方式解决了我的问题(没有保存范围),但是使用了 Copy() 和 Paste() 函数。 这是我的代码:

  Dim missing As Object = System.Reflection.Missing.Value
  Dim sourceWordApp As Word.Application = New Word.Application()
  sourceWordApp.Visible = False
  'Open source Document
  Dim sourceWordDoc As Word.Document = sourceWordApp.Documents.Open(sourceWordFile, missing, False,
                                      missing, missing, missing,
                                      missing, missing, missing,
                                      missing, missing, missing,
                                      missing, missing, missing, missing)
  'Open destination Document
  Dim destWordApp As Word.Application = New Word.Application()
  destWordApp.Visible = False
  Dim destWordDoc As Word.Document = destWordApp.Documents.Open(destWordFile, missing, False,
                                      missing, missing, missing,
                                      missing, missing, missing,
                                      missing, missing, missing,
                                      missing, missing, missing, missing)

  destWordDoc.Activate()
  'Copy all text from source Document
  sourceWordApp.ActiveWindow.Selection.WholeStory()
  sourceWordApp.ActiveWindow.Selection.Copy()

  'Find in destination Document
  Dim wordRangeFind As Word.Range = destWordDoc.Content
  wordRangeFind.Find.Execute(findText)
  wordRangeFind.Text = ""
  wordRangeFind.Select()
  
  'Paste copied text into dest document
  destWordDoc.ActiveWindow.Selection.Paste()

  'Save and close dest document
  destWordDoc.Save()
  destWordDoc.Close(missing, missing, missing)
  sourceWordDoc.Close(missing, missing, missing)