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

问题描述

我正在尝试从一个Word文档中复制所有文本,并替换为另一个Word文档。 在我发现必须在目标文档中替换的文本(使用Word.Range)并将我从原文档创建的Range替换为Range之后,我将所有文档从源文档复制并复制到Word.Range中。

filterKeys

在调试中,我看到wordRangeFind已被wordRange更改,但destWordDoc.Content保持原样。在destWordDoc.Save()之后,我看到的文件没有更改。 您能给我写信吗,我如何将Word.Range保存到Word文档中。 这是我的功能

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

谢谢。

解决方法

我以其他方式解决了我的问题(不保存范围),但是使用了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)
  'Open destination Document
  Dim destWordApp As Word.Application = New Word.Application()
  destWordApp.Visible = False
  Dim destWordDoc As Word.Document = destWordApp.Documents.Open(destWordFile,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)
  sourceWordDoc.Close(missing,missing)