Inserting text before a bookmark

S

singeredel

I guess my question regarding Recreating Bookmarks must not have been clear,
so I am trying to rethink the problem.

How can I insert text BEFORE a bookmark that appears AT THE BEGINNING OF A
LINE? Everything I have tried ends up incorporating the text into the actual
bookmark field.

For instance, I have a variable that stores the SSN#. If there is a value
for this variable, I want to insert BEFORE the bookmark at the beginning of
the line the heading "SSN:" + Chr(9) so that only the SSN# itself is included
in the bookmark range.

Previously I had the following:

oRng.Text = "SSN:" + Chr(9) + bText

but this made the heading "SSN" + Chr(9) + bText the bookmark range, whereas
I only want the actual SSN# (bText) from the variable as the bookmark range
so that reference fields can be used to place just the number (bText) in
other places in the document.

Thanks for any help.
 
G

Greg Maxey

This might make it clearer:

Sub PutTextInBookmark()
Dim oRng As Word.Range
Dim oBM As Bookmarks
Dim pSSN As String Set oBM = ActiveDocument.Bookmarks
Set oRng = oBM("SSN").Range 'SSN is your bookmark name/set a range = 'the
bookmark range
pSSN = "123-45-6789"' this is your data
oRng.Text = pSSN 'write the text at the defined range
oBM.Add "SSN", oRng 'Redefine the bookmark
End Sub
 
S

singeredel

I really appreciate your help, but I have not found how to reassign the
bookmark. Curently this is some of the relevant code:

Dim bBkm As String
Dim bText As String
Dim bIndex As Long
Dim oRng As Range

For bIndex = 1 To 17

bBkm = Choose(bIndex, "ReportBody", "ValdezPara", "ReportDate",
"Addressee", "AttentionLine", "PatientName", _
"SSN", "EMP", "DOI", "DOE", "ClaimNumber", "WCAB", "Occupation",
"OtherHeading", "ReportTitle", _
"NameFirst", "NameLast")

bText = Choose(bIndex, ReportBody, ValdezPara, ReportDate1, Addressee,
AttentionLine, PatientName, _
SSN, EMP, DOI, DOE, ClaimNumber, WCAB, Occupation, OtherHeading,
ReportTitle, PatientFirstName, _
PatientLastName)

If ActiveDocument.Bookmarks.Exists(bBkm) = False Then
GoTo NextBkm
End If

Set oRng = ActiveDocument.Bookmarks(bBkm).Range
oRng.Text = bText

Select Case bIndex


Case 7 'SSN

oRng.Font.Bold = False
oRng.Font.AllCaps = True
oRng.Text = "SSN:" + Chr(9) + bText

If you could tell me how to reassign oRng.Text to = bText after oRng.Text
has been inserted into the document, it would really be appreciated.

Thanks...
 
S

singeredel

I forgot the last piece of existing code which recreates the bookmarks:

If oRng.Text <> "" Then
ActiveDocument.Bookmarks.Add Name:=bBkm, Range:=oRng 'new to test
End If

NextBkm:
Next

It comes after all the Case selections.
 
G

Greg Maxey

Sub Test()
Dim bBkm As String
Dim bText As String
Dim bIndex As Long
Dim oRng As Range
For bIndex = 7 To 7 '1 To 17
bBkm = Choose(bIndex, "ReportBody", "ValdezPara", _
"ReportDate", "Addressee", "AttentionLine", _
"PatientName", "SSN", "EMP", "DOI", "DOE", _
"ClaimNumber", "WCAB", "Occupation", _
"OtherHeading", "ReportTitle", _
"NameFirst", "NameLast")
'bText = Choose(bIndex, ReportBody, ValdezPara, ReportDate1, Addressee,
AttentionLine, PatientName, SSN, EMP, DOI, DOE, ClaimNumber, WCAB,
Occupation, OtherHeading, ReportTitle, PatientFirstName, _
PatientLastName)
bText = "123-45-6789"
Set oRng = ActiveDocument.Bookmarks(bBkm).Range
oRng.Text = bText
Select Case bIndex
Case 7 'SSN
With ActiveDocument.Bookmarks
.Add Name:=bBkm, Range:=oRng
With .Item(bBkm).Range.Font
.Bold = False
.AllCaps = True
End With
End With
'The text "SSN: and tab" should be part of the document.
End Select
Next
End Sub
 
G

Greg Maxey

Actually you could insert the text SSN and tab with the macro:

Sub Test()
Dim bBkm As String
Dim bText As String
Dim bIndex As Long
Dim oRng As Range
For bIndex = 7 To 7 '1 To 17
bBkm = Choose(bIndex, "ReportBody", "ValdezPara", _
"ReportDate", "Addressee", "AttentionLine", _
"PatientName", "SSN", "EMP", "DOI", "DOE", _
"ClaimNumber", "WCAB", "Occupation", _
"OtherHeading", "ReportTitle", _
"NameFirst", "NameLast")
'bText = Choose(bIndex, ReportBody, ValdezPara, ReportDate1, Addressee,
AttentionLine, PatientName, SSN, EMP, DOI, DOE, ClaimNumber, WCAB,
Occupation, OtherHeading, ReportTitle, PatientFirstName, _
PatientLastName)
bText = "123-45-6789"
Set oRng = ActiveDocument.Bookmarks(bBkm).Range
Select Case bIndex
Case 7 'SSN
With oRng
.Text = "SSN:" & vbTab & bText
.MoveStartUntil Cset:=vbTab, Count:=wdForward
.MoveStart wdCharacter, 1
End With
With ActiveDocument.Bookmarks
.Add Name:=bBkm, Range:=oRng
With .Item(bBkm).Range.Font
.Bold = False
.AllCaps = True
End With
End With
End Select
Next
End Sub
 
S

singeredel

'The text "SSN: and tab" should be part of the document.

I don't want this part of the document unless there is data to be put there,
otherwise all the headings have to be deleted.
 
G

Greg Maxey

BTW, I don't know where your bText values are coming from. I just
substituted a string so I could work through this.

Yes that is right, redefining the bookmark range would come after the select
statement:

Sub Test()
Dim bBkm As String
Dim bText As String
Dim bIndex As Long
Dim oRng As Range
For bIndex = 7 To 7 '1 To 17
bBkm = Choose(bIndex, "ReportBody", "ValdezPara", _
"ReportDate", "Addressee", "AttentionLine", _
"PatientName", "SSN", "EMP", "DOI", "DOE", _
"ClaimNumber", "WCAB", "Occupation", _
"OtherHeading", "ReportTitle", _
"NameFirst", "NameLast")
'bText = Choose(bIndex, ReportBody, ValdezPara, ReportDate1, Addressee,
AttentionLine, PatientName, SSN, EMP, DOI, DOE, ClaimNumber, WCAB,
Occupation, OtherHeading, ReportTitle, PatientFirstName, _
PatientLastName)
bText = "123-45-6789"
Set oRng = ActiveDocument.Bookmarks(bBkm).Range
Select Case bIndex
Case 7 'SSN
With oRng
.Text = "SSN:" & vbTab & bText
.MoveStartUntil Cset:=vbTab, Count:=wdForward
.MoveStart wdCharacter, 1
End With
End Select
With ActiveDocument.Bookmarks
.Add Name:=bBkm, Range:=oRng
With .Item(bBkm).Range.Font
.Bold = False
.AllCaps = True
End With
End With
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top