Code help - I am useless and finding problems

J

JayM

I have a 3 macros Print_Thin_Duplex, Print_Thin_Duplex_x2 and
Print_Thin_Duplex_x3
The code for each is along the lines of:
Sub Print_Thin_Duplex()
PrintType = "Print_Thin_Duplex"
frmDuplex1.Show
End Sub

This opens up a form which has the following code on the Click command:

Private Sub CmdOK_Click()
Dialogs(wdDialogFilePrint).Show
frmDuplex1.Hide
If PrintType = "Print_Thin_Duplex" Then
Print_Thin
ElseIf PrintType = "Print_Thin_Duplex_x2" Then
Print_Thin
Print_Thin
ElseIf PrintType = "Print_Thin_Duplex_x3" Then
Print_Thin
Print_Thin
Print_Thin
Else: MsgBox ("Oh .......")
End If
frmDuplex2.Show
End Sub


Print_thin macro just changes the paper tray and prints the document.

Unfortunately I can't get this work. Where am I going wrong. I don't know
how to find where the error occurs. The Msgbox does pop up and so does me
frmDuplex2

Any help would be greatly appreciated.
 
J

Jonathan West

Read these 2 articles

Why variables should be declared properly
http://www.word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm

The art of defensive programming
http://www.word.mvps.org/FAQs/MacrosVBA/MaintainableCode.htm

If you follow the advice, particularly of the first article, your problem
will become immediately apparent. Rather then just tell you the problem, I
think your programming abilities will benefit from me letting you find the
problem for yourself. I promise that if you apply the lessons of the first
article to your code, the nature of the problem will become instantly
obvious.
 
J

JayM

Joanthan
Thanks for the reply. I forgot to mention I had used:
Dim PrintType as string early on
but as my code still doesn't work am looking to see what else could be
causing the problem.
JayM
 
A

Astrid

Hi Jay,
Unfortunately I can't get this work. Where am I going wrong. I don't know
how to find where the error occurs. The Msgbox does pop up and so does me
frmDuplex2

This makes sense, check your code:

Dialogs(wdDialogFilePrint).Show
frmDuplex1.Hide
If PrintType = "Print_Thin_Duplex" Then
Print_Thin
ElseIf PrintType = "Print_Thin_Duplex_x2" Then
Print_Thin
Print_Thin
ElseIf PrintType = "Print_Thin_Duplex_x3" Then
Print_Thin
Print_Thin
Print_Thin
Else
MsgBox ("Oh .......")
End If
frmDuplex2.Show

It runs through the if then else statement and when its done, it runs the
next line which is frmDuplex2.Show. This line is always executed.

If you want to know where it goes wrong, open the Visual Basic Editor and
position your cursor inside this procedure.
Now press F8 to execute the first line (it will be highlighted), press F8
again to execute the next line and so on.
If you position the cursor on the name of a variable, a tooltip will show
with the value of the variable so you will be able to check it´s value. This
way you can test what exactly happens and how your if then else statement is
evaluated.

My guess is that your variable PrintType contains another value then you use
in the If Then Else statement. ´
How exactly is this variable filled, what does it represent?

Kind regards,
Astrid
 
J

Jonathan West

JayM said:
Joanthan
Thanks for the reply. I forgot to mention I had used:
Dim PrintType as string early on
but as my code still doesn't work am looking to see what else could be
causing the problem.

Have you used Option Explicit?

I suspect not, otherwise you would have noticed that you need to declare
PrintType in each of the routines in which it appears. And that is your
problem (at least one of them). Variables declared within different routines
are different variables - even if they have the same name. So, you would
have noticed that it has to be declared in Print_Thin_Duplex, in
CmdOK_Click, and everywhere else you use the vaiable. But variables declared
within a routine don't pass their values to other rotuines.

If you want a variable whose value is shared between routines, it needs to
be declared before the first Sub or Function in a module, using the keyword
Private if the variable is shared only among the routines in that module. or
Public if the variable is shared among routines in other modules.
 

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