Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2007-04-23
7
Medium Priority
?
2,742 Views
Last Modified: 2013-12-17
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?
0
Comment
Question by:jackjohnson44
  • 4
  • 3
7 Comments
 
LVL 11

Accepted Solution

by:
dready earned 2000 total points
ID: 18962602
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
 

Author Comment

by:jackjohnson44
ID: 18968516
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
 
LVL 11

Expert Comment

by:dready
ID: 18968715
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jackjohnson44
ID: 18998289
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
 

Author Comment

by:jackjohnson44
ID: 18998333
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
 

Author Comment

by:jackjohnson44
ID: 18998338
       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
 
LVL 11

Expert Comment

by:dready
ID: 18998506
Well, glad you finally solved it.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question