X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
   Reply to this topicStart new topic
> Access To Word With Bookmarks From Query, Access 2016    
post Aug 10 2017, 03:48 PM

Posts: 64
Joined: 7-January 11

I'm trying to make a letter from data from access. I have about 40 or so records to print out on a work document. I've set up the document with bookmarks. I'm getting the data from a query.

So far this sorta runs.... I click the cmd button.... nothing happens. When I click on the actual file name, it says it's read only, then if I click thru it open and then the it runs.... creating saving the 40 or so files in the documents folder. Then when I open one of the word files it is formatted correctly.

I would like it to print these out, don't really need to save them. But, for testing purposes I'm just saving them, I can change that function later.

How do I get this to run from the cmd click? What am I missing?



Private Sub ExportToWord_Click()

    Dim wApp As Word.Application
    Dim wDoc As Word.Document
    Dim rs As DAO.Recordset
        Set wApp = New Word.Application
        Set wDoc = wApp.Documents.Open(CurrentProject.Path & "\ExportLetter.docx")
        Set rs = CurrentDb.OpenRecordset("AddressLabelCurrent", dbOpenDynaset)
    If Not rs.EOF Then rs.MoveFirst
    Do Until rs.EOF
        wDoc.Bookmarks("FirstName").Range.Text = Nz(rs!Name, "")
        wDoc.SaveAs2 "CurrentccProject.Path" & rs!StreetAddress & "_documentname.docx"
        wDoc.Bookmarks("FirstName").Range.Delete wdCharacter, Len(Nz(rs!Name, ""))

   wDoc.Close False
   Set wDoc = Nothing
   Set wApp = Nothing
   Set rs = Nothing

Go to the top of the page
post Aug 11 2017, 02:09 AM

Posts: 3,138
Joined: 19-October 10

I personally despise bookmarks, its such a 2003 solution to this sort of thing.

For a very very long time Word documents have been able to store XML data in a collection called CustomXMLParts.
This XML data can be mapped to content controls (which can be borderless and seemless so they don't stick out), a single XML node can be mapped to multiple content controls so if you need the name multiple times throughout a document you no longer mess around with bookmark_Name_1 and bookmark_Name_2 etc. Since the content controls are bound changing one changes all with the same binding so everything stays in sync.
You can also pull the data back out of Word later which can be really really useful.
As of 2013 Repeating content controls were introduced which allows repeating data in XML to be now be expressed without any extra coding, its pretty awesome and easy to make a table of data now.

All you have to do in code is make the XML add insert that into the document.

Obviously this doesn't answer what you are asking since you are using bookmarks but I'd suggest there are now better ways and they are worth exploring.
Go to the top of the page
post Aug 11 2017, 07:56 AM

Posts: 64
Joined: 7-January 11

I've seen lots of reference to exporting to tables etc. bit not XML data.

It's a basic form letter with the address and names, not very complex. Just need to get some of of the syntax correct.

I'll look into that to see what I can learn.


Go to the top of the page

Custom Search
RSSSearch   Top   Lo-Fi    19th November 2017 - 04:29 PM