0

When using the Documents.Open("XX") command in excel. My excel prompts the window where it says that the document is lock for editing, by ME. Which isn't the case.

This command does seem to work when i'm using microsoft word/excel 2010 at work. But at home, using office 2016, it doesn't. This goes for my personal laptop with office 2016 aswell.

The following code is used:

Sub test()

Dim wordDoc As Word.Application
Dim testDoc As Word.Application

Set wordDoc = CreateObject("Word.Application")
Set testDoc = wordDoc.Documents.Open("C:\Users\Me\Desktop\Test.docm")

' Do stuff

End Sub

My thoughts:

Office 2016 seems to open a 'Shadow file' which gives the document 'Open' status. But when restarting my PC the code simply does NOT work. Giving an 'Filename' error, upon restarting the code the 'Locked for editing' error appears.

My question:

Am i using the wrong approach on opening a document in Office 2016? If so, what is the right approach? If not, are there settings that need to be changed in order for this to work?

Thanks in advance,

braX
  • 11,506
  • 5
  • 20
  • 33
RAH
  • 41
  • 2
  • 8
  • I've had this happen occasionally when working with a Word document from a network location (not via VBA). Just delete the shadow file. – Comintern Aug 29 '18 at 12:47
  • Thats the thing. At home im not on any network, even after deleting the file it seems to think it either doesnt exit or is in use – RAH Aug 29 '18 at 12:52
  • Ah, in the code you post you Dim `testDoc` as **`Word.Application`** rather than `Word.Document`. So VBA is not going to want to let you assign a Document to it...? – Cindy Meister Aug 29 '18 at 13:09
  • That is a good observation. Upon testing, it didnt seem to work. – RAH Aug 29 '18 at 13:13

1 Answers1

2

I've had this happen before, the issue for me was that a previous run of the macro wasn't able to close the file properly. Give this thread a read: Application.Quit command not closing the entire Excel Application

You could test to see if this is the case by "End Task"ing all MS Word instances and running your macro again, If it's able to run the first time, then this is your issue.

Also, This is the code I used for testing. Dim'ing the variables as "Word.Application" didn't work for me, so if my earlier suggestion doesn't work then try just "Object" instead:

Sub openWord()

    Dim wrd As Object
    Dim doc As Object

    Set wrd = CreateObject("Word.Application")
    wrd.Visible = True
    Set doc = wrd.Documents.Open("C:\Users\name\Desktop\doc.docx")

    doc.Close
    wrd.Quit
End Sub
Blenm
  • 56
  • 5
  • 1
    Was going to make reference to this in the original question but, in order to dim to Word.Application, you will need the `Microsoft Word xx.x Object Library` reference. (Tools --> References). And once you do that you don't need your `Set wrd = CreateObject("Word.Application")` line, plus you get the intellisense when working with the objects. :) – JosephC Aug 29 '18 at 16:21
  • The file opened smoothly after the first run. Im still not sure why it didnt close properly in the first place, but my thanks for this answer. – RAH Aug 29 '18 at 16:45
  • 1
    Final answer: If error occurs, close all word processes via task manager. Make sure your code has a error handling which has a wrd.quit line in it, to make sure the next time an error occurs the app closed correctly. – RAH Aug 30 '18 at 12:57