简体   繁体   English

使用VBA将数据从Excel导出到Word

[英]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. 我已经成功地使用命令按钮在单词中填充了信息,但是我不想在单词中使用难看的灰色按钮,所以我尝试制作一个宏并将相同的脚本粘贴到宏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 感谢所有的建议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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