Inserting variables in multiple bookmarks

S

singeredel

I am trying to insert variable information into multiple bookmarks with the
following code. However, I get the following compile error: "With Object must
be user-defined type, Object, or Variant." Can anyone tell me what I am
missing? Is there is a better way to do this? Thanks!

Dim bBkm As String
Dim bText As String
Dim bIndex As Long

For bIndex = 1 To 5
bBkm = Choose(bIndex, "Address1", "Address2", "Address3", "Address4",
"Address5")
bText = Choose(bIndex, ReportAddress1$, ReportAddress2$,
ReportAddress3$, _
ReportAddress4$, ReportAddress5$)
If bIndex = 4 Or bIndex = 5 Then
Selection.Paragraphs.Style = "Normal"
End If
With ActiveDocument.Bookmarks(bIndex).Range.Text = bText
If bIndex = 4 Or bIndex = 5 Then
If bText = "" Then
ActiveDocument.Bookmarks(bIndex).Select
Selection.Delete Count:=1
End If
End If
End With
Next
End With
 
H

Howard Kaikow

there's at least 2 errors:

1. There's an "End With" without a corresponding "With".
2. The following is an invalid statement

With ActiveDocument.Bookmarks(bIndex).Range.Text = bText

Change it to

ActiveDocument.Bookmarks(bIndex).Range.Text = bText

Then delete both "End With".

Take a look at Steve Roman's book Writing Word Macros and see
http://www.standards,com/index.html?WordVBABooks.
 
S

singeredel

Thanks...that end if was there by mistake. Your suggestion worked; however,
the code did not insert the text where the bookmarks were but all over the
place where there were other booksmarks. Is there some other statements
needed to direct the cursor to the proper bookmark to insert the text??
 
H

Helmut Weber

Hi,

if you need the sequence of bookmarks
from the doc's start to the doc's end, use
ActiveDocument.Range.Bookmarks(bIndex).Range.Text
instead of
ActiveDocument.Bookmarks(bIndex).Range.Text
otherwise the bookmarks may be addressed in the sequence
they were created, probably. Can't check it right now.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

Jezebel

You've misunderstood what's going on with the loop. The index is looping
through the options in the Choose() statements, not the bookmarks
themselves -- The idea is that you select the bookmark by name, not by
index --

In place of ActiveDocument.Bookmarks(bIndex) ...

you should have ActiveDocument.Bookmarks(bBkm) ...
 
S

singeredel

Yes, I discovered the error with the bIndex to bBkm. Now the text is going to
the correct location, but I cannot get the text formatting to work. This is
what I now have. I am sure this is all wrong.

Dim bBkm As String
Dim bText As String
Dim bIndex As Long

For bIndex = 1 To 5
bBkm = Choose(bIndex, "ReportAddress1", "ReportAddress2",
"ReportAddress3", "ReportAddress4", "ReportAddress5")
bText = Choose(bIndex, ReportAddress1$, ReportAddress2$,
ReportAddress3$, ReportAddress4$, ReportAddress5$)

Dim oRng As Range
Set oRng = ActiveDocument.Bookmarks(bBkm).Range
If bIndex = 4 Or bIndex = 5 Then
Selection.Paragraphs.Style = "Normal"
End If
If bIndex = 1 Then
oRng.Font.AllCaps = True
End If
oRng.InsertAfter bText
If bIndex = 4 Or bIndex = 5 Then
If bText = "" Then
Selection.Delete Count:=1
End If
End If
Next

Any help on this would be appreciated also. Thank you!
 
J

Jezebel

The problem is that you are formatting the selection, without selecting the
range you want to work on. Either select the range, or, better, apply the
formatting directly --

Set oRng = ActiveDocument.Bookmarks(bBkm).Range
If bIndex = 4 Or bIndex = 5 Then
oRng.Paragraphs.Style = "Normal"
End If

Similarly, your Selection.Delete statement will be deleting the wrong thing.

If (bIndex = 4 Or bIndex = 5) and len(bText) = 0 Then
oRng.Delete Count:=1
End If

(not sure what you're actually trying to do here so I don't know if this
will work, but deleting the Selection is obviously wrong.)
 
S

singeredel

Well, maybe I am not understanding, but I am still unable to get the text to
be AllCaps. Don't know about the "Normal" style yet, as I have not run a test
on this. This is what I now have:

Dim bBkm As String
Dim bText As String
Dim bIndex As Long

For bIndex = 1 To 5
bBkm = Choose(bIndex, "ReportAddress1", "ReportAddress2",
"ReportAddress3", "ReportAddress4", "ReportAddress5")
bText = Choose(bIndex, ReportAddress1$, ReportAddress2$,
ReportAddress3$, ReportAddress4$, ReportAddress5$)

Dim oRng As Range

Set oRng = ActiveDocument.Bookmarks(bBkm).Range

oRng.Select
If bIndex = 4 Or bIndex = 5 Then
oRng.Paragraphs.Style = "Normal"
End If
If bIndex = 1 Then
oRng.Font.AllCaps = True
End If
oRng.InsertAfter bText
If bIndex = (4 Or bIndex = 5) And Len(bText) = 0 Then
Selection.Delete Count:=1
End If
Next
 

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