Solved

VB code to create individual word documents from an Access report

Posted on 2006-10-25
3
172 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Title # Comments Views Activity
Change the path of the data source in my Excel pivot table with macro 4 47
Spell Check in VB6 13 126
Send outlook email from VBS Script 2 42
Sub or Function is not defined 6 43
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

860 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