PDA

View Full Version : Variables to change the pages to be printed



francozola25
06-10-2008, 10:22 AM
Hi

I have modified my previous post.

I was wondering if someone could help me with the following problem. I currently have a macro hard coded to print a document
pages 1-19 white paper (back to back)[white paper placed in Tray2]
pages 20-52 green paper(back to back)[green paper placed in Tray4]
pages 53-62 singles pages [white paper placed in Tray2]

The problem i face is that although hard coded and functioning correctly, i know down the line, extra pages will be added to the document. Would anyone how to create a variable for Pages and somehow be able to modify these pages To and From with a Userform?

Thanks



Sub Print()
'
' Macro1 Macro
' Macro recorded 6/10/2008 by pmcdonal
'
With ActiveDocument
.Unprotect Password:="protect"
ActivePrinter = "Printer Duplex"
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="1-19", PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True, Background:= _
False, PrintToFile:=False, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
ActivePrinter = "Printer Duplex"
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="20-52", PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True, Background:= _
False, PrintToFile:=False, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
ActivePrinter = "Printer Single"
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="53-62", PageType:= _
wdPrintAllPages, ManualDuplexPrint:=False, Collate:=True, Background:= _
False, PrintToFile:=False, PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
.Protect wdAllowOnlyReading, Password:="protect"
End With
End Sub

lucas
06-10-2008, 11:02 AM
Hi francozola25,
In future, please do not delete you threads or posts. If you are not getting the attention you need then just post in the thread again and clarify or ask for help.

fumei
06-10-2008, 11:26 AM
Pages:="1-19",
Pages:="20-52",
Pages:="53-62",

These are just strings. Therefore you can use string variables.

Dim FirstChunk As String
Dim SecondChunk As String
Dim LastChunk As String

' then set the values. Say txtFirstChunk is a textbox
' on your userform. And say, that value is "1-19"
' txtSecondChunk is also a textbox and its value is
' "20-52"

FirstChunk = txtFirstChunk.Text
SecondChunk = txtSecondChunk.Text
LastChunk = txtLastCunk.Text

' then use in your printer code
Pages:= FirstChunk,
Pages:= SecondChunk,
Pages:= LastChunk,



OR, you could do it as an array:

Option Explicit

Private Sub cmdExit_Click()
Unload Me
End Sub

Private Sub cmdListEm_Click()
Dim Chunks()
Dim TB_Names()
Dim var, var2
Dim j As Long
Dim strShowList As String

' make an array of relevant textboxes
TB_Names = Array("TextBox1", "TextBox2", _
"TextBox3")

' build array - Chunks - of THOSE textbox values
For var = 0 To UBound(TB_Names)
ReDim Preserve Chunks(j)
Chunks(var) = Me.Controls(TB_Names(var)).Text
j = j + 1
Next

' build a string of those values

' for demo purposes only
' you would want to use each separately
For var2 = 0 To UBound(TB_Names)
strShowList = strShowList & Chunks(var2) & _
vbCrLf
Next

' make the Label caption show the
' textbox values
Label1.Caption = strShowList
End Sub

Demo attached (for the userform example). The userform build an array of the textboxes, then builds an array of those textbox values.

In any case, the Pages:= for the print instruction is a string, and can therefore be a string variable. How you get the string to be any specific value is up to you.

For the demo attached, I have the userform display on Document_Open. Type:

1-19 (no quotes)
Tab - which moves to the next textbox
20-52 (no quotes)
Tab - which moves to the next textbox
53-62 (no quotes)
Tab - which moves to the List commandbutton
Enter - which puts the values of the textbox into a Label.

As Pages will use a string variable, you do NOT want to enter the " into the textboxes.

1-19

NOT

"1-19"

francozola25
06-10-2008, 12:00 PM
Apologies Lucas. It was just i didn't want to anyone else time with it!

Fumei

Thanks for your reply

Rather than having the macro on startup, i think i want just place it as a button.

With the array example, would i leave my current macro there and call it from the array macro. I am little confused.

fumei
06-10-2008, 01:03 PM
"I am little confused."

So am I. I do not know what you are asking.

"With the array example, would i leave my current macro there and call it from the array macro."

I have no idea, as i have no idea what your macro is, or does. The demo is just that, a demo. You asked about variables for the Pages:=xxxx.

I showed how you could possibly do that.

I have no idea where you are calling the printing code from. The userform? Well, if so, then just use variables. If the values come from a textbox:

Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, _
Item:= wdPrintDocumentContent, Copies:=1, _
Pages:= TextBox1.Text, PageType:= wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, _
Background:= False, PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0




OR, if you are NOT doing the printing code from the userform, then you could take the values from the userform and put them into a global string variable - that is, a Public variable in a standard module, not the userform module.

Then you could use that string variable any time, any where, you want.


BTW: you probably do not need some of those printer parameters. If not...get rid of them.

francozola25
06-12-2008, 01:58 AM
Hey Fumei

I have constructed the macro like so:

I now have the exit button to call the printing of the document

Sub cmdExit_Click()

Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, _
Item:=wdPrintDocumentContent, Copies:=1, _
Pages:=TextBox1.Text, PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, _
Background:=False, PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, _
Item:=wdPrintDocumentContent, Copies:=1, _
Pages:=TextBox2.Text, PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, _
Background:=False, PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages, _
Item:=wdPrintDocumentContent, Copies:=1, _
Pages:=TextBox3.Text, PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, _
Background:=False, PrintToFile:=False, _
PrintZoomColumn:=0, PrintZoomRow:=0, _
PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
Unload Me
End Sub

Sub Emamectin1()
Dim Chunks()
Dim TB_Names()
Dim var, var2
Dim j As Long
Dim strShowList As String

' make an array of relevant textboxes
TB_Names = Array("TextBox1", "TextBox2", _
"TextBox3")

' build array of THOSE textbox values
For var = 0 To UBound(TB_Names)
ReDim Preserve Chunks(j)
Chunks(var) = Me.Controls(TB_Names(var)).Text
j = j + 1
Next
' build a string of those values
' for demo purposes only
' you would want to use each separately
For var2 = 0 To UBound(TB_Names)
strShowList = strShowList & Chunks(var2) & _
vbCrLf
Next
' make the Label caption show the
' textbox values
Label1.Caption = strShowList
End Sub

Sub TextBox1_Enter()
TextBox1.Value = "1-33"
TextBox2.Value = "33-37"
TextBox3.Value = "48-65"
End Sub



Could i ask what is the function of the list?

What appears is the default values for TextBox1 : 1-33, TextBox2 : 33-37, TextBox3 : 48-65

I want to leave these at the moment but is whereby a user can change the values and click a set default, then the default value will appear as this from now, say an extra page was added 1-33 making 1-34.

Many Thanks

fumei
06-12-2008, 10:12 AM
You are asking about two separate things.

On one hand you asked how you can use variables for the Pages:= values. I have showed you.

Now you are asking about setting the default values for textboxes on a userform.

Fine...then do that. What are you doing with this:
Sub TextBox1_Enter()
TextBox1.Value = "1-33"
TextBox2.Value = "33-37"
TextBox3.Value = "48-65"
End Sub

Why are you doing this? This is not the same thing as the user entering values into the textboxes, and you wanted to change the default values to THOSE.

I am not sure about this, as the values entered by the users are values at run-time.