Macro to Switch Pringer & # of Copies

R

rrstudio2

I am in the process of learning some basics about visual basic and was
wondering if someone could give me a hand. I am familier with Java,
but not visual basic. I have the following simple macro

Sub Macro1()
'
' Macro1 Macro

ActivePrinter = "Test- EasyCoder 3400E"
Application.PrintOut FileName:="", Range:=wdPrintAllDocument,
Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="",
PageType:=wdPrintAllPages, _
ManualDuplexPrint:=False, Collate:=True, Background:=True,
PrintToFile:= _
False, PrintZoomColumn:=0, PrintZoomRow:=0,
PrintZoomPaperWidth:=0, _
PrintZoomPaperHeight:=0
End Sub

This macro is assigned a button that prints one label. When I hit this
tool bar button, I would like a dialg box to be pulled up which ask for
number of labels. So I assume that I would create a form, have the
text input assigned to a variable then for copies, change it to copies
= variable name. I have created forms with the wizard, but don't know
how to assign the text entered in a text box a variable that will be
used elsewhere in the marco. I would appreciate it if someone could
prodivde some direction or a good tuturial site that would address how
to do this.

Thanks,
Andrew V. Romero
 
D

Doug Robbins - Word MVP

Use:

Dim NumCopies As Long
NumCopies = InputBox("Enter the number of copies that you want to
print", , 1)
ActivePrinter = "Test- EasyCoder 3400E"
Application.PrintOut Copies:=NumCopies
End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

rrstudio2

Wow, that is really exciting!
That is much easier than I thought it would be. I need to get a book
and learn more about doing macros and visual basic code for Word.
There are some really neat things you can do, such as this, that it
seems very few people know about.

Thanks a lot, and our staff thanks you as well.
-Andrew V. Romero
 
R

rrstudio2

One more basic question, when I hit cancel on that dialog box, it
displays an error saying type mismatch. I don't quite get that.

Thanks,
Andrew V. Romero
 
S

Shauna Kelly

Hi Andrew

The Inputbox command returns a string. The user could, for example, type
"Good morning, Miss Marple" into the box. If the user cancels, then the
InputBox command returns an empty string, "".

In the code, the result of InputBox is allocated to NumCopies, and it was
defined as a Long (meaning a long integer).

VBA will cheerfully convert things, in this case from a string to a number,
without telling you. So if the result of the InputBox = "2", then VBA will
convert it to the number 2, NumCopies will hold the number 2, and all is
well.

But if I type "Good morning, Miss Marple" into the box, the InputBox will
return that string, and VBA has no way to convert "Good morning, Miss
Marple" into a number. So it throws up its hands in confusion and causes an
error. If NumCopies = "", it throws the same error.

So, you need some way to avoid the error. It would also be nice to avoid
changing the ActivePrinter if the user cancelled. Here's one way:

Dim strNumCopies As String
Dim lngNumCopies As Long

strNumCopies = InputBox("Enter the number of copies that you want to
Print ", , "1")
If IsNumeric(strNumCopies) Then
lngNumCopies = CLng(strNumCopies) 'convert the string to a number
ActivePrinter = "Test- EasyCoder 3400E"
Application.PrintOut Copies:=lngNumCopies
End If

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
D

Doug Robbins - Word MVP

It's because I declared NumCopies as Long so it expects a numeric value.

Use:

Dim NumCopies As Long
NumCopies = Val(InputBox("Enter the number of copies that you want to
Print ", , 1))
If NumCopies = 0 Then Exit Sub
ActivePrinter = "Test- EasyCoder 3400E"
Application.PrintOut Copies:=NumCopies


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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