Hi all,
I've been looking for a solution to my problem for way too long so I hope someone here can help.
From an Excel workbook, I run a macro that will open/activate an existing Word doc, copy some info from the spreadsheet onto this word doc, then, if the user requires it, another existing word doc should open/activate so some more info can be copy-pasted into that 2nd word doc. When neither word doc is open or when only 1 is open, everything works fine, but once they're both open, it doesn't work (see below for exact error message and when it appears).
I am using 2x "filelocked" type of function I found in an excel forum, 1 for each of the word documents that need to be opened or activated, to avoid having to deal with the read-only issue:
I started out using #1 instead of #filenum/#filnumber, but then I thought maybe, seeing as I had 2 documents, using FreeFile would fix the problem. I also tried using #1 for the 1st function and #2 for the 2nd function, but didn't change anything.
The filelocked functions are called during these 2 portions of my code:
Calling filelocked
Calling filelocked2
If anyone can shed some light on this, that'd be great!
Thank you in advance.
I've been looking for a solution to my problem for way too long so I hope someone here can help.
From an Excel workbook, I run a macro that will open/activate an existing Word doc, copy some info from the spreadsheet onto this word doc, then, if the user requires it, another existing word doc should open/activate so some more info can be copy-pasted into that 2nd word doc. When neither word doc is open or when only 1 is open, everything works fine, but once they're both open, it doesn't work (see below for exact error message and when it appears).
I am using 2x "filelocked" type of function I found in an excel forum, 1 for each of the word documents that need to be opened or activated, to avoid having to deal with the read-only issue:
VBA Code:
Function filelocked(path As String) As Boolean
On Error Resume Next
filenum = FreeFile
Open path For Binary Access Read Write Lock Read Write As #filenum
Close #filenum
If Err.Number <> 0 Then
filelocked = True
Err.Clear
End If
End Function
VBA Code:
Function filelocked2(path2 As String) As Boolean
On Error Resume Next
filnumber = FreeFile
Open path2 For Binary Access Read Write Lock Read Write As #filnumber
Close #filnumber
If Err.Number <> 0 Then
filelocked2 = True
Err.Clear
End If
End Function
I started out using #1 instead of #filenum/#filnumber, but then I thought maybe, seeing as I had 2 documents, using FreeFile would fix the problem. I also tried using #1 for the 1st function and #2 for the 2nd function, but didn't change anything.
The filelocked functions are called during these 2 portions of my code:
Calling filelocked
VBA Code:
If filelocked(path) Then
Set wordapp = GetObject(, "Word.Application")
wordapp.Documents(path).Activate
Set doc1 = wordapp.ActiveDocument
Else
Set wordapp = CreateObject("word.application")
wordapp.Visible = True
Set doc1 = wordapp.Documents.Open(path)
End If
Calling filelocked2
VBA Code:
If filelocked2(path2) Then
Set wordapp = GetObject(, "word.application")
wordapp.Documents(path2).Activate 'error message appears here
Set doc2 = wordapp.ActiveDocument
Else
Set wordapp = CreateObject("word.application")
wordapp.Visible = True
Set doc2 = wordapp.Documents.Open(path2)
End If
- If neither doc is open, the entire macro works fine,
- if doc1 is already open but not doc2, the entire macro works fine,
- if both docs are open, then everything works fine with doc1 (it activates, then whatever needs to be copy-pasted from Excel, is and the user is prompted about whether doc2 is required), but when it gets to "wordapp.Documents(path2).Activate" (see code above), I get the following error:
Run-time error '4160': Bad file name.
If anyone can shed some light on this, that'd be great!
Thank you in advance.