Group:  Microsoft Word ยป microsoft.public.word.vba.customization
Thread: Directing/Specifying a Print Request/Location

Directing/Specifying a Print Request/Location
"Doctorjones_md" <xxxDoctorjones_mdxxx[ at ]xxxyahoo.com> 5/22/2007 4:27:00 PM
I'm pretty sure this is possible, but I'm lost in trying to figure it out
....

I have (2) pieces of code which work individually -- I need to
modify/incorporate the two so that they print data into the same WORD
template

Here's my code:
================
(This sub prints the data on an ACCESS form)
Private Sub Command83_Click()
On Error GoTo Err_Command83_Click

Dim stDocName As String
Dim MyForm As Form

stDocName = "PrintDetails"
Set MyForm = Screen.ActiveForm
DoCmd.SelectObject acForm, stDocName, True
DoCmd.PrintOut (I need to be able to specify the document to print to --
at a bookmarked location)
DoCmd.SelectObject acForm, MyForm.Name, False

Exit_Command83_Click:
Exit Sub

Err_Command83_Click:
MsgBox Err.Description
Resume Exit_Command83_Click

End Sub
===========================================
(This piece of code inserts some data into bookmarks -- no problem with
this -- it effectively print into the WORD template)
Sub CreateLetter(strTemplate As String)

' Opens a document in Word and inserts values from
' current record at bookmarks in Word document.
' Accepts: path to Word template file - String

On Error GoTo Err_Handler

Dim objWord As Object
Dim objDoc As Object
Dim frm As Form
Dim strAddress As String

' return reference to form
Set frm = Forms!frmAddresses

' if Word open return reference to it
' else establish reference to it
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If Err.Number = 429 Then
Set objWord = CreateObject("Word.Application")
End If

AppActivate "Microsoft Word"
On Error GoTo Err_Handler

' open Word document in maximised window
objWord.Visible = True
Set objDoc = objWord.Documents.Add(strTemplate)
objWord.WindowState = wdWindowStateMaximize

' insert text at bookmarks, getting values from form
InsertAtBookmarks objWord, objDoc, "FirstName", frm!FirstName
InsertAtBookmarks objWord, objDoc, "LastName", frm!LastName
strAddress = (frm!Address + vbNewLine) & (frm!Address2 + vbNewLine) & _
(frm!City.Column(1) + vbNewLine) & (frm!County + vbNewLine) &
frm!PostCode
InsertAtBookmarks objWord, objDoc, "Address", strAddress
InsertAtBookmarks objWord, objDoc, "CurrentDate", Format(VBA.Date(), "d
mmmm yyyy")
InsertAtBookmarks objWord, objDoc, "ToName", frm!FirstName

Set objDoc = Nothing
Set objWord = Nothing

Exit_here:
On Error GoTo 0
Exit Sub

Err_Handler:
MsgBox Err.Description & " (" & Err.Number & ")"
Resume Exit_here

End Sub
==========================================

What I need is to be able to change the "DoCmd.PrintOut" in the 1st piece of
code so that the data on the "Print Details" form will print at a Bookmarked
location in an EXISTING Word Template.

How would I go about doing this?

Thanks in advance ...


Re: Directing/Specifying a Print Request/Location
"Jean-Guy Marcil" <DontEvenTry[ at ]NoSpam> 5/22/2007 6:56:45 PM
Doctorjones_md was telling us:
Doctorjones_md nous racontait que :

[Quoted Text]
> I'm pretty sure this is possible, but I'm lost in trying to figure it
> out ...
>
> I have (2) pieces of code which work individually -- I need to
> modify/incorporate the two so that they print data into the same WORD
> template
>
> Here's my code:

<snip>

> What I need is to be able to change the "DoCmd.PrintOut" in the 1st
> piece of code so that the data on the "Print Details" form will print
> at a Bookmarked location in an EXISTING Word Template.
>
> How would I go about doing this?
>
> Thanks in advance ...

I have difficulty understanding your problem:

The first sub prints a document on paper with a printer.
The second sub insert text at bookmark locations using code "on a computer
screen."

How can you " print at a Bookmarked location in an EXISTING Word Template",
I mean how can you print a full document at a bookmarked location in a
document that exists only on a computer screen?

Sorry, but I am not seeing exactly what you are trying to do.

If your wording is not quite right and you meant to say that the first piece
of code should insert some information at a bookmark in the document created
by the second piece of code so that you can print that second document
later, then yes, it can be done.

When using the first piece of code, just make sure that a document created
from the second piece of code is actually open. You could do that by
checking the attached template of each opened document in all Word instances
currently open.

If you find one, just set a document object to that found document and use
the same kind of code that you used in the second sub.
If you do not find such a document, either create it or abort.

Alternatively, if it has to be the same specific document, you could set a
flag when the second sub is run, then, the first sub would check for the
flag state to find out if the document was created. The flag could be a
string with the document name, or an object pointing to that document.



--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE[ at ]CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org


Re: Directing/Specifying a Print Request/Location
"Albert D. Kallal" <PleaseNOOOsPAMmkallal[ at ]msn.com> 5/27/2007 1:43:28 AM
You can't send a form, or report to word.

So, must either do:

a) build the whole thing a an access report, and NOT use word.

b) insert the data using books marks, but that means you CAN NOT use a
report, or form for the source, you MUST WRITE CODE to do this.

So, you CAN NOT send a report, or form to word. You can however send query
data to a word document.

If you don't use any graphics, then you can send that *whole* report as word
document, but it is a all, or nothing proposition (you can NOT send parts of
a form or report to a bookmark as you ask).

So, your best bets here:

1) if you only need to send ONE record (or a ONE record that joins in other
tables), then use the standard word merge, and dump the use of bookmarks
(they are hard to use, and wore, just think what happens when the person
using this system finds out that you have to modify code for each new word
document....that makes you look VERY bad. Can you imagine that for each new
word document you had to go back to Microsoft? So, using bookmarks is a less
then ideal approach because those bookmarks require HARD CODING of your
code...

2) If you need one main record, and need to display "many" child records,
then you either need to use a report, and convert that report to word after
it made.

Or, if you must or have to insert he data direct into word, then you can't
use reports or forms, and must use sql, and code to place the fields into
the document (likely using bookmarks). There is some sample example here
that does this:


There are samples and ideas on how to do this at:

http://homepage.swissonline.ch/cindymeister/MergFram.htm

look on the left side for special merges. The one you want is

Multiple items per condition


--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pleaseNOOSpamKallal[ at ]msn.com


Home | Search | Terms | Imprint
Newsgroups Reader