FilePrint in addin vs. attached template

Z

zSplash

I have intercepted my FileSave sub, and when I run my code as an attached
doc it works great; when I run it as an addin, it fails to correctly run my
"inner-code" (i.e., page 1, 2 pages; page 2, 3 pages; page 3, 1 page, etc.).
Any idea how I can correct this? I'm wondering if it's running too fast or
something...

TIA
 
J

Jezebel

There's no such thing as 'running too fast'. The most common "or something"
is a faulty reference to ActiveDocument. As suggested, post your code ...
 
Z

zSplash

Thanks, guys. Yes, here's my code. It used to work just fine (in both an
addin or as attached) until I tried to "clean it up" and broke it up into
separate, more comprehensible, subs. I'm wondering whether if I try to
capture the FilePrint event I ought to leave all the code within that sub or
something.....

Option Explicit
Sub FilePrintDefault()
' ActiveDocument.PrintOut
FilePrint
End Sub
Sub FilePrint()
' Dialogs(wdDialogFilePrint).Show
Dim docNum As String, T As Integer
Dim NumCopies As Integer
docNum = ActiveDocument.BuiltInDocumentProperties(1).Value
PrintNums
MsgBox "Back here"
End Sub
Sub PrintNums()
If docNum = "121" Then
p121
ElseIf docNum = "122" Then
p122
ElseIf docNum = "123" Then
p123
Else
Dialogs(wdDialogFilePrint).Show
End If
End Sub
Sub p121()
Dim T As Integer
On Error GoTo err_trap
reTryIt:
If ActiveDocument.CustomDocumentProperties("TOpages").Value = "two" Then
On Error GoTo 0
T = 2
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=2, Pages:="1",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=2, Pages:="2",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=3, Pages:="3",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=1, Pages:="4",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
Else
singlePage:
T = 1
singlePage_p121 T
End If
printAddlForms T
GoTo theEnd
err_trap:
If Err.Number = 5 Then 'no "TOPages" property, so one-page
ActiveDocument.CustomDocumentProperties.Add Name:="TOpages",
LinkToContent:=False, Type:=msoPropertyTypeString, Value:="one"
Err.Clear
On Error GoTo 0
GoTo singlePage
End If
theEnd:
End Sub
Sub singlePage_p121(T)
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=2, Pages:="1",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=3, Pages:="2",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=1, Pages:="3",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
End Sub
Sub printAddlForms(T) 'used in 121, 123
If T = 1 Then 'T indicates whether TO is one-page or two-pages
If ActiveDocument.CustomDocumentProperties("Print").Value = "DVFF"
Then
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=3, Pages:="4",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=2, Pages:="5",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
ElseIf ActiveDocument.CustomDocumentProperties("Print").Value = "DV"
Then
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=2, Pages:="4",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
ElseIf ActiveDocument.CustomDocumentProperties("Print").Value = "FF"
Then
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=3, Pages:="4",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
End If
ElseIf T = 2 Then
If ActiveDocument.CustomDocumentProperties("Print").Value = "DVFF"
Then
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=3, Pages:="5",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=2, Pages:="6",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
ElseIf ActiveDocument.CustomDocumentProperties("Print").Value = "DV"
Then
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=2, Pages:="5",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
ElseIf ActiveDocument.CustomDocumentProperties("Print").Value = "FF"
Then
Application.PrintOut FileName:="", Range:=wdPrintRangeOfPages,
Item:=wdPrintDocumentContent, copies:=3, Pages:="5",
pagetype:=wdPrintAllPages, manualduplexprint:=False, collate:=True,
Background:=True, printtofile:=False, printzoomcolumn:=0, printzoomrow:=0,
printzoompaperwidth:=0, printzoompaperheight:=0
End If
End If
End Sub

st.
 
J

Jezebel

Your problem is variable scoping. Variables declared in one function are not
meaningful in another. Put 'Option Explicit' at the top of your code module
to force VBA to throw an error for each undeclared variable -- as it is, it
treats all the undeclared values as zero or zero-length strings.

eg DocNum declared and set in FilePrint, then used in PrintNums ... you need
to pass these values as arguments to share them ...

Sub FilePrint ()

Dim docNum as string

docNum = "..."

PrintNums docNum

:
End Sub

Sub PrintNums(Num as string)

if Num = "121" then
...


etc
 
Z

zSplash

Thanks, Jezebel.

I did not copy the declarations in the code I sent -- they were declared
elsewhere, and the variable values "carry" and do not generate errors. That
is not the problem.

As I said before, the code works perfectly when run as an attached template;
it does not work when used as an addin.

st.
 
J

Jezebel

You must be leaving something out. The code you posted is inherently faulty.
Try a test. Add this line to FilePrint immediately before the call to
PrintNums, and to PrintNums as its first line

MsgBox "docNum = " & docNum

Now run FilePrint.
 
Z

zSplash

You were exactly right! (I had tried to "clean up" my code, but neglected
to "carry over" my variable declarations.)

Good thinking, Jezebel. Wonderful help, and THANK YOU SO MUCH!

st.
 

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