Solved

VB code to create individual word documents from an Access report

Posted on 2006-10-25
3
169 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now