vb.net and word

Can anyone help me with the code on how to retrieve bookmark text in a document into a textbox into a forum in vb.net?
Who is Participating?
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
You first need to reference Microsoft Word in the References tab of the project's Properties window.

Then, code like the following will do the trick:
		Dim wordApp As New Microsoft.Office.Interop.Word.Application
			wordApp.Documents.Open("C:\Users\Jacques\Documents\Cours\Windows 8\Applications Windows 8.doc")
			txtTitle.Text = wordApp.Documents(1).Bookmarks("Title").Range.Text
		Catch ex As Exception
			MessageBox.Show("There was a problem accessing bookmark Title in the Word document")
		End Try

Open in new window

This takes a few seconds because Word must be started in the background. You might want to have a waiting message somewhere on the screen while this is running.

The Try...Catch and the WordApp.Quit are very important. They make sure that Word will be closed, even in case of errors. Since it runs in the background, the users does not know that Word is running on his computer. If you do not make sure that it closes, you might end up with many copies of Word taking up resources. Just to be sure, some programmers prefer to add a wordApp.Visible = True line so that Word is visible to the user and he can close it in case of errors. In my opinion, proper error handling is sufficient.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.