Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    54
    Thanks
    6
    Thanked 1 Time in 1 Post
    I currently have a program that evaluates an active document. Now, when the program begins, I want the user to be able to add a second document to the evaluation process. So, I want a form that includes a control to locate and open the second file. What control do I use to allow the user to select a file on their computer in Word VBA 2003 or Word 2007? After that point, I'll need to switch between the two documents that are open to make them active and then evaluate the text in the two documents. I assume, at that point I have two separate ranges constituting the text in both documents.

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    Unless you need the userform for additional purposes, you could also use the built-in File Open dialog to allow the user to navigate to the second document to be opened.
    You can also set object variables to both documents; this makes it easier to move back and forth between the two documents and address content in each document. Once you've set an object variable to both documents, you don't need to activate them, in order to work with them.
    Some sample code:

    Code:
    Sub TestFileOpen()
       Dim strOpenedFilePath As String
       Dim objFirstDoc As Document
       Dim objOpenedDoc As Document
       
       'Set object variable to first document:
       Set objFirstDoc = ActiveDocument
    
       With Dialogs(wdDialogFileOpen)
          .Display
          strOpenedFilePath = .Name
       End With
       
       'Set object variable to second document:
       Set objOpenedDoc = Documents.Open(strOpenedFilePath)
       
       'Do stuff with both documents:
       MsgBox objFirstDoc.Paragraphs(1).Range.Text
       MsgBox objOpenedDoc.Paragraphs(1).Range.Text
       
       Set objFirstDoc = Nothing
       Set objOpenedDoc = Nothing
    End Sub
    Gary

  3. #3
    Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    54
    Thanks
    6
    Thanked 1 Time in 1 Post
    Thank you. That's exactly what I needed.
    Larry

Posting Permissions

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