Re: List pages to print, select one page
- From: "BruceM" <bamoob@xxxxxxxxxxxxxxxx>
- Date: Mon, 17 Dec 2007 15:10:18 -0500
Thanks for the reply. I am not very familiar with user forms, but I did
manage to create one and add a list box. In many cases the user will not
print the selected pages, but will only view the document. Right now the
user navigates to the required page and clicks a custom toolbar item to run
a macro, causing the current page to print. The Help documentation,
however, is vague at best about how to actually open or run a userform.
After poking around some more I managed to create a userform with a list
box, and to populate the list box with selected bookmarks. The user form is
opened when a macro is run. I used the list box After Update event to print
the page on which the selected bookmark is located. First I call the user
form (named fusrBkm) from a macro:
fusrBkm.Show
I do it this way because the situation is somewhat more complex than I have
shown: one or more pages other than the one with the selected bookmark may
need to be printed. The macro identifies other bookmarks and sets the print
range(s) accordingly.
Other code (the list box is lstBkm):
Private Sub UserForm_Initialize()
Dim oBM As Bookmark
Dim oBMs As Bookmarks
Set oBMs = ActiveDocument.Bookmarks
For Each oBM In oBMs
If InStr(oBM.Name, "_") <> 0 Then
Me.lstBkm.AddItem (oBM.Name)
End If
Next
End Sub
I read several times in my research that a DocVariable may be used instead
of a bookmark. I thought that if I could do that I wouldn't need to worry
about filtering the list of bookmarks, but I could not figure out how to
list DocVariables in the list box. Since I don't even know if it's possible,
I didn't spend that much time trying.
Private Sub lstBkm_AfterUpdate()
Dim i As Long
Dim strBkm As String
Dim pStr As String
If fusrBkm.lstBkm.ListIndex <> -1 Then
For i = 0 To fusrBkm.lstBkm.ListCount - 1
If fusrBkm.lstBkm.Selected(i) Then
pStr = pStr + fusrBkm.lstBkm.List(i) & ", "
End If
Next i
End If
'Clean up string
pStr = Left(pStr, Len(pStr) - 2)
'PrintOut
strBkm =
ActiveDocument.Bookmarks(pStr).Range.Information(wdActiveEndAdjustedPageNumber)
Application.PrintOut Range:=wdPrintFromTo, From:=strBkm, To:=strBkm
Unload Me
I could not get the ListIndex or any other properties for lstBkm unless I
drilled down through the userform name. Also, I kept getting an error
(invaalid print range, I think) when I tried:
Application.PrintOut Range:=wdPrintRangeOfPages, Pages:=pStr
Anyhow, it seems to work. I would be interested in clearing up the
remaining questions, but it seems to be something I can work with. Thanks
again.
"Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxx> wrote in message
news:uPul$oLQIHA.1208@xxxxxxxxxxxxxxxxxxxxxxx
Bruce,
You probably don't want to clutter you list of pages the user can select
from with any other bookmark name. One way to do this is with a UserForm.
Create a UserForm that contains a ListBox that list the pages the User can
choose from (I would make it a multi-select list) and a Command Button.
Populate the Listbox with the list of pages bookmarked "Page_1", Page_2",
etc.
Here is some sample code that you could use:
Private Sub CommandButton1_Click()
Dim i As Long
Dim pStr As String
If ListBox1.ListIndex <> -1 Then
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
pStr = pStr + ListBox1.List(i) & ", "
End If
Next i
End If
'Clean up string
pStr = Left(pStr, Len(pStr) - 2)
'PrintOut
Application.PrintOut Range:=wdPrintRangeOfPages, Pages:=pStr
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim oBM As Bookmark
Dim oBMs As Bookmarks
Set oBMs = ActiveDocument.Bookmarks
For Each oBM In oBMs
If InStr(oBM.Name, "Page_") > 0 Then
Me.ListBox1.AddItem Mid(oBM.Name, 6, Len(oBM.Name) - 5)
End If
Next
End Sub
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
BruceM wrote:
I have a situation where the user may select one of several possible
pages to print. My idea (which may be off the mark, but it's what
has occurred to me) is that each of the pages would contain a
bookmark. I have adapted some code Jay Freedman posted about a year
ago to list bookmarks. I run the code as a macro.
Dim bkm As Word.Bookmark
Dim doc As Word.Document
Dim strBkm As String
Set doc = ActiveDocument
strBkm = ""
For Each bkm In doc.Bookmarks
strBkm = strBkm & bkm.Name & vbCrLf
Next
MsgBox strBkm
This produces a list of bookmarks in the document. I realize the
list as produced by this code is for display only, but what I hope is
that the user can select from the list, thereby printing the page on
which the bookmark appears. I already have worked out some code that
can identify the page number. Maybe it would be something like:
Dim b1 as Long
b1 = ActiveDocument.Bookmarks("MoreStart").Range _
.Information(wdActiveEndAdjustedPageNumber)
ActiveDocument.PrintOut _
Range:=wdPrintFromTo, From:=CStr(b1), To:=CStr(b1)
This may not be the smoothest way to accomplish what I need. For
instance, maybe I can produce a list of bookmarks, and when the user
selects one Word can go to that page, and I can use wdPrintSelection,
or something like that. Or maybe there is another option I have not
considered.
The main thing is that I would first need to have a user-selectable
list to identify the page that needs to be printed.
.
- Follow-Ups:
- Re: List pages to print, select one page
- From: Greg Maxey
- Re: List pages to print, select one page
- References:
- List pages to print, select one page
- From: BruceM
- Re: List pages to print, select one page
- From: Greg Maxey
- List pages to print, select one page
- Prev by Date: Re: Switch between drives
- Next by Date: Re: Switch between drives
- Previous by thread: Re: List pages to print, select one page
- Next by thread: Re: List pages to print, select one page
- Index(es):