Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
  HOME    TRACKER    Visual Basic

Bookmarks In Word

I am busy with a macro to put data from a textfile into a Word document using bookmarks. Here is the code:

Dim a(16) As String, x As Integer

Open "c:VBA ester.txt" For Input As #1

While Not EOF(1)
x = x + 1
Line Input #1, a(x)
Selection.InsertAfter Text:=a(x)

Close #1
I have named my bookmarks a1 to a16. The macro places the first string into a1, no problem. The next one, though, it places in a10, continues through to a16, then only goes to a2, a3, etc.

Is there any way to change the order of the bookmarks to make my macro work correctly?

View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Between Two Bookmarks (VB/WORD)

It's possible to get all the text (and images if can be possible) in a Word File between two bookmarks:

Something like this:


-Start: BookMark1-

All the text that are store in a Word File

-another bookmar-

more text and a image


That I want is to extract all that values in the Word file between StartBookmark1 and EndBookMark1 and place it into another word document

Is this possible? Thanks in advance

P.S: The bookmarks are empty, I only use them to locate the cursor, and know where starts and where end

Word And Bookmarks

I'm trying to place results from formulas in Excel into Word but having trouble with the positioning.
I've heard that bookmarks and the way to go but cant work out the code through VBA to place onto a word doc

Any ideas?

Thanks in advance

Bookmarks In Word
Hi All,
I have several word documents which have several bookmarks in them. My problem is that i need to change the name of a bookmark from "Payment" to "Paid" in all these files. The corresponding changes have to be done in the Hyperlinked text which referes to this bookmark in all the attachments.

Please can someone help me write the macro that can do this or even guide me to the correct place.

Word, Bookmarks, And VB
Hi Everyone,

My problem is that I have word template with bookmarks and some of the bookmarks appear several times throughout the template. I want to use a vb form using text boxes and radio buttons to interface with the word template and fill in the bookmarks. I can't seem to get the multiple bookmarks to work. Does anyone have any suggestions? Thanks

Bookmarks In Word

I read almost all threads about this subject but I didn't found an answer to my problem. What I want to do:

I select a few records from an Access database and I want to print these records in a table in Word. I created a table and in each column I put a bookmark. So far so good, but the amount of records can be variable. So I can select 1 til 1000 records. I don't want to make 1000 rows with bookmarks in it... it is a lot of work and if I don't use them I see the rows.

Can someone help me with this problem.

Regards, Silvano.

Bookmarks In Word
I am creating documents in Word in my app and populating the documents with data via bookmarks. This works fine - the problem I have is that in order to put data on the doc I ask the user questions and fill the doc up depending on the answers. I don't want the doc to appear until it is complete. But if the user has Word open already with another document, this one appears in an uncompleted state - how can I stop it showing? It is fine unless there is already a document active on the PC.

Bookmarks In Word
I am having trouble getting my vb code to fill Word bookmarks. This is my code:

Dim objWord As Word.Application
Set objWord = New Word.Application
objWord.Visible = True

'Open Template
objWord.Documents.Add Template:="P:Submittal", NewTemplate:=False

'Put in text
objWord.Selection.Goto What:=wdGoToBookmark, Name:="Equip"
objWord.Selection.TypeText strEquip

I keep getting object variable or with block variable not set error

Also, how do I print the document when I'm finished?

From VB To Word Doc's Bookmarks
Dim vals() As String
Dim wrd As Word.Application
Dim doc As Word.Document
Dim x As Integer
Set wrd = New Word.Application

Set doc = wrd.Documents.Open("C:Path_to_file.doc")

ReDim vals(doc.FormFields.Count - 1)
For x = 1 To doc.FormFields.Count
vals(x - 1) = doc.FormFields(x).Result


Previously [LGS]Static wrote (above) the code to extract Word doc's bookmarks works perfectly. Now I want to reverse it (from VB to word doc) and hit error on statement below:

For X = 1 to doc.FormFields.Count

doc.FormField(X-1).result = txtBKValue(X-1).Text
-> Run Time Error '4120', Bad Parameter


I also used the statement(s) you suggested and work. But I have 42 fields and I need toplace 42 similar statements below.

wrd.FormFields("Text7").Result = txtBKValue(1).Text

Any good way to solve it ?


Vb Word Bookmarks..not Going There
I have the following:

VB Code:
Private Sub mnuAutoCadDwgReq_Click()On Error GoTo AdoErrorDim objWord As Word.ApplicationDim objDoc As Word.DocumentDim objDB As Word.Range Set objWord = CreateObject("Word.Application")Set objDoc = objWord.Documents.Open("\ANDREWSJAK_Eng_Data000 JAK Mechanical StandardsStandards ManualChapter 1.doc")objWord.Visible = True objWord.ActiveDocument.GoTo wdGoToBookmark, , , "BM_1_5_1" With objDoc.Bookmarks        .DefaultSorting = wdSortByName        .ShowHidden = FalseEnd With Done:Exit Sub AdoError:    MsgBox Err.Description, vbCritical, "Error #: " & Err.Number    Resume Done End Sub

From a vb application. I open the document, which is fine..but I specify a bookmark to go directly to and it never just opens up at the top of the document. Can anyone point out my mistake or give me a hand.


Bookmarks In Word
Hi, I have a document that utilises bookmarks.
I use the bookmarks to insert text at certain places within the document.

My problem is that I want to update the bookmark text via code..


When I do this the text is appended to the text that already exists for the
What I would like to do is clear the existing text and insert some new text
in its place.

I tried calling the delete method but this deletes the bookmark as well.

Any help appreciated

Word Bookmarks
Does anyone know how I can get vb to open up a word document so that a particular bookmark is displayed rather than opening up at the top of the document every time.

Word Bookmarks

Using a VB-Script form I am trying to read from Word a string of text between two bookmarks. I use the following code to read the text in however I am unsure of the syntax to stop the loop once the second bookmark is reached.

Selection.GoTo what:=wdGoToBookmark, Name:="bookmark1"
character = selection.text


Bookmarks In Word
I'm trying to access Bookmarks in Word and place text in the
document at that point. I can get VB6 to open Word and the template I'm using. However, I get an error message telling me I'm not using the syntax correctly.

Can anyone HELP me!!!! PLEASE!!!!

That's the only problem in this program.

Bookmarks In Word 6 Vs Word 2002
I'm converting some Word 6 code to Word 2002 and I'm a a it baffled how some stuff works differently. For example the bookmarks.

This WD6 code counts the bookmarks in the header only
Code:size_ = WordBasic.CountBookmarks() - 1

Whereas this code in WD2002 counts all the bookmarks in document.

The only way I could duplicate the same count as the WD6 code was to do the following.

Public Function CountHeaderBookmarks() As Integer

Dim oBookmark As Bookmark
Dim oBookmarks As Bookmarks
Dim iCount As Integer

Set oBookmarks = ActiveDocument.Bookmarks
For Each oBookmark In oBookmarks
    If oBookmark.StoryType = wdMainTextStory Then
        iCount = iCount + 1
    End If
Next oBookmark

CountHeaderBookmarks = iCount - 1

End Function


Is this correct or is there an easier way?

Or is it simply that its just different in VBA6.3?


Edited by - Sparky191 on 12/16/2003 3:11:18 PM

Adding New Bookmarks Into Word Doc
Hi Guys 'n Galls,
I am trying to fill a Word form from VB. So far I have done this using Bookmarks I set in the Word-Doc. But now I would like to add some bookmarks from VB, so that I can be more flexible with the data I am adding.
My questions are:
How do I add a new bookmark to my Doc using VB code?
How can I define where I want this Bookmark to be?

Thanks in advance,

Question Regarding VBA Word Bookmarks
I have a Access program that has a DB tied to a form. Users just fill in the form the same as they would if it were on paper (minus the pens obviously ). The form fits perfectly on an 8.5x11 inch sheet.

I am adding the ability to export the data to MS Word. Each record / form is one page, and when I export them to word, I want them all in the same file - one page after another.

I open the word doc using a template with bookmarks B1-B50 which give me the fields, and I fill in the text. The Word form is basically contructed with lines and text boxes. The text boxes are where the bookmarks are.

Right now, I basically have a tempDoc and a totalDoc. TempDoc is opened with the template and the data is pumped in. I then copy it, and paste it to TotalDoc and fill in TempDoc again, and paste again until the records are all done.

The problem is this - how do I clear the text in the bookmarks from the last record on tempDoc. The way it is now, it just appends it. For example, if the data has "Hey" a field in the first record and "You" for the same field in the second record, my 2nd word page/form would contain "HeyYou" in that field/textbox.

To get around this, I closed TempDoc, then re-opened it to clear the data each loop, but I'm sure this causes un-needed processor cycles and slows down the application.

Here is the basic code - if you are still with me.

How do I clear the data in tempDoc.Bookmarks(s).Range.Text ??

That line appears to just be appending the data onto what is already there... not replacing it - which is what I want.

I am also open to any other pointers about the code below? Am I going about this the wrong way? I'm no expert when it comes to VBA or VB for that matter, so if the code is screwed up, I'd like some fixes / opinions / easier way to do things / performance improvements / etc.

Public Sub WordExport()

Set m_objWord = New Word.Application
m_objWord.Visible = False

Dim i As Integer, s As String, strWorkingDocName As String
Dim tempDoc As Word.Document
Dim totalDoc As Word.Document
Dim totalRange As Range
Dim tempRange As Range

strWorkingDocName = m_objWord.ActiveDocument.Name
Set totalDoc = m_objWord.Documents(strWorkingDocName)

Dim MyLocation As String
MyLocation = ""
Set tempDoc = m_objWord.Documents.Add(MyLocation)
Set totalRange = totalDoc.Range

Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("Sorted", dbOpenDynaset)


Do While Not rst.EOF

'This is where I tested the idea of closing and re-opening each loop
'Set tempDoc = m_objWord.Documents.Add(MasterLoc)

For i = 0 To 50

'need this (I think) because you can't call a bookmark just a #
s = "B" & i
tempDoc.Bookmarks(s).Range.Text = rst.Fields(i)

Next i

'copy from temp to total
Set tempRange = tempDoc.Range(Start:=tempDoc.Content.Start, End:=tempDoc.Content.End)

If (Not rst.EOF) Then
totalRange.Collapse Direction:=wdCollapseEnd
totalRange.InsertBreak Type:=wdPageBreak
End If

'This is where I used the idea of closing and re-opening each loop
'tempDoc.Close wdDoNotSaveChanges


Set rst = Nothing

tempDoc.Close wdDoNotSaveChanges
m_objWord.Visible = True

Set tempDoc = Nothing
Set totalDoc = Nothing
Set m_objWord = Nothing
Set totalRange = Nothing
Set tempRange = Nothing

End Sub

Listing Word Bookmarks In VB
I'm trying to write a little program that lists all of the bookmarks in a Word document - similar to the add-bookmark dialogue that comes with Word. All I want to do is add the name of each bookmark to a listbox. I have no troulbe getting the bookmark names into the listbox in alphabetical order. But I can't seem to get the bookmarks into the listbox in the order they exist in the document. No matter what I do, it comes in alphabetically, regardless of how I have the sorted property set. This is the code I'm using:

For Each booky In Word.ActiveDocument.Bookmarks

I'm sorry if this is a very newbie-ish question. Any help would be greatly appreciated!


Arranging Bookmarks In Word
I am trying to add bookmarks to a Word doc that I create. I want these bookmarks to be underneath eachother. I have the following code. (The result of this code is that the bookmarks seem to be placed on top of eachother)

Set WordObject = CreateObject("Word.Application")
With WordObject
.Documents.Open (App.Path & "")
.Selection.Text = CurrentStudent

Do While Not m_rs.EOF
.Selection.Text = m_rs("ExamDate").Value & Chr(9) & Chr(9) & m_rs("Exam").Value & Chr(9) & Chr(9) & m_rs("Mark").Value & Chr(9) & Chr(9) & m_rs("Result").Value & vbCrLf
.ActiveDocument.Bookmarks.Add ("c" & lHolder)
.ActiveDocument.Bookmarks("c" & lHolder).Select
lHolder = lHolder + 1

End With
WordObject.Visible = True
'quite word
If Not (WordObject Is Nothing) Then Set WordObject = Nothing

Thanking you in advance

Bookmarks In Userform Word
On a user form I want this to occur. When the an option in the Drop-down Form Field is selected, the text in Bookmark”Student” and text “well done” will be copied to Bookmark”Comment”
My code does not work and no errors occur. Any ideas please.

Sub BookmarkCopy()
Dim bmComment As String
Dim bmStudent As String
bmComment = ActiveDocument.Bookmarks("BehaveComment").Range.Text
bmStudent = ActiveDocument.Bookmarks("Student").Range.Text
With ActiveDocument
If .Bookmarks("BehaveRating").Range.Text = "A" Then
bmComment = bmStudent & "Well done"
End If
End With
End Sub

Extract Bookmarks Value From Word.doc
Anybody please help me out how to extract bookmarks values from a word doc?

I have a word document with many bookmarks field named from Text1, Text2, Text3....etc.

What I did was:

Dim w As Word.Document
Dim a As Word.Application
Dim output1 As String

Set a = New Word.Application
Set w = New Word.Document
Set w = a.Documents.Open("c:my document est303.doc")

output1 = w.Bookmarks.Item(1) -> This only retreive BookMarks name as "Text1" The "Text1" current contains value "ABC Company". How to extract value from "Text1"?


Retrieve Bookmarks Value In Word Doc.
Quesiton: how to retreive Bookmarks value from word doc

I have a word document with many bookmarks field named from Text1, Text2, Text3....etc.

What I did was:

Dim w As Word.Document
Dim a As Word.Application
Dim output1 As String

Set a = New Word.Application
Set w = New Word.Document
Set w = a.Documents.Open("c:my document est303.doc")

output1 = w.Bookmarks.Item(1) -> This only retreive BookMarks name as "Text1"

How do I get the value from "Text1"? The Text1 contains value "ABC Company".

Appreciate anyone's help. Thank you.


Writting To Word Bookmarks
I have made a Word Template with Bookmarks and an application that writes to the bookmarks with data from the app. I have Word 2003 installed on my system. After creating the setup and installing it on another system the App crashed, I then noticed the other system has XP installed. So here is my question, is there a way to create an app that write data to bookmarks within Word that is independant of the version of Word being using? I will need to it work on systems with Word 2000, XP or 2003 installed.

I tried some test code, trying to make it work but was not successful, only works with the version of Word installed on my system. Here is the test code I used...

Dim WordApp As New Word.Application
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim ReportGenerator As New Word.Document
ReportGenerator = WordApp.Documents.Open("C:Report GeneratorDemo.doc")
With ReportGenerator

If Len(txtName1.Text) > 0 Then
End If

If Len(txtName2.Text) > 0 Then
End If

End With


Arranging Bookmarks In Word
I am trying to add bookmarks to a Word doc that I create. I want these bookmarks to be underneath eachother. I have the following code. (The result of this code is that the bookmarks seem to be placed on top of eachother)

Set WordObject = CreateObject("Word.Application")
With WordObject
.Documents.Open (App.Path & "")
.Selection.Text = CurrentStudent

Do While Not m_rs.EOF
.Selection.Text = m_rs("ExamDate").Value & Chr(9) & Chr(9) & m_rs("Exam").Value & Chr(9) & Chr(9) & m_rs("Mark").Value & Chr(9) & Chr(9) & m_rs("Result").Value & vbCrLf
.ActiveDocument.Bookmarks.Add ("c" & lHolder)
.ActiveDocument.Bookmarks("c" & lHolder).Select
lHolder = lHolder + 1

End With
WordObject.Visible = True
'quite word
If Not (WordObject Is Nothing) Then Set WordObject = Nothing

Thanking you in advance

MS Word Form Bookmarks
I'm sure this is quite trivial:

I have a combobox in a form as a bookmark. I choose from a list of people in the combobox (no problem), but I want a phone number to appear elsewhere on the page after I selected one person. I am not familiar with handling bookmarks in VB. I can do the If..then..else statements, but I don't know how to control the bookmark combobox in VB.

Can anyone help?

Bookmarks Word And Pictures

I was hoping someone could help i have a simple template and a user form which inserts text from Textboxs at the bookmarks.

HOw can i also insert a picture at the bookmark? Is this the right/wrong approach please help


Bookmarks And Word Documents
I read the recent bookmark help that was provided and thought it was well done. I do have a question I haven't seen a solution to yet.

I have a bookmark in a document and I would like to add four bookmarks on individual lines below the original bookmark programmatically.
for example:

[exisiting bookmark]
-new bookmark through code
-new bookmark through code
-new bookmark through code
-new bookmark through code

I have seen a lot of use of (range) RNG but it appear to be more pixel related or x,y rather than a actual new line.

Is it possible to add a bookmark to a specific line in a word document?

VBA - Word Bookmarks Alternative
I am currently working on a VB application that uses bookmarks on a word template
to insert fields from a database.

I have been instructed to use an alternative method that is supposed to be
called DataField. The advantage is apparently that you can use DataFields
to set the same reference in multiple places in a word document. Bookmarks
requires a seperate reference on each occaision even if the contents of the
field are the same.

For example if typing a letter and you insert the recipient "Mr J. Smith"
in a number of locations on the letter with bookmarks you have to set up
a distinct reference each time.

Does anyone know anything about using DataFields or perhaps it's called something
else. But apparently it exists and is superior to using bookmarks.

Any help would be greatly appreciated.


Word: Bookmarks Refering To The Header
I'm working with Word and want to make a bookmark refer to a place in the documents header. How is that done? I made a bookmark manualy that refered to a place in the header but it seemed to have the same range as a bookmark refering to a place in the main document.
That is, when i checked the bookmarks ranges start and end points they were overlapping (3 to 12 for the header bookmark and 9 to 13 for the main document bookmark).
How do i refer to a place in the header? Happy for any help.

Trouble With Bookmarks In Word Header
I am having trouble getting VBA to place data in bookmarks in the header of a word template. Specifically, VBA returns an error saying that it can't find the bookmarks (when they are in the header). I have been successful getting VBA to send data to bookmarks in other areas of the word template, just not the header. here is what i have tried so far. Thanks for any help.

Sub wordexport()

Dim name As String
name = "Smith, Bob"

Dim wdApp As Word.Application
Set objWord = New Word.Application
Set objWordDocs = objWord.Documents
objWordDocs.Open Filename:=""
Set objWordDoc = objWord.ActiveDocument
objWord.Visible = True

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.Goto What:=wdGoToBookmark, name:="CustomerName"

End Sub

Need Help To Set Bookmarks And Hyperlinks Automatically In MS Word
Hi all,

I used to work with MS Word Documents everyday. and I handle large amout of files everyday. I need to give hyperlinks in these word documents. I feel difficult to give hyperlink. I think it is too much time consuming and effort. So, I thought I may automate this process (i.e., to set bookmark and hyperlink). I decided I will ask the experts like your help to do this process.

Herewith I attached a sample file with all instructions.

Pls let me know your suggestions.



Move Text Between Two Bookmarks - Word VBA
Hi to everybody.

I am trying in VB for Word 2003, to build a macro that allows me to select the text between to bookmarks. Do you think this is posible? I don´t know what king of code I have to you. I need to copy everything that is between Bookmark A and Bookmark B.

Selection.GoTo What:=wdGoToBookmark, Name:="A"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.StartOf Unit:=wdParagraph
Selection.GoTo What:=wdGoToBookmark, Name:="B"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveEnd Unit:=wdParagraph

But this code only selects las paragrah were Bookmark "B" is. I need to copy from Bookmark "A" to Bookmark "B".

Thanks for any help and Ideas on how to select.

Word - Filename Derived From Bookmarks
Looking for a way to create a macro that saved a doc using a unique filename pieced together from bookmarks. That is, if a document has a person's name and work center bookmarked, and the directory location were C: emp
eports then then the macro when run should save the doc to that directory and name the file JOHNSON RECEIVING.doc (where Johnson is the Name Bookmark and Receiving is the Workcenter Bookmark).

It would be ideal if the macro were able to create a subdirectory to place the doc file. That is, if the person's name is JOHNSON, then it creates a subdirectory at C: emp
eportsJOHNSON and files the doc there.

Is this doable?



Opening Word And Using Bookmarks - RESOLVED
I made a really simple word doc piece for a friend that uses bookmarks and it works great on my machine, however when I put it on theirs it keeps shutting down the application.

I did a package and deploy and did a full install and made sure that the dll's were there, however whenever they hit print, it just shuts down the exe.

I want them to be able to basically let the bookmarks autofill and open the document and leave it open so they can insert a picture.

Here is sort of what I am doing.

Dim objWord As New Word.Application
wFileName = "c:program filespaetzoldformsconsent.doc"
Set objWord = New Word.Application
Set objDoc = objWord.Documents.Open(wFileName)

With objWord.ActiveDocument
MyMsg = MsgBox("Active Document Open")
.Bookmarks("BirthDate").Range.InsertAfter gDOB
.Bookmarks("ClientName").Range.InsertAfter txtFName.Text & " " & txtLName.Text
.Bookmarks("ClientName1").Range.InsertAfter txtFName.Text & " " & txtLName.Text
.Bookmarks("ClientName2").Range.InsertAfter txtFName.Text & " " & txtLName.Text
.Bookmarks("ClientName3").Range.InsertAfter txtFName.Text & " " & txtLName.Text
.Bookmarks("IntakeDate").Range.InsertAfter gIntakeDate
.Bookmarks("IntakeDate1").Range.InsertAfter gIntakeDate
.Bookmarks("SIN").Range.InsertAfter txtSIN.Text
.Bookmarks("SIN1").Range.InsertAfter txtSIN.Text
End With
objWord.Visible = True
End If

Set objWord = Nothing

Thanks in advance.

Extract Bookmarks Value From Word.doc (part 3)
Hi RobDog888,

Thank you for your speedy response! ButI still get the same error.

1) How to iterate through the collection?

Label1.caption =””
For i = 0 to 41
Labe1.caption = Label1.caption ^ w.Bookmarks(i).Range.Text ^ chr(13)

Next i

The “for loop” does not work for w.Bookmark(i).Range.Text -> because Bookmarks is an object (or collection)?

Perhaps something like the following (I think)??

Dim cntrlItem as ?? (control, index…etc)
For each cntrItem IN ?? (collection, ?)

Next cntrlItem

Can you or someone fill the “??” blank?
Thank you.

Extract Bookmarks Value From Word Doc (Solved!)
Thank you very much. It works perfectly!!!!!

Extract Bookmarks Value From Word.doc (part 2)
Dim w As Word.Document
Dim a As Word.Application
Dim output1 As String

Set a = New Word.Application
Set w = New Word.Document
Set w = a.Documents.Open("c:my document est303.doc")


Registered: Apr 01
Location: Los Angeles, CA.
Posts: 2625

visual basic code:--------------------------------------------------------------------------------
msgbox w.Bookmarks("Text1").Range.Text


Thank you very much, RobDog888.

Part 2:

I have about 42 bookmarks fields in a word doc. -> output1 = w.Bookmarks.Count /** to get the number of bookmarks in the doc.

Now I need to extract EACH of bookmarks value from the doc, and I think “for loop” should do it. However, I failed.

Dim CurrentField as String
Dim result as string

result = “”

For i = 0 to (a.ActiveDocument.Bookmarks.Count) - 1
CurrentField = w.Bookmarks.Item(i) -> Run-time error ‘5941’
-> The requested member of collection does not exit
result = result ^ w.Bookmarks(CurrentField).Range.Text ^ chr(13)

Next i

How to define “collection” for this case?

Your help is greatly appreciated.

MS WORD (Bookmarks Issue)- URGENT
I have a Word Tempelate and I'm trying to do something like this. On the first page I have three bookmarks (Name, Address, Sex) after filling the data from VB form I give a page break like this:

VB Code:
.Selection.InsertBreak Type:=wdPageBreak

How do I make sure that in that new page the Bookmarks from the first page are there...

Word: Select Text Between Two Bookmarks

Is it possible to have VB select the text between two pre-defined bookmarks so that I can then apply numbering or bullets?

If so, how is this done.


Bookmarks && Ranges In Microsoft Word
I have 2 bookmarks in a document called "RecipientStart" and "RecipientEnd" containing the following text:

Joseph Smith
15 Earl Street
M3 4DN

What is the best way to:
1) Specify the range of text
2) Delete the range of text without deleting the bookmarks.
3) Insert some replacement text

Are bookmarks the best way to specify a range of text? if so could someone give me an example of how to do the above.

Automating Word And Skipping Bookmarks (please Help)
I have a button that when clicked the contents of that form are sent to a word document with bookmarks in place to indicate where certain data should go. I have a global variable (intNo) that increments so that if the contents of another form are sent to the word document they go to the right place. However, the document contains a date which i dont need put in again if the button is clicked again. If intNo is above a certain value then that means the date has been inserted, i am trying to test if intNo above x amount then don't write the date, otherwise do, but its not working Any ideas? Here's my code:

With WordObj.ActiveDocument.Bookmarks
        If intNo > 1 Then
        ActiveDocument.Bookmarks("bmk" & intNo).Range.Text = Date
        intNo = intNo + 1
        End If
    End With
    With WordObj.ActiveDocument.Bookmarks
    ActiveDocument.Bookmarks("bmk" & intNo).Range.Text = (CStr(Forms!frmTutor!TU_DATE_APPOINTED))
    intNo = intNo + 1
    ActiveDocument.Bookmarks("bmk" & intNo).Range.Text = (CStr(Forms!frmTutor!TU_TITLE)) & " " & (CStr(Forms!frmTutor!TU_FORENAME)) & " " & (CStr(Forms!frmTutor!TU_SURNAME))
    intNo = intNo + 1
    ActiveDocument.Bookmarks("bmk" & intNo).Range.Text = (CStr(Forms!frmTutor!TU_CODE))
    intNo = intNo + 1
    End With

Inserting DELETE And TAB At Bookmarks In Word
Utilizing VBA in Word, is it possible to insert a delete command at a bookmark? How about a tab command?

The reason I ask is because depending on what the user selects in the UserForm of my template, some lines will be left blank but I do not want the user to have to delete them manually, I want the document to be as complete as possible after OK is clicked.

Can anyone tell me the command if this is possible?


Excel To Word Automation - Bookmarks
I have an excel workbook that I create a word report from. The report is based on a template. The template contains bookmarks. One of the bookmarks is 'CompanyName'. I want to know how I can fill the bookmark with the Company name from cell reference A7 from my spreadsheet?


Edited by - ajwelsh on 2/8/2004 7:35:05 AM

Bookmarks In Rtf Disappear When Dragged To Word
I'm affraid I found a bug in Word. (again)

Need some help.
I am dragging RTF text fragments from my VB app to MS Word.
This dragging works fine, but bookmarks disappear.
For example when the following fragment is dragged:
Code:par {*kmkstart aaaaa}zzzzzzzzzzz{*kmkend aaaaa}
par bbbbbbbbbbbbb
par }}
It appears without the bookmark in Word.
If I save it to disk and use Insert File in Word, the bookmarks stay...
As far as I know it is not possible to copy/paste or drag/drop formatted text to Word, without losing bookmarks.

It works fine when pasting from one Word doc to another Word doc.
Is Word using some secret clipboard format ?

Inserting Text Into Word Template With Bookmarks
I just started using the word functions yesterday, so I'm still pretty clueless about how to put this together. What I have is a form that a user can enter some data into, and then check that form off as ready to print. In the future they will be able to batch print all the forms that are checked, but at this point I'm just trying to do one.
I created a template in Word and inserted bookmarks where I wanted the form data to be filled in. This is the code I have right now:

Dim objWord As Word.Application
Dim aDoc As Word.Document
Dim var As Variant

Set objWord = New Word.Application

Set aDoc = Documents.Add("c:Renewal")

Selection.InsertAfter (newRepForm.Text20(57) & " " & newRepForm.Text20(58))

'Branch Number
Selection.InsertAfter (newRepForm.Text20(65))

'This goes on for about 16 more bookMarks
'Then I have this next line, though it doesn't work

msgbox "Done Printing"
And that's it. It makes it through the code and displays the message box, but nothing else seems to happen.
What am I missing? I searched these forums yesterday enough to decide bookmarks were the way to go, but like I said I've never used and of the word functions and I feel like I'm missing something fundamental. Any help would be appreciated.


Word 97 - Bookmarks/table Cell Refs

I know how to create a simple bookmark in Word using VB, but is there a way to tell it to find a table cell ref and create the bookmark?



***Resolved*** How To Replace Text Between Bookmarks In Word
Does anyone know how to replace text between two bookmarks in Word 2000? What I need to do is replace whatever text is between the bookmarks with a predefined phrase.

The actual text between the bookmarks to be replaced will be different everytime this routine is run so I can't use find and replace.

Thanks for your input.

Word Automation Data From Sql Server To Bookmarks
Appreciate sample code to populate WORD 6.0 bookmarks from SQL server
DB. Same templates would be used several times with different data
extracted from SQL Server DB.

Thanks & regards

Automation Problem Creating Bookmarks In MS Word
I have been given the task of creating an automation function that will add bookmarks to MS Word without having to go into Word itself. I have been able to add one bookmark successfully but adding a second one is causing a problem.

When the second bookmark is added, it attaches itself to the first bookmark at the beginning of the document.

Does anybody know how to add multiple bookmarks successfully to a Word Document and if possible how to add them to certain locations within the document.

I have tried moving the cursor around the document but the bookmarks are still being created at the beginning of the document.

Here is the code I am currently using

Dim BMRange As Range
Dim BMRange2 As Range
Dim TempRange As Range

SC.ActiveDocument.bookmarks.Add "MyBookmark"

Set BMRange = SC.ActiveDocument.bookmarks("MyBookmark").Range
BMRange.Text = "<<Bookmark1>>"
SC.ActiveDocument.bookmarks.Add "MyBookmark", BMRange

' This is the second bookmark which keeps setting itself a the beginning of the document with the first bookmark.

SC.ActiveDocument.bookmarks.Add "AnotherBookmark"

Set BMRange2 = SC.ActiveDocument.bookmarks("AnotherBookmark").Range
BMRange2.Text = "<<Bookmark2>>"

SC.ActiveDocument.bookmarks.Add "AnotherBookmark", BMRange2


Any help would be greatly appreciated, Thanks in advance

Preventing Mailmerge (Word) From Erasing Bookmarks && TextInput Boxes
“How can I prevent Mailmerge (Word) to ‘erase’ TextInput Boxes and Bookmarks (originally located in the main mailmerge document) in the merged document?
(I would like to put some shaded TextInput Boxes in the merged document in order to show the user where he has to type in some extra information! I would rather not prefer the use of Ask or Fillin Fields …)”

More detailed description of the problem:
The code I wrote opens Word from another application (Access), opens a main mailmerge document, merges with a text file that Access has created (I use a text file in order to speed up the code) and shows the merged document on screen.
The main mailmerge document contains mergefields, bookmarks and 2 formfields (a TextInput Box, and a DropDown Box).

The problem is: in the merged document the bookmarks and the TextInput Box have disappeared. The DropDown Box, however, didn’t disappear!

I would like to find an answer to the following questions:
1) What’s the raison that the Bookmarks and the TextInput Box have disappeared?
2) Why has the DropDown Box ‘survived’ the mailmerge?
3) Is it possible to write some code to prevent the bookmarks from disappearing or is there another solution?

P.S.: I am using Windows 2000 and Office 2000 (VBA)

Thanks in advance!!

Copyright © 2005-08, All rights reserved