Solved

VB code to create individual word documents from an Access report

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…

914 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

22 Experts available now in Live!

Get 1:1 Help Now