如何从Excel检索数据并将其添加到Word [英] How to retrieve data from Excel and add to Word

查看:256
本文介绍了如何从Excel检索数据并将其添加到Word的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Word模板文件,该文件从Excel文件中检索数据以填充表单.

I have a Word template file that retrieves data from an Excel file to populate a form.

代码看起来像这样:

Dim myXL As Object
Set myXL = Getobject("myfile.xls")
myXL.Application.Visible = True
myXL.Parent.Windows(1).Visible = True

此代码在Office 2010和2007中工作正常,但是当我在2013年尝试使用它时,它会给出 run time error 9 ,即 array subscript error .当我检查Windows数组时,它具有零个元素,因此错误是正确的.

This code works fine in Office 2010 and 2007, but when I try it in 2013, it gives run time error 9 which is an array subscript error. When I check the Windows array it has zero elements, so error is correct.

我如何在2013年获得相同的结果?

How do I achieve the same result in 2013?

下一段代码尝试访问 Worksheets("mysheet" ),如果我跳过Visible = True行,则访问工作表将给出 runtime error 1004.

The next bit of code attempts to access the Worksheets("mysheet") and if I skip the Visible = True line accessing the worksheet gives runtime error 1004.

任何解决此问题的帮助将不胜感激.

Any help with fixing this would be greatly appreciated.

推荐答案

为使代码在Office 2013上正常工作,我在尝试使Window可见之前添加了myXL.Activate行.因此代码变为:

To make the code work on Office 2013 I added the line myXL.Activate before trying to make the Window visible. So the code becomes:

Dim myXL As Object
Set myXL = Getobject("myfile.xls")
myXL.Application.Visible = True
myXL.Activate
myXL.Parent.Windows(1).Visible = True

这解决了运行时错误,并且代码恢复了正常工作.

This fixed the run-time error, and the code went back to working well.

这篇关于如何从Excel检索数据并将其添加到Word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆