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 / August 2006

Tip: Looking for answers? Try searching our database.

Questions regarding Userform/Macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ithaca - 31 Jul 2006 06:21 GMT
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...
Greg Maxey - 31 Jul 2006 13:24 GMT
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

> 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
[quoted text clipped - 89 lines]
> 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...
Ithaca - 01 Aug 2006 03:08 GMT
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

> I suppose that I would do it something like this:
>
[quoted text clipped - 155 lines]
> > 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...
Greg Maxey - 01 Aug 2006 03:16 GMT
Ryan,

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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Thanks Greg, that code looks really nice but I have a question about
> what you wrote...
[quoted text clipped - 169 lines]
>>> 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...
Ithaca - 01 Aug 2006 04:50 GMT
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?

> Ryan,
>
[quoted text clipped - 79 lines]
> >> Me.Hide
> >> End Sub
Greg Maxey - 01 Aug 2006 13:06 GMT
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

> Greg - Thanks again for the simple fix.  if only I had been thinking I might
> have seen that ;)
[quoted text clipped - 93 lines]
> > >> Me.Hide
> > >> End Sub
 
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.