Search code examples
vb.netwinformsms-wordoffice-interop

How to save Word.Range into document


I'm trying to copy all text from one Word document and to replace into other Word document. I opened and copied all text from source document into Word.Range after I found the text that must be replaced in destination document (with Word.Range) and I replace founded Range with Range from source document:

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

In debug I see that wordRangeFind are changed with wordRange but destWordDoc.Content remains as it was. After destWordDoc.Save() i see the file without changes. Can you write me please, how can i save Word.Range into Word document. Here are my function:

  '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)

Thanks.


Solution

  • I solved my Problem otherwise (without to save Range) but with Copy() and Paste() functions. Here is my code:

      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)