0
votes

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)

enter image description here

Is there an alternative to opening a file with Excel and how I rewrite my program?

1
Have you tried changing 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". You should also change your "File" variable name too - that can be a reserved word too depending on references you've setdbmitch
Also, prefix instead, such as pthWordLocation and flnmWordTemplate, stops the risk of using reserved words, if you've the library referenced, then use dim wdApp as word.application and dim wdDocument as word.documentNathan_Sav
@dbmitch That surprisingly worked.... I have no idea how you came to that conclusion. Make it an answer pleaseJebathon
OT: If you are using late binding, why to add the reference for word?Sgdva

1 Answers

0
votes

As requested - this is a common problem

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".

You should also change your File variable name too - that can be a reserved word too depending on references you've set

Added Comment:

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