Solved

VB code to create individual word documents from an Access report

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…

808 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