Let MS Access do the writing/printing

G

Guest

I am trying to get Access to output to word, much like using "write #1,
StringOfImportantText"; however, i have no idea how to accomplish tihs
without using Word Fields. Thank you for your help.

Goals of this exercise:
1) export fields from a query to word
2) apply formatting to certain paragraphs
3) create tables
4) apply formatting to tables, including column width
5) do this with an everchanging query

Public Sub printWord(ByVal qry As DAO.Recordset, _
ByVal progBar As Control, _
ByVal tmpPath As String, _
ByVal runTime As Date, _
Optional ByVal strDivision As String, _
Optional ByVal strTable As String)

Dim appWord As Word.Application
Dim docWord As Word.Document

Dim strDoc As String

strDoc = "wordTemplate.dot"

On Error Resume Next
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then Set appWord = CreateObject("Word.Application"):
Err.Clear

appWord.Visible = True

Set docWord = appWord.Documents.Add(tmpPath & "\" & strDoc)
docWord.SaveAs tmpPath & "\" & qry![nameDivision].Value & " " &
strDate(runTime) & " " & strTime(runTime) & ".doc"

'let MS Access do the typing
'
' Pseudo Code:
' write qry![nameTable].Value
' apply heading1 to qry![nameTable].Value
' insert table (3 colums by 1 row)
' apply tableStyle to table; make column1 2", make column2 1", make
column3, 1"
' write "name", "quantity", "percent" in table row
'
' add row
' write firstname in column1
' write amountsold in column2
' write percent in column3
'
' and so on... from the start to then end of the query.
'


docWord.SaveAs tmpPath & "\" & qry![nameDivision].Value & " " &
strDate(runTime) & " " & strTime(runTime) & ".doc"

appWord.Quit True

End Sub
 
J

John Nurick

Hi Rogge,

You'll need to learn more about the Word object model, in particular how
to manipulate the Word.Range and Word.Table objects.

A good way to start is to record macros in Word as you do what you need.
Then - still in Word - modify them to work with explicit Document and
Range objects instead of the Selection object that the macro recorder
uses. Finally move the code into an Access module and tweak it to work
there.

Also, check out http:word.mvps.org, where there's a mass of useful
information about making Word do what you want it to.

I am trying to get Access to output to word, much like using "write #1,
StringOfImportantText"; however, i have no idea how to accomplish tihs
without using Word Fields. Thank you for your help.

Goals of this exercise:
1) export fields from a query to word
2) apply formatting to certain paragraphs
3) create tables
4) apply formatting to tables, including column width
5) do this with an everchanging query

Public Sub printWord(ByVal qry As DAO.Recordset, _
ByVal progBar As Control, _
ByVal tmpPath As String, _
ByVal runTime As Date, _
Optional ByVal strDivision As String, _
Optional ByVal strTable As String)

Dim appWord As Word.Application
Dim docWord As Word.Document

Dim strDoc As String

strDoc = "wordTemplate.dot"

On Error Resume Next
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then Set appWord = CreateObject("Word.Application"):
Err.Clear

appWord.Visible = True

Set docWord = appWord.Documents.Add(tmpPath & "\" & strDoc)
docWord.SaveAs tmpPath & "\" & qry![nameDivision].Value & " " &
strDate(runTime) & " " & strTime(runTime) & ".doc"

'let MS Access do the typing
'
' Pseudo Code:
' write qry![nameTable].Value
' apply heading1 to qry![nameTable].Value
' insert table (3 colums by 1 row)
' apply tableStyle to table; make column1 2", make column2 1", make
column3, 1"
' write "name", "quantity", "percent" in table row
'
' add row
' write firstname in column1
' write amountsold in column2
' write percent in column3
'
' and so on... from the start to then end of the query.
'


docWord.SaveAs tmpPath & "\" & qry![nameDivision].Value & " " &
strDate(runTime) & " " & strTime(runTime) & ".doc"

appWord.Quit True

End Sub
 
G

Guest

Thank you... i forgot about "the do it in word and transfer the code"
technique. And also, thank you for the "range" property.

I was able to move the move the code which does teh typing and formatting
for me, but it is sooooo slow. is this just the nature between the two
programs? I am using 2 queries and a couple of loops to create the word
document.

again thank you for your help.
 

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