OLK2K3: Standard Field Printing Issue

B

Bill Billmire

Sue,

I am still having problems getting the form I created to print. I have made
some progress (via trial and error) from where I was about two weeks ago, but
problems still exist that prevent me from calling this complete. The
original code example was published in:
(http://www.outlookexchange.com/articles/home/lengho01.asp)
Reference previous thread dated 12/9/2004, Subject: OLK2K3: Form Printing
Assistance (Standard Field).
I added some code in the loop to handle the "standard field" and the loop
now iterates completely through the entire collection of bookmarks. The
three remaining issues are:
1) The standard field (Item.SentOn) is still not being processed as I want.
(i.e., being printed on the output (page).

2) Using msgboxes I can validate that the loop processes all the bookmarks,
however, only the last bookmark gets printed on the output (page).

3) Using the code line: "objWord.PrintOut Background = True" causes the
VBScript to error out with: Microsoft VBScript runtime error: Variable is
undefined: 'Background'. Since I am using Office 2003, I am wondering if the
"Background" parameter, or it's syntax has changed in usage from previous
versions of Work/Outlook?

Latest Code...

'------------------Printing Routine--------------------
Dim objWord
Dim strTemplate
Dim objDocs
Dim objDoc
Dim objMark
Dim mybklist
Dim counter
Dim objField

Sub cmdPrint_Click()

Set objWord = CreateObject("Word.Application")

' Put the name of the Word template that contains the bookmarks
strTemplate = "OSR.dot"

' Location of Word template; could be on a shared LAN
strTemplate = "\\ivory\forms\" & strTemplate

Set objDocs = objWord.Documents
objDocs.Add strTemplate
Set mybklist = objWord.ActiveDocument.Bookmarks

For counter = 1 to mybklist.count
Dim strField
Dim strField1
Set objMark = objWord.ActiveDocument.Bookmarks(counter)
strField = objMark.Name
If strField = "SentField" then
strField = CStr(Item.SentOn)
msgbox strField 'temporary while debugging
Else
msgbox strField 'temporary while debugging
strField1 = Item.UserProperties(strField)
msgbox strField1 'temporary while debugging
Set objDoc = objDocs.Add(strTemplate)
Set objMark = objWord.ActiveDocument.Bookmarks(counter)
End If
objMark.Range.InsertAfter strField1
Next

objWord.PrintOut Background = True
objWord.Quit(0)

End Sub

Thanks for your help!

Bill Billmire -
(e-mail address removed)
 
S

Sue Mosher [MVP-Outlook]

I added some code in the loop to handle the "standard field" and the loop
now iterates completely through the entire collection of bookmarks. The
three remaining issues are:
1) The standard field (Item.SentOn) is still not being processed as I
want.
(i.e., being printed on the output (page).

What is the name of the bookmark where you want to insert this text? What
happens when the code reaches that bookmark?
2) Using msgboxes I can validate that the loop processes all the
bookmarks,
however, only the last bookmark gets printed on the output (page).

Your statement to insert text at a bookmark:

objMark.Range.InsertAfter strField1

appears outside your For ... Next loop. If you want it to apply to each
bookmark, it needs to be inside the loop.

3) Using the code line: "objWord.PrintOut Background = True" causes the
VBScript to error out with: Microsoft VBScript runtime error: Variable is
undefined: 'Background'.

That statement is not valid in any version of Word. The correct syntax in
VBScript code would be:

objWord.PrintOut True

since Background is the first parameter for the PrintOut method.
 
B

Bill Billmire

The name of the Bookmark is "SentField" in the Word Template.
When the code reaches that item "SentField", right now, it hits the Else
statement and processes the last bookmark - in effect skipping that
"SentField" bookmark. If I try to add code to further process the item, I
get an "Object variable not set"... eventhough, I have converted the
(Item.SentOn) to string data using the CStr function. Which makes it look
just like all the rest of the string type bookmarks.

The "objMark.Range.InsertAfter strField1" is within the For... Next... Loop.
Please re-check.

Thanks for the info on the objWord.PrintOut {True} statement.

Thanks much!

Bill Billmire -
(e-mail address removed)
 
S

Sue Mosher [MVP-Outlook]

The name of the Bookmark is "SentField" in the Word Template.
When the code reaches that item "SentField", right now, it hits the Else
statement and processes the last bookmark - in effect skipping that
"SentField" bookmark.

If you have these statements:

strField = objMark.Name
If strField = "SentField" Then
' some code
Else
' more code
End If

but the If block is being skipped and the code is executing the Else block
instead, then the name of the bookmark must not be SentField. Check for
leading or trailing spaces. Use the Locals, WAtch, or Immediate window or a
MsgBox or Debug.Print statement to check the **exact** value. One way would
be:

msgbox strField & vbCrLf & (strField = "SentField")

The second line of the message popup should be True or False depending on
the value of strField.

You'll want to change this statement:

strField = CStr(Item.SentOn)

to

strField1 = CStr(Item.SentOn)

since strField1 is the value you're trying to put into the template.

Also, these statements should appear at the beginning of your procedure, not
inside the loop:

Dim strField
Dim strField1

since you need to declare variables just once. It would also be preferable
to declare all the other variables inside the procedure, rather than at the
module level, since you are using them only within the one procedure.
The "objMark.Range.InsertAfter strField1" is within the For... Next...
Loop.

Sorry, my bad. I have an appointment with the eye doctor on Saturday. The
real problem is that you have these statements inside the loop:

Set objDoc = objDocs.Add(strTemplate)
Set objMark = objWord.ActiveDocument.Bookmarks(counter)

so you're creating a new document for every bookmark, but are only printing.
Take both those statements out of the loop. They duplicate statements that
precede the loop.

I would use objDoc.PrintOut instead of objWord.PrintOut.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



If I try to add code to further process the item, I
 
B

Bill Billmire

OK and Eureka!

The code now works!

Working version below...
Note: when I tried changing "objWord.PrintOut" to "objDoc.PrintOut" it
errored out, so I changed it back.

'----------------------Printing Routine------------------------
Dim objWord
Dim strTemplate
Dim strField
Dim strField1
Dim objDocs
Dim objDoc
Dim objMark
Dim mybklist
Dim counter
Dim objField

Sub cmdPrint_Click()

Set objWord = CreateObject("Word.Application")

' Put the name of the Word template that contains the bookmarks
strTemplate = "OSR.dot"

' Location of Word template; could be on a shared LAN
strTemplate = "\\ivory\forms\" & strTemplate

Set objDocs = objWord.Documents
objDocs.Add strTemplate
Set mybklist = objWord.ActiveDocument.Bookmarks

For counter = 1 to mybklist.count
Set objMark = objWord.ActiveDocument.Bookmarks(counter)
strField = objMark.Name
If strField = "SentField" then
strField1 = CStr(Item.SentOn)
Else
strField1 = Item.UserProperties(strField)
End If
objMark.Range.InsertBefore strField1
Next

objWord.PrintOut
objWord.Quit(0)

End Sub

Thanks!

Bill Billmire -
(e-mail address removed)
 
S

Sue Mosher [MVP-Outlook]

Note: when I tried changing "objWord.PrintOut" to "objDoc.PrintOut" it
errored out, so I changed it back.

That's because you never set a Word document. Instead of using
ActiveDocument, TELL WORD exactly what document you want to use:

Set objDoc = objDocs.Add(strTemplate)
Set mybklist = objDoc.Bookmarks

I thought we discussed in the other thread?

So what was the problem with the bookmark?
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
B

Bill Billmire

The problem with the "bookmark" was, it was correctly processing the
bookmark; that is, finding it in the Word Template and correctly assigning
the string converted date to the variable. I just had the wrong assignment
in the next line, which you correctly identified {strField1 =
CStr(Item.SentOn)} adding the 1 to strField fixed it.

I will revisit the other thread and see where I missed the
comments/suggestions on TELLING WORD exactly what document I want to use and
make the necessary mods.

Now that I am testing this on additional machines, I have noticed that it
doesn't print on every machine tested... such as machines with locally
attached printers (on LPT1:), and other cases that I am still
investigating??? Any suggestions?

This code would be great for your next book; Printing outlook data in a
For... Next... loop and handing an Outlook Standard field.

Thanks again!

Bill Billmire -
(e-mail address removed)
 
S

Sue Mosher [MVP-Outlook]

Word (unlike Outlook) does have an Application.ActivePrinter property
(string). Maybe the thing to do is check its value on the problem machines.
 
B

Bill Billmire

Sue - Are you suggesting that I add that function, "Application.ActivePrinter
property(string)" to the Outlook Printing code to check the status of the
Default Printer? Please explain some more...

Thanks,

Bill Billmire -
(e-mail address removed)
 
S

Sue Mosher [MVP-Outlook]

I'm suggesting that you see if Application.ActivePrinter returns the name of
the printer you want the user to print to.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
B

Bill Billmire

Sue,

How do I use that function in the printing code?
Do you have any samples/examples for reference that I could use?
Should the test be at the being of the routine or near the end?

Thanks,

Bill Billmire -
(e-mail address removed)
 
B

Bill Billmire

Sue,

Also, I am attempting to modify the code to use the...

That's because you never set a Word document. Instead of using
ActiveDocument, TELL WORD exactly what document you want to use:

Set objDoc = objDocs.Add(strTemplate)
Set mybklist = objDoc.Bookmarks

I thought we discussed in the other thread?

Now the code is erroring out in the "Set objDoc = objDocs.Add(strTemplate)"
line with "Object required: 'objDocs'.

'----------------------Printing Routine------------------------
Dim objWord
Dim strTemplate
Dim strField
Dim strField1
Dim objDocs
Dim objDoc
Dim objMark
Dim mybklist
Dim counter
Dim objField

Sub cmdPrint_Click()

Set objWord = CreateObject("Word.Application")

' Put the name of the Word template that contains the bookmarks
strTemplate = "OSR.dot"

' Location of Word template; could be on a shared LAN
strTemplate = "\\ivory\forms\" & strTemplate

Set objDoc = objDocs.Add(strTemplate)
Set mybklist = objDoc.Bookmarks

For counter = 1 to mybklist.count
Set objMark = objDoc.Bookmarks(counter)
strField = objMark.Name
If strField = "SentField" then
strField1 = CStr(Item.SentOn)
Else
strField1 = Item.UserProperties(strField)
End If
objMark.Range.InsertBefore strField1
Next

objDoc.PrintOut
objDoc.Quit(0)

End Sub
 
S

Sue Mosher [MVP-Outlook]

I'd just thrown in a MsgBox as a diagnostic to help you see what's happening
on the machines having a problem.

MsgBox "Printing to " & Application.ActivePrinter

HINT: You need to start trying some of these things yourself.
Experimentation is the best way to learn.
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

Sue Mosher [MVP-Outlook]

Didn't you have a Set objDocs instantiatiation statement in your previous
routine? If so, you need to restore it. If not, you can use:

objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add(strTemplate)
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
B

Bill Billmire

Thanks Sue; believe me, I really appreciate your time and expertise in
helping me resolve my issues (and assisting in my learning curve). Also,
know that I do try things out and experiment a lot - this relatively simple
project has taught me quite a bit. Unfortunately I don't get to do this on a
daily basis (Script Programming) and so I loose some of what I acquire and
have to re-learn when tasked with new projects months/years later. The tools
and scripting versions are constantly changing, so having someone that is
up-to-date/current on these materials and available as a resource is
extremely helpful (and appreciated).

Going back to the printing observation...
Ref: [msgbox "Printing to " & objWord.ActivePrinter]
I added the msgbox, and now verified that on each machine the correct
printer is configured (or displayed) in the msgbox. But, the form does not
print. What I observe is; the item shows up in the respective printer
queue(s) (but only for a very brief second) then vanishes and nothing
prints... Suggestions/comments?

Current working code (except printing on other machines...)

'----------------------Printing Routine------------------------
Dim objWord
Dim strTemplate
Dim strField
Dim strField1
Dim objDoc
Dim objMark
Dim mybklist
Dim counter

Sub cmdPrint_Click()

Item.Save
Set objWord = CreateObject("Word.Application")

' Put the name of the Word template that contains the bookmarks
strTemplate = "OSR.dot"

' Location of Word template; could be on a shared LAN
strTemplate = "\\ivory\forms\" & strTemplate

Set objDoc = objWord.Documents.Add(strTemplate)
Set mybklist = objDoc.Bookmarks

For counter = 1 to mybklist.count
Set objMark = objDoc.Bookmarks(counter)
strField = objMark.Name
If strField = "SentField" then
strField1 = CStr(Item.SentOn)
Else
strField1 = Item.UserProperties(strField)
End If
objMark.Range.InsertBefore strField1
Next
msgbox "Printing to " & objWord.ActivePrinter
objDoc.PrintOut
objWord.Quit(0)

End Sub

Thanks VERY much!

Bill Billmire -
(e-mail address removed)
 
S

Sue Mosher [MVP-Outlook]

Sorry, but I'm no good at debugging printer problems, which is what this
sounds like. You might want to get the code down to a test snippet that
isolates the problem to just Word objects and then ask in one of the Word
forums.
 
B

Bill Billmire

Thanks Sue - just to recap!!! I really appreciate all your assistance,
guidance and support through this project. I have learned a lot and I now
also have a copy of your book (for reference). Your knowledge and skills
have been invaluable to me in the success of this project/task. I hope to
work with you again in the future.

My sincerest regards, and Happy New Year to you!

Bill Billmire -
(e-mail address removed)

Sue Mosher said:
Sorry, but I'm no good at debugging printer problems, which is what this
sounds like. You might want to get the code down to a test snippet that
isolates the problem to just Word objects and then ask in one of the Word
forums.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers


Bill Billmire said:
Thanks Sue; believe me, I really appreciate your time and expertise in
helping me resolve my issues (and assisting in my learning curve). Also,
know that I do try things out and experiment a lot - this relatively
simple
project has taught me quite a bit. Unfortunately I don't get to do this
on a
daily basis (Script Programming) and so I loose some of what I acquire and
have to re-learn when tasked with new projects months/years later. The
tools
and scripting versions are constantly changing, so having someone that is
up-to-date/current on these materials and available as a resource is
extremely helpful (and appreciated).

Going back to the printing observation...
Ref: [msgbox "Printing to " & objWord.ActivePrinter]
I added the msgbox, and now verified that on each machine the correct
printer is configured (or displayed) in the msgbox. But, the form does
not
print. What I observe is; the item shows up in the respective printer
queue(s) (but only for a very brief second) then vanishes and nothing
prints... Suggestions/comments?

Current working code (except printing on other machines...)

'----------------------Printing Routine------------------------
Dim objWord
Dim strTemplate
Dim strField
Dim strField1
Dim objDoc
Dim objMark
Dim mybklist
Dim counter

Sub cmdPrint_Click()

Item.Save
Set objWord = CreateObject("Word.Application")

' Put the name of the Word template that contains the bookmarks
strTemplate = "OSR.dot"

' Location of Word template; could be on a shared LAN
strTemplate = "\\ivory\forms\" & strTemplate

Set objDoc = objWord.Documents.Add(strTemplate)
Set mybklist = objDoc.Bookmarks

For counter = 1 to mybklist.count
Set objMark = objDoc.Bookmarks(counter)
strField = objMark.Name
If strField = "SentField" then
strField1 = CStr(Item.SentOn)
Else
strField1 = Item.UserProperties(strField)
End If
objMark.Range.InsertBefore strField1
Next
msgbox "Printing to " & objWord.ActivePrinter
objDoc.PrintOut
objWord.Quit(0)

End Sub

Thanks VERY much!

Bill Billmire -
(e-mail address removed)
 
B

Bill Billmire

Sue, just an update and FYI...

The Word group provided me the answer to the printing problem.

use objDoc.PrintOut 0

Add the "0" (zero) to the argument.

Without this, a background print is started while the code continues to the
next line. The next line quits Word, abandoning the print!

The 0 parameter turns off background printing and forces the code to wait
until the print job has been fully sent to the print spooler.

Many regards!

Bill Billmire -
(e-mail address removed)

Sue Mosher said:
Sorry, but I'm no good at debugging printer problems, which is what this
sounds like. You might want to get the code down to a test snippet that
isolates the problem to just Word objects and then ask in one of the Word
forums.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Bill Billmire said:
Thanks Sue; believe me, I really appreciate your time and expertise in
helping me resolve my issues (and assisting in my learning curve). Also,
know that I do try things out and experiment a lot - this relatively
simple
project has taught me quite a bit. Unfortunately I don't get to do this
on a
daily basis (Script Programming) and so I loose some of what I acquire and
have to re-learn when tasked with new projects months/years later. The
tools
and scripting versions are constantly changing, so having someone that is
up-to-date/current on these materials and available as a resource is
extremely helpful (and appreciated).

Going back to the printing observation...
Ref: [msgbox "Printing to " & objWord.ActivePrinter]
I added the msgbox, and now verified that on each machine the correct
printer is configured (or displayed) in the msgbox. But, the form does
not
print. What I observe is; the item shows up in the respective printer
queue(s) (but only for a very brief second) then vanishes and nothing
prints... Suggestions/comments?

Current working code (except printing on other machines...)

'----------------------Printing Routine------------------------
Dim objWord
Dim strTemplate
Dim strField
Dim strField1
Dim objDoc
Dim objMark
Dim mybklist
Dim counter

Sub cmdPrint_Click()

Item.Save
Set objWord = CreateObject("Word.Application")

' Put the name of the Word template that contains the bookmarks
strTemplate = "OSR.dot"

' Location of Word template; could be on a shared LAN
strTemplate = "\\ivory\forms\" & strTemplate

Set objDoc = objWord.Documents.Add(strTemplate)
Set mybklist = objDoc.Bookmarks

For counter = 1 to mybklist.count
Set objMark = objDoc.Bookmarks(counter)
strField = objMark.Name
If strField = "SentField" then
strField1 = CStr(Item.SentOn)
Else
strField1 = Item.UserProperties(strField)
End If
objMark.Range.InsertBefore strField1
Next
msgbox "Printing to " & objWord.ActivePrinter
objDoc.PrintOut
objWord.Quit(0)

End Sub

Thanks VERY much!

Bill Billmire -
(e-mail address removed)
 
S

Sue Mosher [MVP-Outlook]

Great tip! Thanks.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Bill Billmire said:
Sue, just an update and FYI...

The Word group provided me the answer to the printing problem.

use objDoc.PrintOut 0

Add the "0" (zero) to the argument.

Without this, a background print is started while the code continues to
the
next line. The next line quits Word, abandoning the print!

The 0 parameter turns off background printing and forces the code to wait
until the print job has been fully sent to the print spooler.

Many regards!

Bill Billmire -
(e-mail address removed)

Sue Mosher said:
Sorry, but I'm no good at debugging printer problems, which is what this
sounds like. You might want to get the code down to a test snippet that
isolates the problem to just Word objects and then ask in one of the Word
forums.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Bill Billmire said:
Thanks Sue; believe me, I really appreciate your time and expertise in
helping me resolve my issues (and assisting in my learning curve).
Also,
know that I do try things out and experiment a lot - this relatively
simple
project has taught me quite a bit. Unfortunately I don't get to do
this
on a
daily basis (Script Programming) and so I loose some of what I acquire
and
have to re-learn when tasked with new projects months/years later. The
tools
and scripting versions are constantly changing, so having someone that
is
up-to-date/current on these materials and available as a resource is
extremely helpful (and appreciated).

Going back to the printing observation...
Ref: [msgbox "Printing to " & objWord.ActivePrinter]
I added the msgbox, and now verified that on each machine the correct
printer is configured (or displayed) in the msgbox. But, the form does
not
print. What I observe is; the item shows up in the respective printer
queue(s) (but only for a very brief second) then vanishes and nothing
prints... Suggestions/comments?

Current working code (except printing on other machines...)

'----------------------Printing Routine------------------------
Dim objWord
Dim strTemplate
Dim strField
Dim strField1
Dim objDoc
Dim objMark
Dim mybklist
Dim counter

Sub cmdPrint_Click()

Item.Save
Set objWord = CreateObject("Word.Application")

' Put the name of the Word template that contains the bookmarks
strTemplate = "OSR.dot"

' Location of Word template; could be on a shared LAN
strTemplate = "\\ivory\forms\" & strTemplate

Set objDoc = objWord.Documents.Add(strTemplate)
Set mybklist = objDoc.Bookmarks

For counter = 1 to mybklist.count
Set objMark = objDoc.Bookmarks(counter)
strField = objMark.Name
If strField = "SentField" then
strField1 = CStr(Item.SentOn)
Else
strField1 = Item.UserProperties(strField)
End If
objMark.Range.InsertBefore strField1
Next
msgbox "Printing to " & objWord.ActivePrinter
objDoc.PrintOut
objWord.Quit(0)

End Sub

Thanks VERY much!

Bill Billmire -
(e-mail address removed)
 

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