从Excel打开两个现有的Word文件 [英] Open two existing word files from excel

查看:92
本文介绍了从Excel打开两个现有的Word文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写代码以使用excel与两个现有的word文档一起工作,但是我一直收到OLE错误.这仅仅是开始,但它一直崩溃.我在做什么错了?

  Sub BoQtoWord()昏暗的单词作为对象昏暗的WordDoc作为对象昏暗的WordDoc1作为对象Dim StdSpec作为字符串昏暗的NewSpec作为字符串StdSpec = Application.GetOpenFilename()设置Word = CreateObject("Word.Application")设置WordDoc = Word.Documents.Open(StdSpec)Sheet1.Range("A1").Value = StdSpecNewSpec = Application.GetOpenFilename()设置WordDoc1 = Word.Documents.Open(NewSpec)Sheet1.Range("A2").Value = NewSpec结束子 

解决方案

这对我来说似乎很好,尽管有点慢,至少就将文件路径写入单元格A1和A2而言.除此之外,我们还需要查看更多代码.

根据您试图用Word对象完成的操作,OLE问题可能源于引用中的冲突,请确保检查所引用的库(工具/参考...)是否存在任何可能的冲突./p>

另一个可能的冲突可能是使用单词"Word"作为对象的变量名.使用Microsoft Word库时,Word也是键名,请尝试使用其他名称,看是否有帮助.

说到这,通过将Microsoft Word库添加到引用中,您可以跳过创建word对象的步骤,而可以直接创建word.document对象,如下所示:

  Sub BoQtoWord()将WordDoc变暗为Word.Document将WordDoc1变暗为Word.DocumentDim StdSpec作为字符串昏暗的NewSpec作为字符串'获取第一份文件StdSpec = Application.GetOpenFilename()设置WordDoc = Documents.Open(StdSpec)Sheet1.Range("A1").Value = StdSpec'获取第二个文档NewSpec = Application.GetOpenFilename()设置WordDoc1 = Documents.Open(NewSpec)Sheet1.Range("A2").Value = NewSpec'对打开的文件进行处理结束子 

也许可以解决您的OLE问题.

希望这会为您指明正确的方向,如果有的话,更多的信息可能有助于缩小问题的范围!

Hi I'm trying to write code to use excel to work with two existing word documents but I keep getting OLE errors. This is just the start but it keeps crashing. What am I doing wrong?

Sub BoQtoWord()
Dim Word As Object
Dim WordDoc As Object
Dim WordDoc1 As Object
Dim StdSpec As String
Dim NewSpec As String

StdSpec = Application.GetOpenFilename()
Set Word = CreateObject("Word.Application")
Set WordDoc = Word.Documents.Open(StdSpec)
Sheet1.Range ("A1").Value = StdSpec
NewSpec = Application.GetOpenFilename()
Set WordDoc1 = Word.Documents.Open(NewSpec)
Sheet1.Range("A2").Value = NewSpec

End Sub

解决方案

It seems to works fine on my end, albeit a bit slow, at least as far as writing the file paths to cells A1 and A2 is concerned. Beyond that we'd need to see more of your code.

Depending on what you're trying to accomplish with the Word objects the OLE problems might stem from conflicts in your references, make sure you check what libraries you are referencing (Tools/References...) for any possible conflict.

Another possible conflict might be the use of the word "Word" as your variable name for the object. Word is also a key name when using the Microsoft Word library, try using a different name see if that helps at all.

Speaking of which, by adding the Microsoft Word library to your references you can skip the step to create the word object, you can directly create a word.document object instead, like this:

Sub BoQtoWord()
Dim WordDoc As Word.Document
Dim WordDoc1 As Word.Document
Dim StdSpec As String
Dim NewSpec As String

'Get first doc
StdSpec = Application.GetOpenFilename()
Set WordDoc = Documents.Open(StdSpec)
Sheet1.Range("A1").Value = StdSpec

'Get second doc
NewSpec = Application.GetOpenFilename()
Set WordDoc1 = Documents.Open(NewSpec)
Sheet1.Range("A2").Value = NewSpec

'Do something with the documents opened

End Sub

Maybe that would solve your OLE problems.

Hope this points you in the right direction, if anything, a bit more information might help narrow down the issue!

这篇关于从Excel打开两个现有的Word文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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