PDA

View Full Version : Solved: Before Print



daniels012
01-29-2007, 10:02 AM
I am trying to add a Macro into Word to execute when someone prints the document. The macro message would say "Would you like to change the text in this file?" Yes, No, Cancel"
If Yes, Go to the Doc and edit text
If No, Go ahead and Print
If Cancel, take me back to the sheet and end Macro.

Any help is greatly appreciated.
Michael

fumei
01-30-2007, 06:55 AM
1. It seems Yes, and Cancel are the same.

2. Why are you doing this?

3. You can intercept the Print command with your own procedure. Put a procedure named FilePrint into the file.

daniels012
01-30-2007, 08:17 AM
1. That is true "Yes" and "Cancel" are the same.

2. Can you give me some lead way as to how and what to write for my own proceedure:banghead: . Sorry, I am very green when it comes to VBA.:bug:

Thank You,:friends:
Michael

fumei
01-30-2007, 11:34 AM
1. I asked already, but...why do you want to do this?

2. Look up Msgbox Function in Help to get an idea of how to do a message box with Yes, No, Cancel.

3. As No means Print, record a macro that prints, and look at the code. You will use it in the logic for "No."

4. Write your procedures in the ThisDocument module of the document. That means it will ONLY apply for THAT document.

5. If you want it to apply to ALL Word documents - and I do not recommend this - then put the procedures into Normal.dot.

6. You will need to cover both the Print button AND the File > Print menu command. These are two separate procedures in Word.

Print button = FilePrintDefault
File > Print = FilePrint.

I would recommend writing ONE procedure to process your messagebox, and call it from both the printing procedures. Something like:Sub FilePrint()
' the File > Print command
Call PrintIntercept
End Sub

Sub Fileprintdefault()
' the Print button
Call PrintIntercept
End Sub

Sub PrintIntercept()
' I am not posting the code
' look in Help under MsgBox Function
' it will show how to use a messagebox with
" Yes and No
' IF the answer is "No" then
' the basic printing of a document
ActiveDocument.PrintOut
Else
Exit Sub
End If
End Sub

daniels012
01-30-2007, 12:22 PM
Thank You very much, I have been searching for an answer on this for 2 days. Here is the code I have used, which works great by the way!!:clap:


Sub FilePrint()
' the File > Print command
Call PrintIntercept
End Sub

Sub Fileprintdefault()
' the Print button
Call PrintIntercept
End Sub

Sub PrintIntercept()

Response = MsgBox("Do you want to change the info on the sheet", vbYesNo)
If Response = vbNo Then
ActiveDocument.PrintOut
Else
Exit Sub
End If
End Sub


The reason for my use... I print postcards to customers with different images. I use the same text on the back of the form for the different pictures on the front. Sometimes I do change the text. This is what I would like the reminder before I print, so I can change the text If I would like and before it prints something wrong to a different person.

Thank You for your help,
Michael

fumei
01-30-2007, 12:48 PM
Bingo! You code is bang on.

Except.....you do not declare response as a variable. That indicates to me you are NOT using Option Explicit.

Do so. Start now. In the VBE , go Tools > Options, and on the Editor tab, check "Require Variable Declaration".

This will not put Option Explicit into any existing code modules, but will automatically put it into any new ones.

In the long run, you will be glad you did. What this does is, well, require variable declarations. You will not be permitted to use variables unless you explicitly declare them first.

mdmackillop
01-30-2007, 01:31 PM
... and in this case
Dim Response As Long

lucas
01-30-2007, 02:00 PM
Hey Malcolm, we need an article on declaring variables...I don't think there is one there now but there is lots of confusion about this topic. I would have thought response as string....but it gets converted to long anyway right????

mdmackillop
01-30-2007, 02:06 PM
Hi Steve,
I checked before I posted, as I wasn't certain myself. I agree, an article is a good idea. Are you up for it?

MsgBox Function

Displays a message in a dialog box, waits for the user to click a button, and returns an Integer indicating which button the user clicked.

lucas
01-30-2007, 02:07 PM
This before print procedure for Word might be nice to have in the kb.......

lucas
01-30-2007, 02:10 PM
No, I'm really not up to speed on it Malcolm but I would find it very useful if someone with the skills would take it on. I would have to do a lot of research and I might still get it wrong.....:eek:

fumei
01-31-2007, 09:53 AM
Response can be Integer, it does not have to be Long. As an Integer variable takes 2 bytes, and a Long variable takes 4 bytes....you can save 2 bytes! AND, if you really want to be fussy, technically, you could use Byte for Response - as Byte can be 0 to 255. Byte only uses 1 byte for storage, so you could save 3 bytes of memory addressing.

The only return values for MsgBox() are:

vbOK = 1
vbCancel = 2
vbAbort = 3
vbRetry = 4
vbIgnore = 5
vbYes = 6
vbNo = 7

The button Yes/No = 4

So numerically, the Sub could be:Sub PrintIntercept()
Dim Response As Byte
Response = _
MsgBox("Do you want to change the info on the sheet", 4)
If Response = 7 Then
ActiveDocument.PrintOut
Else
Exit Sub
End If
End Sub

lucas
01-31-2007, 10:10 AM
There's another great article.....msgbox returns and how to deal with the response.......

fumei
01-31-2007, 10:20 AM
And just to be clear....

Msgbox is an instruction that displays a string.

Msgbox() is a FUNCTION - the parenthesis gives that away - that returns an integer value.

Which is why if you executed:MsgBox("Do you want to change the info on the sheet", 4)by itself, you will get a compiling syntax error. It requires an =, something (a variable) must = Msgbox() - as Msgbox() returns a number. Those numbers can be 1 to 7, which can in fact be handled by a Byte data type.

mdmackillop
01-31-2007, 11:00 AM
Integer v Long

Microsoft Office XP Developer
The Integer, Long, and Byte Data Types
Three data types in Microsoft? Visual Basic? for Applications (VBA) can represent integers, or whole numbers: the Integer, Long, and Byte data types. Of these, the Integer and Long types are the ones you are most likely to use regularly.
The Integer and Long data types can both hold positive or negative values. The difference between them is their size: Integer variables can hold values between -32,768 and 32,767, while Long variables can range from -2,147,483,648 to 2,147,483,647. Traditionally, VBA programmers have used integers to hold small numbers, because they required less memory. In recent versions, however, VBA converts all integer values to type Long, even if they are declared as type Integer. Therefore, there is no longer a performance advantage to using Integer variables; in fact, Long variables might be slightly faster because VBA does not have to convert them.
The Byte data type can hold positive values from 0 to 255. A Byte variable requires only a single byte of memory, so it is very efficient. You can use a Byte variable to hold an Integer value if you know that value will never be greater than 255. However, the Byte data type is typically used for working with strings. For some string operations, converting the string to an array of bytes can significantly enhance performance.

fumei
01-31-2007, 01:06 PM
I stand corrected. I did not realize that VBA converts Integer to Long.

I guess they decided...what the heck, no one is going to notice the 2 byte difference with machines with gigabyte levels of memory. Which is of course true.

Thanks Malcolm. I will stop using Integer. Seems kind of ironic as there is still a valid use for Byte.

mdmackillop
01-31-2007, 01:11 PM
Glad to help Gerry, and Byte seems to work as a value for MsgBox, even although Help says it is Integer, so you can save 3 bytes. Maybe you could collect a few and sell them on Ebay!

fumei
01-31-2007, 01:28 PM
Byte is an integer.

Actually, I have collected quite a few already. I am just waiting to fill up the 40 Gb hard drive I am keeping them in. I figure I can make a sale better if I can offer a bulk discount.

TonyJollans
02-02-2007, 07:11 AM
Personally I prefer to use the Enumeration

Dim Response As VbMsgBoxResult


The returned value is a Long - if you define it differently, conversion has to take place.

What that quote from MSDN doesn't really explain is when and where the conversions betwen Integers and Longs take place. Integers occupy two bytes of memory in your compiled VBA module (although alignment of other variables to appropriate boundaries might negate the space saving). Any conversion that takes place is behind the scenes when the variables are operated on and, whilst I can guess what happens based on my knowledge of IBM architecture, I can't be certain of what it is and how it may affect anything. It would, however, seem to make sense to stick with Longs.