export access query to word vba

NillGolv

New Member
Joined
May 13, 2022
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
Hello I need this help, I can't run using the query(qryCliente) to export only the list of customers. Only the nameCliente field. In the .docx file I have the Bookmark of the customer nameCliente field.

Error: Undefined variable

Public Sub ExportToWord()
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 & "\DBClientes\listCliente.docx")
Set rs = CurrentDb.OpenRecordset(CST_CLIENTE, "nameCliente")

If Not rs.EOF Then rs.MoveFirst
Do Until rs.EOF
wDoc.Bookmarks("nameCliente").Range.Text = Nz(rs!nameCliente, "")

wDoc.SaveAs2 CurrentProject.Path & "" & rs!ID & "_listCliente2.docx"

Set wDoc = wApp.Documents.Open(CurrentProject.Path & "\listCliente2.docx")
If Not wDoc Is Nothing Then
MsgBox "open file!"
Else
MsgBox "file not open!"
End If

wDoc.Bookmarks("nameCliente").Range.Delete wdCharacter, Len(Nz(rs!nameCliente, ""))

rs.MoveNext
Loop

wDoc.Close False

wApp.Quit
Set wDoc = Nothing
Set wApp = Nothing
Set rs = Nothing

End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I see nothing obvious because I know next to nothing about Word vba. However, what I did find doesn't use wdCharacter in that fashion so I'm thinking that is the line that raises the error.
When posting code please use code tags (vba button on posting toolbar) and maintain indentation to make it easier to trouble shoot, but more importantly, indicate which line causes the error.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top