View Full Version : [SOLVED:] Referencing a Word document that is already open
heedaf
08-16-2017, 04:35 PM
I'm having problems with the following code where the oRng is comming up empty. Using Create Object to open the word document works fine but it is a very large document to reopen so I would like to be able to reference the open document with objWord. Any ideas?
Dim objWord As Object
Dim oRng as Word.Range
Set objWord = GetObject(,"Word.Application")
Set objWord = GetObject("C:/test.docm") 'I've tried with and with out this line without any luck
set oRng = objWord.activedocument.content
macropod
08-16-2017, 08:28 PM
Try:
Dim AppWd As New Word.Application
Dim WdDoc As Word.Document, WdRng As Word.Range
Set WdDoc = AppWd.Documents.Open("C:/test.docm")
You can now reference the document any time you need, via WdDoc. For example:
Set WdRng = WdDoc.Sections(1).Range
sets WdRng to span only the first Section in what may or may not be a multi-Section document. Having dealt with that, you might want to do something to the entire document, for which you'd reset WdRng:
Set WdRng = WdDoc.Range
heedaf
08-16-2017, 10:36 PM
Thank you for the replay. So you kind of gave me a solution. I tried what you suggested but I got the file already open problem. If I use part of the original code but use your option for the range it works. Any idea why? (I'm trying to do this from within Access)
Private Sub Command0_Click()
Dim objWord As Object
Dim oRng As Word.Range
Set objWord = GetObject(, "Word.Application")
Set objWord = GetObject("C:/temp/test1.docm")
Set oRng = objWord.Sections(1).Range
Debug.Print oRng.Text
End Sub
macropod
08-16-2017, 10:43 PM
I cannot see how you could possibly get a "file in use" warning if you used the code how I described it. Clearly, you've been doing something different - which includes re-opening the file. The code I posted does not do that.
heedaf
08-16-2017, 11:27 PM
I believe I failed to mention I'm running the script from within Access, if that matters. I copy and pasted your script directly in and I get a "file in use warning". Since the file is open already wouldn't "Set WdDoc = AppWd.Documents.Open("C:/test.docm")" try and reopen the document?
macropod
08-16-2017, 11:49 PM
Why (and how) would the file be open before using:
Set WdDoc = AppWd.Documents.Open("C:/test.docm")
There's nothing in the code you posted to indicate that you ever tested whether Word was even running before trying to open the document...
heedaf
08-17-2017, 08:22 AM
To keep this question simple (or so I thought) the word document would already be open since another script will open the document. The following code seems to work but I'm not sure why "Set oRng = objWord.Sections(1).Range" works with getobject but "Set oRng = objWord.activedocument.content" does not.
Private Sub Command0_Click()
Dim objWord As Object
Dim oRng As Word.Range
Set objWord = GetObject(, "Word.Application")
Set objWord = GetObject("C:/temp/test1.docm")
Set oRng = objWord.Sections(1).Range
Debug.Print oRng.Text
End Sub
heedaf
08-17-2017, 10:27 AM
Here is the full script that works. Note that I had to use wApp.sections(1).Range with GetObject and wApp.ActiveDocument.Content with CreateObject. I would assume they would work the same way, any idea why?
Private Sub Command0_Click()
Dim wApp As Object
Dim oRng As Word.Range
Dim lngFileNum As Long
Dim lngErr As Long
On Error Resume Next
lngFileNum = FreeFile()
Open "c:/temp/test.docm" For Input Lock Read As #lngFileNum
Close lngFileNum
filestatus = Err
If filestatus = 0 Then
Set wApp = CreateObject("Word.Application")
wApp.Documents.Open "c:/temp/test.docm"
wApp.Visible = True
Set oRng = wApp.ActiveDocument.Content
Else
Set wApp = GetObject(, "Word.Aplication")
Set wApp = GetObject("C:/temp/test.docm")
Set oRng = wApp.Sections(1).Range
End If
Debug.Print oRng.Text 'Used to verify it works
End Sub
macropod
08-17-2017, 01:59 PM
To keep this question simple (or so I thought) the word document would already be open since another script will open the document.
In that case, you should be referencing the instance of the document opened by that other script. Anything else - especially creating a new instance of Word and trying to have that open the document - is a waste of time.
heedaf
08-17-2017, 02:06 PM
To clarify my problem is that I want to run the majority of the script on the same object (also behaves the same) if the document is opened or if it needs to be opened. For some reason I can't figure out how to do this.
macropod
08-17-2017, 02:09 PM
How about paying attention to my last reply.
heedaf
08-17-2017, 02:22 PM
I have read your reply and it doesn't tell me how I can solve my problem. If the document is already open how can I obtain access to the document?
macropod
08-17-2017, 02:37 PM
You re-write the code so both lots can reference the same instance of the document. Your code in post #8 opens the document but fails to make it available outside that instance. Solving that is as trivial as defining the relevant variables at the top of the code module instead of within that sub. Your code in post #8 has some other basic flaws - which the code I provided in post #2 addresses - but that's another issue.
heedaf
08-17-2017, 02:50 PM
I think I might have given you wrong information, sorry! The original code just used CreateObject and I closed the document everytime I ran it which is stupid so now I'm trying to figure out how to run the code without closing the document first. I thought I had the right idea in #8 but the objects created are quite different.
macropod
08-17-2017, 03:07 PM
I think I might have given you wrong information
Indeed. And I still haven't seen anything that looks like the 'other' script you referred to or an indication that you've made any changes that would allow the script(s) you have posted to interact with the document that other script opens.
heedaf
08-17-2017, 03:21 PM
Thank you for bringing this up once again. I simply mispoke and meant to write the same script would open the word document on the first run through. Now that this mistake has been fully explored can we move on to a solution?
macropod
08-17-2017, 04:06 PM
So where is this userform being run from - Word or another application?
heedaf
08-17-2017, 04:18 PM
From Access.
Paul, maybe you can help me with a concept. If I change the getobject part of #8 to "set oHdr = wApp.Application.ActiveDocument.content" it seems to mimic the object created with CreateObject and seems to work ok. Why would the use of Application be required with the use of getobject? Any ideas? I'm sure you are right that there is a better way of doing this but this is all I have been able to find.
Private Sub Command0_Click()
Dim wApp As Object
Dim oRng As Word.Range
Dim lngFileNum As Long
Dim lngErr As Long
On Error Resume Next
lngFileNum = FreeFile()
Open "c:/temp/test.docm" For Input Lock Read As #lngFileNum
Close lngFileNum
filestatus = Err
If filestatus = 0 Then
Set wApp = CreateObject("Word.Application")
wApp.Documents.Open "c:/temp/test.docm"
wApp.Visible = True
Set oRng = wApp.ActiveDocument.Content
Else
Set wApp = GetObject(, "Word.Aplication")
Set wApp = GetObject("C:/temp/test.docm")
Set oRng = wApp.Application.ActiveDocument.Content
End If
Debug.Print oRng.Text 'Used to verify it works
End Sub
macropod
08-17-2017, 04:23 PM
Try:
Option Explicit
Dim AppWd As New Word.Application
Dim WdDoc As Word.Document
Private Sub Command0_Click()
If WdDoc Is Nothing Then Set WdDoc = AppWd.Documents.Open("C:\temp\test.docm")
With WdDoc
'Do whatever you need to do to the document. For example:
With .Range
MsgBox .Paragraphs.First.Range.Text
MsgBox .Paragraphs.Count
End With
End With
End Sub
Note: This code, which uses early binding, requires a reference to Word to be set via Tools|References.
heedaf
08-17-2017, 04:29 PM
Paul, you are awesome. It works great.
Thank you very much!!!!
macropod
08-17-2017, 04:34 PM
See how easy it is once you've provided the correct problem description...
Getting it for this one's been like pulling teeth!
heedaf
08-22-2017, 11:20 AM
Not exactly sure but it is only working for me about 50% of the time. When it doesn't work I get the read only warning. Any idea what the problem might be?
macropod
08-22-2017, 02:48 PM
The only reason I can see that you'd get a 'read only' warning is if you had the document open in Word before running the macro the first time.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.