1

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?

Community
  • 1
  • 1
Jebathon
  • 4,310
  • 14
  • 57
  • 108
  • 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 set – dbmitch Aug 18 '16 at 16:23
  • 1
    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.document` – Nathan_Sav Aug 18 '16 at 16:28
  • 1
    @dbmitch That surprisingly worked.... I have no idea how you came to that conclusion. Make it an answer please – Jebathon Aug 18 '16 at 16:31
  • OT: If you are using late binding, why to add the reference for word? – Sgdva Aug 18 '16 at 16:36

1 Answers1

0

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

dbmitch
  • 5,361
  • 4
  • 24
  • 38
  • OT: Shouldn't be this closed since it may be considered as a typo using restricted name variables? – Sgdva Aug 18 '16 at 16:37
  • @Sgdva - Where did you find that reason to close? You're welcome to flag question as duplicate or whatever reason for closing, but typos are a common question on this site. I don't believe I've ever seen one closed for that reason. Illuminate me. – dbmitch Aug 18 '16 at 16:59
  • 1
    Quote: On [2. ...Questions about a problem that can no longer be reproduced or that was caused by a simple typographical error...](http://stackoverflow.com/help/on-topic) Don't worry, I just learned that today as well. – Sgdva Aug 18 '16 at 17:02
  • Nice - never knew that - but IMHO this one still doesn't fit that issue. restricted name variables often don't show up as typos and an app hanging because it wasn't visible yet is more of a troublseshooting tip. – dbmitch Aug 18 '16 at 17:09
  • We are taking so much OT in this thread, there is a [meta thread](http://meta.stackexchange.com/questions/212024/dealing-with-questions-with-obvious-replies) about it. OT(ha): I didn't vote to close neither down voted but, recently I faced a similar situation and got vote downed for answering referring to the link provided. – Sgdva Aug 18 '16 at 17:21
  • Wowza - must have got someone on a bad day at the office – dbmitch Aug 18 '16 at 17:44