Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #11  
Old 05-29-2018, 02:47 AM
slaycock slaycock is offline SENDKEYS with vbYesNoCancel and Select Case Windows 7 64bit SENDKEYS with vbYesNoCancel and Select Case Office 2016
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

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
You will also need to amend the printout statement to use the page ranges in .txtxPages

Code:
ActiveDocument.PrintOut copies:=1, pages:=my_form.txtPages
Reply With Quote
 



Similar Threads
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
SENDKEYS with vbYesNoCancel and Select Case A macro that can insert FILENAME, sendkeys CTRL ALT T, paste clipboard, and nextline 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
SENDKEYS with vbYesNoCancel and Select Case From all UPPER CASE to Proper Case davers Word 1 04-30-2009 12:41 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:17 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft