Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Try this slightly modified code



    One line of code removed (in GREEN)
    Added code in RED

    The idea is that it will use an existing instance of Word if it is open
    If not error handling code is used to open a new instance of Word

    Code:
    Option Compare Database
    
    Public Sub ExportNamesToWord()
    Dim wApp As Word.Application
    Dim wDoc As Word.Document
    Dim rs As DAO.Recordset
    
    'Set wApp = New Word.Application 'REMOVED
    On Error GoTo CreateWordApp
    Set wApp = GetObject(, "Word.Application") 
    
    Set wDoc = wApp.Documents.Open("C:\Users\WolphePC\Desktop\ARC \TYLetter\NewLetter.docx")
    Set rs = CurrentDb.OpenRecordset("TblNames")
    
    If Not rs.EOF Then rs.MoveFirst
    
    Do Until rs.EOF
    wDoc.Bookmarks("FullName").Range.Text = Nz(rs!FullName, "")
    wDoc.Bookmarks("Address").Range.Text = Nz(rs!Address, "")
    wDoc.Bookmarks("City").Range.Text = Nz(rs!City, "")
    wDoc.Bookmarks("Zipcode").Range.Text = Nz(rs!Zipcode, "")
    wDoc.Bookmarks("Amount").Range.Text = Nz(rs!Amount, "")
    wDoc.SaveAs2 "C:\Users\WolphePC\Desktop\ARC\TYLetter" & rs!ID & "_NewLetter.docx"
    
    wDoc.Bookmarks("FullName").Range.Delete wdCharacter, Len(Nz(rs!FullName, ""))
    wDoc.Bookmarks("Address").Range.Delete wdCharacter, Len(Nz(rs!Address, ""))
    wDoc.Bookmarks("City").Range.Delete wdCharacter, Len(Nz(rs!City, ""))
    wDoc.Bookmarks("Zipcode").Range.Delete wdCharacter, Len(Nz(rs!Zipcode, ""))
    wDoc.Bookmarks("Amount").Range.Delete wdCharacter, Len(Nz(rs!Amount, ""))
    
    rs.MoveNext
    Loop
    
    wDoc.Close False
    wApp.Quit
    
    Set wDoc = Nothing
    Set wApp = Nothing
    Set rs = Nothing
    
    CreateWordApp:
       ' If getobject fails, then ms-word was NOT running.
       ' The below will then launch word
       Set wApp = CreateObject("Word.Application")
       Resume Next
    
    End Sub
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  2. #17
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    I don't know what the exact problem was/is. I don't use word very often, and never with vba. When I had the "running" issue and coud not get Access to respond, I checked taskmanager. At one point there were 4 or 5 instances of word. I cancelled Access and all the word instances. Then found when I went to the word document, it was being used/held (by Access I think). In order to continue, I made a new Word doc( copy of the other and renamed) and adjusted the vba to reflect the new word file name. I ran the program and it worked. As per the copies of 2 output files I sent earlier.
    I also tried another test -- I took the word doc with the bookmarks and put $ right before the Amount bookmark, and deleted all of the output files, then ran the vba proc again. This time it ran fine and there was a $500 etc in the output. I also modified one of the amounts to 700.17, After deleting all the outputs again I ran the vba and $700.17 was written.
    I wanted to confirm that the $ could be done and see if dollars and cents were handled--since originally only the dollar amounts were being output (500 rather than 500.00). It did work when the "cents" values were non-zero.

    My best guess is that when Access was "running" and had to be cancelled via TM, the word file was locked/held/still used by Access.
    My attempts to get control of the file failed, so I built another word file.

    I suggest you check TM before running the program and make sure there are no word instances in TaskManager--cancel any that are there, since the vba is creating a new instance and that may be the source of the problem.

    Link to Compact and Repair

    Good luck.

    PS: I think ridders change will handle the word is already available or not (Get vs Create). Try it and let us know.
    Last edited by orange; 02-02-2018 at 09:01 AM. Reason: spelling and link to CR

  3. #18
    Wolphe is offline Novice
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    9
    Will give that a go, thank you!

    I was playing with word more and discovered another option other than bookmarks. It's called "Mail (something)" and it works. I'd have to go back to my laptop later if you'd like to know what I did.

  4. #19
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Probably mail merge or word merge -- I did use that many years ago.

    Good luck with your project.

  5. #20
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,974
    Albert Kallal provides a very easy mail merge to Word utility.
    The code is very complex but using it is simple - just one line of code
    Code:
    MergeSingleWord
    Its available from various places (not all active)
    This one definitely works: http://hitechcoach.com/downloads-mai...asy-word-merge
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 05-31-2017, 11:42 AM
  2. Coding to export data from single record to Word Bookmarks
    By Fatmonkeymedia in forum Programming
    Replies: 5
    Last Post: 12-01-2016, 10:39 PM
  3. Export Data from Access to Word
    By Byrkster in forum Access
    Replies: 4
    Last Post: 07-22-2016, 08:00 AM
  4. Replies: 0
    Last Post: 01-11-2016, 02:13 AM
  5. export access report to word
    By afshin in forum Access
    Replies: 6
    Last Post: 01-15-2014, 12:11 AM

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