Export Ms Access report to Ms word (1 Viewer)

Sokkheng

Member
Local time
Today, 12:39
Joined
Jul 12, 2023
Messages
34
How i can export ms access report to ms word document with specific file name (mean I want to put file name with my customer ID).
Thanks
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:39
Joined
Oct 29, 2018
Messages
21,473
Do you mean manually or using code?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 01:39
Joined
Feb 19, 2002
Messages
43,275
You can do this but you will probably be disappointed with the results. Reports are better exported as .pdf's which you also do with OutputTo.

Here's the code to export to an .rtf and a .pdf. This report has external criteria and the OutputTo method does not support this so although in MY applications, I always use "internal" criteria - the report's recordSource query gets its criteria from controls on forms, for this example, I showed what you have to do if you don't use criteria that is embedded in the report. You must first open the report and then, you can output to either or both formats. If you have any lines in your format, they will show in the PDF but not in the RTF so most people prefer the PDF version. Also, when you send someone a file, you probably don't want them to easily modify it so the PDF is safer.
I also would never hardcode the path but I did here for convenience. The file name ends up being:
BPStats-MyName.PDF or BPStats-MyName.RTF

If you are only exporting one copy of the report at a time, this won't be a problem and you can leave the report open. If you are exporting multiple reports in a loop, you MUST specifically close the report inside the loop or you'll run out of memory. The method will also be sluggish. Since I frequently export a bunch of PDF's - for example a commission report for each of 50 sales people, I always use the WHERE inside the query to pick up the salespersonID from a hidden form field. So, the loop reads the table of salespeople, copies the ID to the hidden form field and prints the pdf. Then loops to get the next sales person.
Code:
Private Sub cmdStatsRpt_Click()
Dim strCriteria As String
    If Me.cboFindPerson & "" = "" Then
        MsgBox "Person is required.", vbOKOnly
        Exit Sub
    End If
    If IsDate(Me.txtFrom) And IsDate(Me.txtThru) Then
        If Me.txtFrom <= Me.txtThru Then
            strCriteria = "PersonID = " & Me.cboFindPerson ''' & " AND ReadingDate Between #" & Me.txtFrom & "# AND #" & Me.txtThru & "#"
            DoCmd.OpenReport "rptBloodPressureStats", acViewPreview, , strCriteria
            DoCmd.OutputTo acOutputReport, "rptBloodPressureStats", acFormatRTF, "C:\Data\UsefulDatabases\BPStats" & "-" & Me.cboFindPerson.Column(1) & ".rtf"
            DoCmd.OutputTo acOutputReport, "rptBloodPressureStats", acFormatPDF, "C:\Data\UsefulDatabases\BPStats" & "-" & Me.cboFindPerson.Column(1) & ".pdf"
        Else
            MsgBox " From date must be <= Thru date.", vbOKOnly
            Me.txtFrom.SetFocus
            Exit Sub
        End If
    Else
        MsgBox "Both From and Thru date are required.", vbOKOnly
        Me.txtFrom.SetFocus
        Exit Sub
    End If
End Sub
 

onur_can

Active member
Local time
Yesterday, 22:39
Joined
Oct 4, 2015
Messages
180
You can transfer it with a command like this.
Code:
DoCmd.OutputTo acOutputReport, "ReportName", acFormatRTF, customerID & "ReportName"
 

isladogs

MVP / VIP
Local time
Today, 06:39
Joined
Jan 14, 2017
Messages
18,221
As already stated, exporting reports as .RTF is rarely satisfactory. I would just use .PDF output.
However, another possible approach to get a Word doc of the report is to export to PDF using code as provided in #3, then opening that in Word and saving as .DOCX.
Results will not be perfect but may be closer than the .rtf output.
 

Users who are viewing this thread

Top Bottom