VBA Excel-无法打开现有的Word文档文件 [英] VBA Excel - Unable to open existing Word Document file
问题描述
我有一个简单的宏,可以使用Excel打开Word文档.我确保正确引用了Word对象库,但是在运行此宏时,它会在调用Documents.Open之后冻结(基于我在调试器中看到的失败之处).我不知道是不是 OLE 自动化错误,但宏冻结了,我必须强制关闭 Excel.
I have a simple macro that opens a Word Document using Excel. I made sure the Word Object Library is properly referenced but when running this macro it freezes after Documents.Open is called (based on me seeing where it fails in the debugger). I don't know if it is a OLE Automation Error but the macro freezes and I have to force close Excel.
Public Const Dir = "C:/Temp/"
Public Const File = "temp.docx"
Public Sub OpenFile()
Dim f As String: f = Dir & File
Dim oWord As Object, oDoc As Object
Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.Documents.Open(f)
oDoc.Visible = True
End Sub
我也收到此消息:(即使没有打开其他应用程序)
I get this message as well: (even though there is no other application open)
是否可以使用Excel打开文件以及如何重写程序?
Is there an alternative to opening a file with Excel and how I rewrite my program?
推荐答案
根据要求-这是一个常见问题
As requested - this is a common problem
您应该更改您的 Dir
变量-这是一个保留名称-您可能会遇到Word错误,当您尝试打开该文件"时看不到该错误.
You should change your Dir
variable - that's a reserved name - and you're probably getting a Word error you can't see when you try to open that "file".
您还应该更改您的 File
变量名-根据您设置的引用,它也可以是保留字
You should also change your File
variable name too - that can be a reserved word too depending on references you've set
添加了评论:
关于冻结-您可以删除 oDoc.Visible = True
语句,并将其替换为 oWord.Visible = True
之前问题语句 Set oDoc = oWord.Documents.Open(f)
.那会弹出错误消息,表明您的文件名有问题
With regard to it freezing - you can remove the oDoc.Visible = True
statement and replace it with oWord.Visible = True
BEFORE the problem statement Set oDoc = oWord.Documents.Open(f)
. That would popup the error indicating you had a problem with your filename
这篇关于VBA Excel-无法打开现有的Word文档文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!