Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 23

Thread: Referencing a Word document that is already open

  1. #1
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location

    Referencing a Word document that is already open

    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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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
    Last edited by macropod; 08-17-2017 at 04:09 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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
    Last edited by heedaf; 08-16-2017 at 11:11 PM.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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?

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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

  8. #8
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by heedaf View Post
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  10. #10
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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.
    Last edited by heedaf; 08-17-2017 at 02:18 PM. Reason: Clarify original problem

  11. #11
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    How about paying attention to my last reply.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  12. #12
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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?

  13. #13
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  14. #14
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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.

  15. #15
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by heedaf View Post
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  16. #16
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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?

  17. #17
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    So where is this userform being run from - Word or another application?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  18. #18
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    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

  19. #19
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  20. #20
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    Paul, you are awesome. It works great.
    Thank you very much!!!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •