PDA

View Full Version : Export Access report to a Word Template



wmtravis
04-20-2018, 08:46 AM
Hello
I am a total VBA newbie and would like some help with exporting a report I've created in Access to a Word template. The report is a capture of a employee's resume listing (Engineering firm) projects they have worked on and other personal information. It is for adding to proposals for applying for new possible projects. We require each resume to be customized for the type of building the proposal is for. The resume needs to be in Word - the report doesn't flow properly. The resume also includes a photo.

What would the VBA code be to click on a button on the resume report? I already have a template with the bookmarks but don't know how to tell Access how to grab the current report and put the fields in the current report into the bookmarks.

If I missed any pertinent information, please let me know.

Thank you,

macropod
04-20-2018, 03:50 PM
Basic code for Word automation:

Sub Demo()
'Note: A reference to the Word library must be set, via Tools|References
Dim wdApp As New Word.Application, wdDoc As Word.Document, StrName As String, TmpltNm As String
TmpltNm = "C:\Users\" & Environ("Username") & "\Documents\Resume Report.dotx"
If Dir(TmpltNm) = "" Then Exit Sub
StrName = Split(TmpltNm, ".dotx")(0) & " - " & Format(Now, "YYYYMMDD")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Add(Template:=TmpltNm, Visible:=True)
With wdDoc
'update the document

'save & close
.SaveAs Filename:=StrName & ".docx", FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
' and/or:
.SaveAs Filename:=StrName & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close SaveChanges:=True
End With
Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
Note that the code assumes you're creating a new document from a Word template stored in the user's 'Documents' folder. Change the TmpltNm variable to suit. As coded, the above sub assumes you're saving the document back to the same folder as that template and adding the current date to the filename. Again, change the StrName details to suit (e.g. using a different path and/or a data field rather than the date).

wmtravis
04-23-2018, 12:12 PM
Sorry, wasn't clear. I'm trying to use the fields in the generated report and populate an existing template that contains bookmarks as per below. I just know the command to tell Access to reference the report fields.
With Wrd.ActiveDocument.Bookmarks
.Item("Name").Range.Text = EName
.Item("Credentials").Range.Text = Initials
.Item("Title").Range.Text = Title
.Item("Years").Range.Text = Years
.Item("Profile").Range.Text = Profile
.Item("Education").Range.Text = Education
.Item("Affiliations").Range.Text = Affiliations
.Item("City").Range.Text = [City or Town]
.Item("Provine").Range.Text = Province

macropod
04-23-2018, 02:20 PM
That's as simple as deleting:

Sub Demo()
and:

End Sub
from the code I posted, so you can add it to whatever code module you're running this from, and and inserting:

.Bookmarks("Name").Range.Text = EName

.Bookmarks("Credentials").Range.Text = Initials
.Bookmarks("Title").Range.Text = Title
.Bookmarks("Years").Range.Text = Years
.Bookmarks("Profile").Range.Text = Profile
.Bookmarks("Education").Range.Text = Education
.Bookmarks("Affiliations").Range.Text = Affiliations
.Bookmarks("City").Range.Text = [City or Town]
.Bookmarks("Provine").Range.Text = Province



after:
'update the document
I'm assuming you already know how to obtain EName, Initials, etc.