Hello
I wondered if someone might be able to help me. My understanding of VB is nil so please bear with me if I make an error.
I have an Access database that I have set-up to help me record invoices and have written in VBA to insert values from a query into Bookmarks of a Word document (SDK-template.docx).
The code I'm using is from a Youtube video (https://www.youtube.com/watch?v=JilB511V3AU), but I don't really understand what any of it means. Here is the end code:
Code:
Option Compare DatabaseOption Explicit
Public Sub ExportInvoicesToWord()
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("C:\Users\steve\Documents\Invoices\SDK-template.docx")
Set rs = CurrentDb.OpenRecordset("004_Export query")
If Not rs.EOF Then rs.MoveFirst
Do Until rs.EOF
wDoc.Bookmarks("Invoice_date").Range.Text = Nz(rs![Invoice date], "")
wDoc.SaveAs2 "C:\Users\steve\Documents\Invoices\" & rs!Invoice & ".docx"
wDoc.Bookmarks("Invoice_date").Range.Delete wdCharacter, Len(Nz(rs![Invoice date], ""))
rs.MoveNext
Loop
wDoc.Close False
wApp.Quit
Set wDoc = Nothing
Set wApp = Nothing
Set rs = Nothing
End Sub
I have a Bookmark in Word called Invoice_date and a corresponding column in my query called Invoice date.
I have another column in the query called Invoice which is the invoice's reference and I'm trying to save the Word document as this value.
The query that I'm trying to get values from is called 004_Export query.
When I run the code it just freezes, and I have to crash out of Access and no files are saved in the specified folder. Is there anything anyone can spot in the above code that could be causing this? Please let me know if you need more info.
Many thanks in advance
Steve