Forum Discussion
Maxine14062
Oct 31, 2024Brass Contributor
Access to Word Document
Office 365. I have an Access report with one image (logo) and 4 subreports as a Print Preview. I need to save it as a Word .docx.
Would it be best to just use the export to word command on the Print Preview tab or should I try to set it up as a merge document? It would require subdocuments, I believe. I will need to give it a unique name, based on the data in the report.
Once it is in Word, can I make changes from Access? Can I re-open the Word document from Access at a later date to edit it?
OR is there a better way???
Please help. Thanks in advance.
6 Replies
Sort By
- Ken_SheridanBrass Contributor
If you output an Access report to a PDF file, and then open the file in Word it will be converted to an editable version of the file, which can then be saved as a Word .docx file. Images will be preserved in the .docx file.
For an illustration of how to output a report (invoice) as a PDF file you might like to take a look at InvoicePDF.zip in my public databases folder at:
https://onedrive.live.com/?id=44CC60D7FEA42912%21169&cid=44CC60D7FEA42912
In this little demo file a button on the main invoices form outputs the current invoice report to a PDF file, which can be opened in Word and saved as a .docx file.
For an illustration of how to browse to a file in Access, and then open (or print where appropriate) the file in the default application for the file type, take a look at BrowseDemo.zip in my same OneDrive folder. The file will not reflect any changes in the data in Access since the file was created. For that you’d need to output the Access report to a PDF file again.Maxine14062
- Maxine14062Brass ContributorThanks, that worked great. The next part of the issue is to open Word and the created PDF from inside Access. A Macro would be good.
- Ken_SheridanBrass Contributor
I gave you a link in my last reply to my BrowsDemo file in my public databases on OneDrive. The demo includes the following module:
' module basShellExecute
Option Compare Database
Option Explicit
Public Const SW_HIDE = 0
Public Const SW_SHOWNORMAL = 1
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMAXIMIZED = 3
Public Const OP_OPEN = "Open"
Public Const OP_PRINT = "Print"
Declare PtrSafe Function ShellExecute& Lib "shell32.dll" Alias "ShellExecuteA" (ByVal _
hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal _
lpParameters As String, ByVal lpDirectory As String, ByVal nshowcm As Long)
Sub ShellToFile(strPath As String, _
Optional strOperation As String = OP_OPEN, _
Optional lngShow As Long = SW_SHOWNORMAL)
Dim lngRetVal As Long
Dim lngHwnd As Long
lngHwnd = Application.hWndAccessApp
lngRetVal = ShellExecute(lngHwnd, strOperation, strPath, _
vbNullString, CurDir, lngShow)
If lngRetVal < 32 Then
MsgBox "Unable to open/print file " & strPath, vbInformation, "Warning"
End If
End Sub
Add the module to your database. To open a file in its associated application you would call the ShellToFile procedure like this:
ShellToFile “C:\MyWordFiles\MyFile.docx”
- arnel_gpSteel Contributor
Maxine14062 , you may also want to try to OutputTo your report to an RTF file (Word can read RTF file).
Unfortunately, you need to Reprint (OutputTo) again if you want to make changes to the source table.
- Gerrit_ViehmannBrass Contributor
For me, it does not preserve report images, which was one of the requirements. Also, some text formats and layouts are not preserved.
But I have to admit that it works much better now than I remember. It was borderline unusable before.
- Gerrit_ViehmannBrass Contributor
Question: Why does it have to be Word? What is the intended purpose? If you don't want a PDF file, you are likely at a dead end.
If you indeed need a Word document, start with a Word document and import the data to be displayed. In simple cases, a merge document is fine, in complex export the data as an XML file. XML-Content-Controls allow you to display everything that a report can show, including subreports, in one document. Create them with the XML Mapping Pane in the hidden Developer tab.
But beware—this is an advanced topic. At some point, you will want to program in VBA and use XPath. Everything you can do manually can be done in code, but this is not true the other way around.
Example code for exporting a query from Access to Word:
Const WordFilePath As String = "Path to Word report doc" Dim tmpXmlDataFile As String tmpXmlDataFile = CurrentProject.Path & "\tmpReportData.xml" ExportXML acExportQuery, "ReportQueryName", tmpXmlDataFile Dim WordApp As Object Set WordApp = CreateObject("Word.Application") Dim doc As Object Set doc = WordApp.Documents.Open(WordFilePath, ReadOnly:=False, AddToRecentFiles:=False, Revert:=True, Visible:=True, Format:=9) ' wdOpenFormatXMLDocument=9 doc.CustomXMLParts.Add doc.CustomXMLParts(doc.CustomXMLParts.Count).Load tmpXmlDataFile WordApp.Activate