Results 1 to 7 of 7
  1. #1
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305

    Error when exporting report to Word


    Crosspost from here for more exposure. I'm using the following code to export a report to Microsoft Word:

    Code:
    Private Sub cmd_exportformPDF_Click()    Dim reportName As String
        Dim criteria As String
        Dim strfolder As String
        Dim strfilename As String
        
        reportName = "CompletedForm"
        criteria = "[ComplaintNumber]= " & [Forms]![frm2021Details]![ComplaintNumber]
        strfolder = "F:\Documents"
        strfilename = Me.CustomerLastName & ", " & Me.CustomerFirstName & " " & Format(Me.DateOpened, "m.d.yyyy") & ".pdf"
        
        DoCmd.OpenReport reportName, acViewPreview, criteria, acHidden
        DoCmd.OutputTo acOutputReport, reportName, acFormatRTF, strfolder & strfilename
        DoCmd.Close acReport, reportName, acSaveNo
     End Sub
    It exports it fine but when trying to open the file, Word says "it found unreadable content" and asks if I want to recover it. I have no images in the report so I'm unsure of the error. I wanted to see if anyone has any experience with this before I abandon and move to bookmarking.

    Thanks!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    it seems to do that even tho NOTHING is wrong. It may be a Word warning due to the RTF format.

  3. #3
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    You choose acFormatRTF in the Docmd.OutputTo line and yet you force a PDF extension in strfilename. What happens if you replace ".pdf" with ".rtf"?

    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  4. #4
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    My mistake...it was right in my database but wrong here. It's ".docx". That might be my issue. I'll change it to .rtf and report back

  5. #5
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I see a couple of issues:
    Code:
    Private Sub cmd_exportformPDF_Click()
        Dim reportName As String
        Dim criteria As String
        Dim strfolder As String
        Dim strfilename As String
        
        reportName = "CompletedForm"
        criteria = "[ComplaintNumber]= " & [Forms]![frm2021Details]![ComplaintNumber]
        strfolder = "F:\Documents"    '<<-- missing backslash
        strfilename = Me.CustomerLastName & ", " & Me.CustomerFirstName & " " & Format(Me.DateOpened, "m.d.yyyy") & ".pdf"  '<<-- Not a good idea having a comma in the file name. Also shouldn't have multiple periods in file name
        
        DoCmd.OpenReport reportName, acViewPreview, criteria, acHidden
        DoCmd.OutputTo acOutputReport, reportName, acFormatRTF, strfolder & strfilename   ' <<--  it looks like you are using the report unfiltered by having "reportName" in the objectname parameter
        DoCmd.Close acReport, reportName, acSaveNo
     End Sub


    Consider:
    Code:
    Private Sub cmd_exportformPDF_Click()
        Dim reportName As String
        Dim criteria As String
        Dim strfolder As String
        Dim strfilename As String
        
        reportName = "CompletedForm"
        criteria = "[ComplaintNumber]= " & [Forms]![frm2021Details]![ComplaintNumber]
        strfolder = "F:\Documents\"     '<<-- added backslash
        strfilename = strfolder  & Me.CustomerLastName & "_" & Me.CustomerFirstName & "_" & Format(Me.DateOpened, "m_d_yyyy") & ".rtf"
        
        Debug.Print strfilename 
    
        DoCmd.OpenReport reportName, acViewPreview, criteria, acHidden
        DoCmd.OutputTo acOutputReport, , acFormatRTF, strfilename  ' <<-- Notice no  objectname (see below)
        DoCmd.Close acReport, reportName      ' , acSaveNo means save changes to DESIGN changes - it is not needed when you close the report
     End Sub

  6. #6
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Hi Steve. This did the trick but the word document itself is a mess. Retained none of the formatting really so I think bookmarking is the path I need to go (kinda expected that). I really appreciate the help though

  7. #7
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    An alternative to bookmarking is using Word automation with mail-merge, been using that for years with great success. There are few tricks to do it right (don't use an Access table or query as the mail merge source but rather export it to a csv file and use that so the Word document doesn't lock\interact with the db). Have a look at Albert's (super easy)mail merge code, should get you on the right track:
    http://www.kallal.ca/msaccess/msaccess.html
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 09-19-2015, 10:17 AM
  2. exporting access report to ms word 2007
    By joshynaresh in forum Reports
    Replies: 8
    Last Post: 09-22-2014, 11:11 AM
  3. Exporting Report into MS Word??
    By swicklund in forum Access
    Replies: 3
    Last Post: 04-10-2014, 10:22 AM
  4. Poor Word Wrapping When Exporting Report to Rich Text File
    By caki2112 in forum Import/Export Data
    Replies: 11
    Last Post: 11-01-2012, 03:00 PM
  5. Exporting Report to a word file or PDF or whatever
    By AccessDatabaseGuy in forum Access
    Replies: 1
    Last Post: 05-03-2011, 02:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums