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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.