Consulting

Results 1 to 4 of 4

Thread: Export Access report to a Word Template

  1. #1
    VBAX Newbie
    Joined
    Apr 2018
    Posts
    2
    Location

    Export Access report to a Word Template

    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,

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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).
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Newbie
    Joined
    Apr 2018
    Posts
    2
    Location
    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

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •