Questions regarding Userform/Macro

I

Ithaca

Hello, it's been a while since I've posted my problems here :D but I'm still
having the same ones... I've got a macro that will insert a form number in a
document (where ever the cusor is) and it works well. When I tried insert
the option to omit the last page from printing I ran into issues with
variables (as in I'm not sure how to use them correctly). The code uses
multiple message boxes to get all the info required and Jezebel suggested
using a userform. I've got that created but I'm not sure how to go about
setting it up for use with my macro (assigning buttons/fields). I'm
discovering that I may be in a little over my head with this stuff so your
help would be GREATLY appreciated :D

I'll post the code for the macro, the userform, and the "omit last page
print" code that Jezebel suggested. Can you help me combine these to
hopefully work using the userform. Thanks :)

Here's the code for the macro:
Dim NumCopies As String
Dim StartNum As String
Dim Counter As Long
Dim oRng As Range

If MsgBox("The copy number will appear at the insertion point." _
& " Is the cursor at the correct position?", _
vbYesNo, "Placement") = vbNo Then End
If ActiveDocument.Saved = False Then
If MsgBox("Do you want to save any changes before" & _
" printing?", vbYesNoCancel, "Save document?") _
= vbYes Then
ActiveDocument.Save
End If
End If
StartNum = Val(InputBox("Enter the starting number.", _
"Starting Number", 60099444))
NumCopies = Val(InputBox("Enter the number of copies that" & _
" you want to print", "Copies", 1))
ActiveDocument.Bookmarks.Add Name:="CopyNum", Range:=Selection.Range
Set oRng = ActiveDocument.Bookmarks("CopyNum").Range
Counter = 0
If MsgBox("Are you sure that you want to print " _
& NumCopies & " numbered " & " copies of this document", _
vbYesNoCancel, "On your mark, get set ...?") = vbYes Then
While Counter < NumCopies
oRng.Delete
oRng.Text = StartNum
Dim sCurrentPrinter As String
Dim sFormPrint As String

sCurrentPrinter = Application.ActivePrinter

Application.ActivePrinter = "\\AMPACPRINT\05025_82_9k on NE02:"

Application.PrintOut

Application.ActivePrinter = sCurrentPrinter
StartNum = StartNum + 1
Counter = Counter + 1
Wend
End If
End Sub


Here's the code for the "omit last page":

Select Case MsgBox("Do you want to print the last page?", vbYesNoCancel)
Case vbYes
ActiveDocument.PrintOut
Case vbNo
ActiveDocument.PrintOut From:=1,
To:=ActiveDocument.BuiltInDocumentProperties("Number of pages") - 1
Case Else
End Select


And finally the userform code...

Private Sub CancelMacro_Click()

End Sub

Private Sub FormNumber_Click()

End Sub

Private Sub PrintLastPage_Click()

End Sub

Private Sub RunMacro_Click()

End Sub


If you've made it this far I appreciate it and would be forever grateful!

Also, if you could point me to some tutorials regarding this stuff so that I
don't have to bother everyone as much in the future...
 
G

Greg Maxey

I suppose that I would do it something like this:

Create a form with a StartNum Textbox, a NumCopy Textbox, a Print last
page checkbox, and a Print command button.

Call the form using:

Sub myPrint()
Dim oFrm As myPrintForm
Set oFrm = New myPrintForm
If myRouter Then
oFrm.Show
Unload oFrm
Set oFrm = Nothing
End If
End Sub

Function myRouter() As Boolean
If MsgBox("The copy number will appear at the insertion point." _
& " Is the cursor at the correct position?", _
vbYesNo, "Placement") = vbNo Then
myRouter = False
Exit Function
End If
If ActiveDocument.Saved = False Then
Select Case MsgBox("Do you want to save any changes before" & _
" printing?", vbYesNoCancel, "Save document?")
Case vbYes
myRouter = True
ActiveDocument.Save
Case vbNo
myRouter = True
Case vbCancel
myRouter = False
End Select
End If
End Function

Have the following (I took out your custom printer stuff) run on the
command button click event:

Private Sub CommandButton1_Click()
Dim NumCopies As String
Dim StartNum As String
Dim Counter As Long
Dim oRng As Range
StartNum = Me.TextBox1
NumCopies = Me.TextBox2
ActiveDocument.Bookmarks.Add Name:="CopyNum", Range:=Selection.Range
Set oRng = ActiveDocument.Bookmarks("CopyNum").Range
Counter = 0
While Counter < NumCopies
oRng.Delete
oRng.Text = StartNum
If Me.CheckBox1.Value = True Then
ActiveDocument.PrintOut
Else
ActiveDocument.PrintOut From:=1,
To:=ActiveDocument.BuiltInDocumentProperties("Number of pages") - 1
End If
StartNum = StartNum + 1
Counter = Counter + 1
Wend
Me.Hide
End Sub
 
I

Ithaca

Thanks Greg, that code looks really nice but I have a question about what you
wrote...

When I try to run that macro I get an error code pointing to "oFrm As
myPrintForm" stating that "user-defined type not defined". What needs to be
defined in the Type...End Type, or does that even need to be done?


Thanks again,

-Ryan
 
G

Greg Maxey

Ryan,

Open the VB Editor then the Userform. In not showing, display the
Properties Dialog pane. Name the UserForm myPrintForm. Sorry for the
confusion.
 
I

Ithaca

Greg - Thanks again for the simple fix. if only I had been thinking I might
have seen that ;)

Ok, I changed a few more things to get it to run right but now when I get
the the part where it omits the last page (ActiveDocument.PrintOut From:=1,
To:=ActiveDocument.BuiltInDocumentProperties("Number of pages") - 1) I get a
"Run-time error '13': Type Mismatch"... What's causing this?
 
G

Greg Maxey

Ryan,

I never tested your code to print excluding the last page. I thought
that you had that worked out. The type mismatch was due to a Long
variable when the compilier was looking for a string value.

Try:

Private Sub CommandButton1_Click()
Dim NumCopies As String
Dim StartNum As String
Dim Counter As Long
Dim oRng As Range
StartNum = Me.TextBox1
NumCopies = Me.TextBox2
ActiveDocument.Bookmarks.Add Name:="CopyNum", Range:=Selection.Range
Set oRng = ActiveDocument.Bookmarks("CopyNum").Range
Counter = 0
While Counter < NumCopies
oRng.Delete
oRng.Text = StartNum
If Me.CheckBox1.Value = True Then
ActiveDocument.PrintOut
Else
ActiveDocument.PrintOut Range:=wdPrintFromTo, From:="1", _
To:=CStr(ActiveDocument.BuiltInDocumentProperties("Number of
pages") - 1)
End If
StartNum = StartNum + 1
Counter = Counter + 1
Wend
Me.Hide
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