Solved

VB code to create individual word documents from an Access report

Posted on 2006-10-25
3
173 Views
Last Modified: 2012-03-15
I have a database with a Microsoft access front end.  I need to get a word document created and saved for each unique instance of one of the fields (called APO) with the filename being stored as "apo".doc.  The report name is  R_monitoring_case_management_dataconv

I am having trouble with how to get the code to loop through this APO field, and then store the uniquely named document ... which actually translates to having problems with the whole piece of code required.

Help is appreciated.
0
Comment
Question by:jainesteer
3 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 17810343
It's not quite clear what you need or what problems you are having, so let's start with this:

Private Sub CreateDocuments(rs As Recordset)
    Dim wdDoc As Word.Document
    Dim wdApp As Word.Application

    Set wdApp = New Word.Application
    wdApp.Visible = True 'optional
    rs.MoveFirst
    Do Until rs.EOF
        Set wdDoc = wdApp.Documents.Add("C:\MyTemplates\MyTemplate.Dot")
        'Insert any document editing here
        wdDoc.SaveAs "C:\MyReports\R_monitoring_case_management_dataconv_" & rs.Fields("APO").Value & ".doc"
        wdDoc.Close
        Set wdDoc = Nothing
        rs.MoveNext
    Loop
    Set wdApp = Nothing
End Sub
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 125 total points
ID: 17811443
This is kind of convoluted, mainly because OutputTo does not allow for filtering.

1) Add a table to your db, APO_Control, with a single field, APO.  Do not populate it

2) Modify the query underlying your report to join to that new table

3) Run code like this:



Sub MakeReports()

    Dim rs As DAO.Recordset

    Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT APO FROM SomeTable")

    With DoCmd
        .SetWarnings False
        rs.MoveFirst
        Do While Not rs.EOF
            .RunSQL "DELETE * FROM APO_Control"
            .RunSQL "INSERT INTO APO_Control (APO) VALUES ('" & rs!APO & "')"
            .OutputTo acOutputReport, "R_monitoring_case_management_dataconv", _
                acFormatRTF, "C:\Folder\Subfolder\R_monitoring_case_management_dataconv_" & _
                rs!APO & ".rtf"
            rs.MoveNext
        Loop
        rs.Close
        Set rs = Nothing
        .SetWarnings True
    End With

    MsgBox "Done"

End Sub



Truth be told, I avoid using RTF because the formatting fidelity is not all that high.  I generally prefer outputting
to Snapshot:


Sub MakeReports()

    Dim rs As DAO.Recordset

    Set rs = CurrentDb.OpenRecordset("SELECT DISTINCT APO FROM SomeTable")

    With DoCmd
        .SetWarnings False
        rs.MoveFirst
        Do While Not rs.EOF
            .RunSQL "DELETE * FROM APO_Control"
            .RunSQL "INSERT INTO APO_Control (APO) VALUES ('" & rs!APO & "')"
            .OutputTo acOutputReport, "R_monitoring_case_management_dataconv", _
                acFormatSNP, "C:\Folder\Subfolder\R_monitoring_case_management_dataconv_" & _
                rs!APO & ".snp"
            rs.MoveNext
        Loop
        rs.Close
        Set rs = Nothing
        .SetWarnings True
    End With

    MsgBox "Done"

End Sub




Patrick
0
 

Author Comment

by:jainesteer
ID: 17838457
Aplogies for not getting to this sooner.  I did start looking at it and then my pc crashed (which is still the case).  As soon as I fix that, I will get back to this thread.

Thanks so far
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question