0

Right now I have a Word Document with the following text:

FullName Position Address

Each line is made as a separate bookmark. The bookmark name for each line is the same as the word (the bookmark for FullName is FullName).

When I iterate through the bookmarks and update the text of the three different bookmarks, the first two get deleted and I am just left with the address. I think some how the bookmarks are getting combined in the editing process somehow.

Here is the code I have:

Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "Word|*.docx"
saveFileDialog1.Title = "Save your report file"

Dim FilePath = "C:\Users\Administrator.Laptop1\Desktop\TESTER.docx"
Dim myWordDoc As Microsoft.Office.Interop.Word.Document
Dim myWordApp As Microsoft.Office.Interop.Word.Application
Dim filepathname As Object = TryCast(FilePath, Object)
Dim missing As Object = Type.Missing
Dim objTrue As Object = TryCast(True, Object)

' create Word.Application object for the document
myWordApp = New Microsoft.Office.Interop.Word.Application

' open the document
myWordDoc = myWordApp.Documents.Open(filepathname, missing, missing, missing, missing,     missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)

'update Opinion
myWordDoc.Bookmarks("SendDate").Range.Text =     (Convert.ToDateTime(TextBox7.Text)).ToString("MMMM dd, yyyy")
myWordDoc.Bookmarks("FullName").Range.Text = TextBox4.Text & " " & TextBox5.Text & " " & TextBox6.Text & vbLf
myWordDoc.Bookmarks("Position").Range.Text = TextBox8.Text & vbLf
user3394463
  • 1
  • 1
  • 2
  • 1
    it is quite common issue in ms-word. [try this](http://word.mvps.org/faqs/macrosvba/InsertingTextAtBookmark.htm) or do better search in google. – Kazimierz Jawor Jun 05 '14 at 20:40

1 Answers1

0

Instead of using Interop services and bookmarks to accomplish this, you should consider using DocumentFormat.OpenXml.Wordprocessing and simple replacements.

Its more stable and behaves better, doesn't hang word.

You could simply loop through the documents body descendants for things like "{FirstName}" and do replacements, rather than using bookmarks which may always haunt you.

Nathan
  • 171
  • 3