简体   繁体   中英

Export data from excel to word using VBA

I want to export data from a spreadsheet such as name, date of birth, address etc to a letter that I'm writing in word.

I've been following this tutorial :

I've had success in populating the information in word using a command button, but i don't want the ugly grey button in word so i tried making a macro and pasted the same script into a macro vba.

here is the script I'm using when trying to make the macro:

Sub Macro1()

Dim objExcel As Object
Set objExcel = CreateObject("Excel.Application")

Set exWb = objExcel.Workbooks.Open("C:\Users\Admin\Desktop\Case Log.xlsx")

ThisDocument.solicitor.Caption = exWb.Sheets("Sheet1").Cells(4, 3)

exWb.Close

Set exWb = Nothing

End Sub

when i run the macro it highlights the word "solicitor" and displays the message "Compile error: Method or data member not found"

I've checked and rechecked and the label name is correct, it works with the command button, i don't understand why it shouldn't work as a macro.

Any helps would be much appreciated.

I found a native solution for doing this. Mail Merge! I set one up and saved it, now everytime I open the document, it loads all the info from the spreadsheet which it is already pointing to.

Thanks for all the suggestions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM