Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2005

Tip: Looking for answers? Try searching our database.

Inserting variables in multiple bookmarks

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
singeredel - 27 Feb 2005 19:27 GMT
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

Signature

singeredel

Howard Kaikow - 27 Feb 2005 20:29 GMT
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.

Signature

http://www.standards.com/; See Howard Kaikow's web site.

> I am trying to insert variable information into multiple bookmarks with the
> following code. However, I get the following compile error: "With Object must
[quoted text clipped - 24 lines]
> Next
> End With
singeredel - 27 Feb 2005 21:21 GMT
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??

> there's at least 2 errors:
>
[quoted text clipped - 42 lines]
> > Next
> > End With
Helmut Weber - 27 Feb 2005 21:51 GMT
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/
Jezebel - 27 Feb 2005 22:29 GMT
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) ...

> Thanks...that end if was there by mistake. Your suggestion worked;
> however,
[quoted text clipped - 50 lines]
>> > Next
>> > End With
singeredel - 27 Feb 2005 23:53 GMT
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!

> You've misunderstood what's going on with the loop. The index is looping
> through the options in the Choose() statements, not the bookmarks
[quoted text clipped - 59 lines]
> >> > Next
> >> > End With
Jezebel - 28 Feb 2005 00:37 GMT
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.)

> Yes, I discovered the error with the bIndex to bBkm. Now the text is going
> to
[quoted text clipped - 96 lines]
>> >> > Next
>> >> > End With
singeredel - 28 Feb 2005 01:31 GMT
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

> 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
[quoted text clipped - 114 lines]
> >> >> > Next
> >> >> > End With

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.