![]() |
#11
|
|||
|
|||
![]()
Well done. I'm impressed with your ability to extend the userform based on previous instructions.
Its very easy to update the code to validate the page range you want. The first trick is to use the Trim and Replace functions to get rid of any commas and hyphens and multiple spaces. You can then split the list of page numbers separated by spaces using the split function to give an array of page numbers You can then test each page number in turn for it being numeric. This is the updated code for the form_validates_ok function If it were for my personal use then there would be some further abstraction as the validation is quite repetitive. Code:
Private Function form_validates_ok() As Boolean Dim txt_pages As String Dim pages() As String Dim a_page_number As Variant ' must be a variant for loop control If Not IsNumeric(Trim(Me.txtCopies)) Then MsgBox "The number of copies should be a number", vbOKOnly Me.txtCopies.SetFocus Me.txtCopies.SelLength = Len(Me.txtCopies) form_validates_ok = False Exit Function End If If Not IsNumeric(Trim(Me.txtStartNumber)) Then MsgBox "The start number should be a number", vbOKOnly Me.txtStartNumber.SetFocus Me.txtStartNumber.SelLength = Len(Me.txtStartNumber) form_validates_ok = False Exit Function End If ' convert the txtPages string into an array of page numbers in array pages txt_pages = Trim(Me.txtPages) txt_pages = replace(txt_pages, ",", " ") txt_pages = replace(txt_pages, "-", " ") Do While InStr(txt_pages, " ") > 0 ' checking for the presence of two spaves adjacent to each other txt_pages = replace(txt_pages, " ", " ") Loop pages = split(txt_pages, " ") ' Note the absence of () when assigning to an array For Each a_page_number In pages If Not IsNumeric(a_page_number) Then MsgBox "The pages required should be a number", vbOKOnly Me.txtPages.SetFocus Me.txtPages.SelLength = Len(Me.txtPages) form_validates_ok = False Exit Function End If Next form_validates_ok = True End Function Code:
ActiveDocument.PrintOut copies:=1, pages:=my_form.txtPages |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Help with Case and Select case | brent chadwick | Word VBA | 34 | 10-18-2015 02:13 PM |
Replace & case | Jennifer Murphy | Word | 1 | 02-11-2013 03:26 AM |
![]() |
kyjac85 | Word VBA | 13 | 09-20-2012 05:00 PM |
Problem with the sendkeys in Win7 | vidyapakki | Excel Programming | 1 | 05-07-2012 11:10 PM |
![]() |
davers | Word | 1 | 04-30-2009 12:41 PM |