vb.net open word document, find bookmark, type text and print

I have a word document with a bookmark called "mybookmark".  How do I find the bookmark, type some text there, print it and close the document?
jackjohnson44Asked:
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.

dreadyCommented:
try this:

Function FindBMark()
   Dim WordObj As Word.Application
   Dim WordDoc As Word.Document
   Dim WordRange As Word.Range
   Set WordObj = CreateObject("Word.Application")
   Set WordDoc = WordObj.Documents.Open _
       ("C:\My Documents\Wordtest.doc")
   WordObj.Visible = True

   ' Go to the bookmark named "mybookmark."
   Set WordRange = WordDoc.Goto(What:=wdGoToBookmark, Name:="mybookmark")
   WordRange.InsertAfter "Here is some text for you"
    WordDoc.PrintOut Background:=False

   ' Uncomment the next line of code to save the modified document.
    WordDoc.Save
   ' Uncomment the line of code to quit Microsoft Word without
   ' saving changes to the document.
   'WordObj.Quit SaveChanges:=wdDoNotSaveChanges

   Set WordObj = Nothing
End Function
                  
0

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
jackjohnson44Author Commented:
thanks, I tried that but it said it can't find my bookmark even though I know it is there.  Is there any way to maybe list the bookmarks?
0
dreadyCommented:
wordDoc should have a Bookmarks collection
so wordDoc.Bookmarks.Count gives you the number, if you loop over it you can figure out the names of the single bookmarks.
Also, it might be that the name of the bookmark is case sensitive, did you check on that?

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

jackjohnson44Author Commented:
thanks, but I can't find my bookmark, it says "Word cannot find the requested bookmark."

It is definitely there though.  Haven't found a way to list them though.
0
jackjohnson44Author Commented:
I can't even find the bookmark this way, even though it prints out the name correctly.
        Debug.Print(WordDoc.Bookmarks.Item(1).Name.ToString)
        WordRange = WordDoc.GoTo(Word.WdGoToItem.wdGoToBookmark, Name:=WordDoc.Bookmarks.Item(1).Name)
0
jackjohnson44Author Commented:
       WordRange = WordDoc.Bookmarks.Item(1).Range
this is what I needed, the other line did not work
Set WordRange = WordDoc.Goto(What:=wdGoToBookmark, Name:="mybookmark")
0
dreadyCommented:
Well, glad you finally solved it.
0
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
.NET Programming

From novice to tech pro — start learning today.

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.