PDA

View Full Version : .Open Method Tries to Open Word File as ReadOnly Even Though It's Not



bnieto87
11-13-2015, 08:18 AM
I am trying to write a macro that allows a user to select documents from a list to open (and then eventually be edited automatically).

I have an Excel spreadsheet with the macro and a worksheet listing all of the files. The macro reads the list of files and an inputbox pops up, allowing the user to select which files they want. The macro then passes the filenames onto another sub that opens each file (in Word). It's having no problem finding the files and opening Word, but it keeps telling the user that "[Filename] is loced for editing by '[username]'. Do you want to: Open a Read Only copy / Create a local copy and merge your changes later / Receive notification when the original copy is available", but the file isn't locked for editing!!! I don't get it!

I've tried using SendKeys to hit ENTER to continue along, but the prompt isn't the active window, so that does nothing and I can't use ALT+TAB because the number of times that needs to be used to get to the prompt is dependent on the number of other windows open. I've also used "wordapp.ReadOnly = True", but that doesn't seem to change anything. I've checked that the file is definitely NOT read-only, even creating brand new test files with the correct filename (i.e., created a new Word file, named it one of the filenames from the list, and saved it to the folder to the macro looks in).

Any ideas? Thanks in advance.

Here is my code:



Sub Compile_Specs()

SpecPrompt.Show

End Sub

-------------------------------

Sub SpecSheetPopulate(Tname As String)


Set wordapp = CreateObject("word.Application")

wordapp.documents.Open "C:\Users\bnieto\Documents\" & Tname & ".docx"


End Sub

-----------------

Private Sub Cancelprompt_Click()
UFpromptcancel = True
Unload Me
End Sub

------------------------

Private Sub OKprompt_Click()

Dim k As Integer
Dim Tname As String
Dim varItm As Variant
Dim strBuild As String

'Initial value should be row of first Spec listed
SpecRow = 2

For k = 0 To PromptList.ListCount - 1

If PromptList.Selected(k) = True Then

Tname = Worksheets("SpecList").Cells(SpecRow + k, 2)
Tname = Tname & " - " & Worksheets("SpecList").Cells(SpecRow + k, 1)


Call SpecSheetPopulate(Tname)


Application.ScreenUpdating = False



End If
Next k


Unload Me
UFpromptcancel = False

End Sub

---------------------

Private Sub UserForm_Initialize()

PromptList.Clear

Dim SpecRow As Integer
Dim strSpec

Sheets("SpecList").Select

'Initial value should be row of first Spec listed
SpecRow = 2

While Not IsEmpty(Cells(SpecRow, 1))

strSpec = Worksheets("SpecList").Cells(SpecRow, 2)
strSpec = strSpec & " - " & Worksheets("SpecList").Cells(SpecRow, 1)

PromptList.AddItem strSpec
SpecRow = SpecRow + 1
Wend

End Sub

gmayor
11-14-2015, 01:31 AM
The problem presumably arises because you already have the document open in an other instance of Word, and you are creating a new instance in order to run your process, and there is thus a lock file associated with the other instance that prevents it from opening again in the new instance. It is therefore better (and faster) to use the existing instance of Word and only create a new one if Word is not already open. See also http://www.gmayor.com/what_to_do_when_word_crashes.htm (http://www.gmayor.com/what_to_do_when_word_crashes.htm) with regard to lock files.

Also declare your variables. It is good practice and makes debugging code easier.


Dim wordapp As Object
On Error Resume Next
Set wordapp = GetObject(, "Word.Application")
If Err Then
Set wordapp = CreateObject("Word.Application")
End If
On Error GoTo 0

MacroWizard
11-14-2015, 02:44 PM
Also, make sure that the files themselves are not set as read-only - just to cover all of the bases here.