dcsimg
Results 1 to 12 of 12

Thread: Opening existing document and new document at the same time

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    822

    Opening existing document and new document at the same time

    I wish to create a new blank document and I understand how to do that. But I also want to open an existing document and copy a number of extracts from it, piece by piece into the new one. I think I understand how to locate the extracts using range but I am stuck on creating the new document and also opening an existing one at the same time.

    Here's what I have that makes the new document.

    Code:
    Option Explicit
    Dim Version As String
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Dim aPath As String
    
    Private Sub Form_Load()
        lblVersion.Caption = "Version V1 9/2/19"
        aPath = "C:\Users\Alan\Documents\VB6Stuff\Vb6TestFolder\"
    
        Set wrdApp = New Word.Application 'was CreateObject("word.application")
        Set wrdDoc = wrdApp.Documents.Add(DocumentType:=wdNewBlankDocument)
        
    End Sub
    
    Private Sub cmdGo_Click()
        wrdDoc.SaveAs FileName:=aPath & "HOY"
    End Sub
    
    Private Sub cmdExit_Click()
        wrdApp.Quit
        Unload Me
    End Sub
    I am thinking that perhaps I need to close, and then reset wrdDoc again ready for the (repetitive) copy process?
    Thanks all!

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,054

    Re: Opening existing document and new document at the same time

    I am not sure 'at the same time' is what you want to do. I'd open your existing file, extract the information you want from it (putting it into memory, variables, controls, etc), close the file, then open a new blank file and then write that information to it and save it...somewhat like this: (I extracted ALL the data in the orig file, but you can pick and choose what you want to save and where/how to save it)

    Code:
    Option Explicit
    
    Dim sFile As String
    Dim objWord As Word.Application
    Dim objDoc As Word.Document
    Dim objDocContent As Word.Range
    
    Private Sub cmdLoadExistingFile_Click()
        sFile = App.Path & "\yesterday.doc"
        Set objWord = New Word.Application
        Set objDoc = objWord.Documents.Open(sFile)
        Set objDocContent = objWord.ActiveDocument.Content
        Text1.Text = objDocContent
        objWord.Documents.Close
        objWord.Quit
        Set objWord = Nothing
    End Sub
    
    
    
    
    Private Sub cmdGo_Click()
        Set objWord = New Word.Application
        Set objDoc = objWord.Documents.Add(DocumentType:=wdNewBlankDocument)
        objDoc.Range.Text = Text1.Text
        objDoc.SaveAs FileName:=App.Path & "\HOY.doc"
        objWord.Documents.Close
        objWord.Quit
        Set objWord = Nothing
    End Sub
    EDIT: I am sure you can have both docs open at the same time, just use different variables for the docs. Using Range you should be able to get what you need from the existing file, and then adding that to the range(s) in the new file. Just not 100% sure what you want to get (pictures/tables/text?).
    Last edited by SamOscarBrown; Feb 9th, 2019 at 07:03 PM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    822

    Re: Opening existing document and new document at the same time

    Hi Sam

    Thanks very much for that input. I should perhaps explain better what I am trying to do. I want to open a new document, and then copy successive random selections (of text) from within each section of an existing document into it, so opening a new blank document each time would not do. I am pretty comfortable with operations of select and ranges, and random is easy for me. I seem to remember in a VBA book, they showed referencing documents by number (index?) so once I have created the new blank document once, I would like to open it (keep it open?)and copy the selections as required.
    Thanks all!

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,054

    Re: Opening existing document and new document at the same time

    Well, my tiny example does NOT open multiple blank documents, nor re-open one repeatedly. What I simply did was put what was in the original doc (yesterday.doc) into a textbox (text only), then closed the original file. Then I created a new file and wrote what was in the textbox to that new file and then closed it up. I am sure you can do something similar, taking all the 'pieces' of the orig file you want, 'store' that data somewhere, close the orig file, and then write that stuff to a new file (like I did). But, as I mentioned before, I am confident you can have two instances of Word open at one time, to put the stuff from the orig into the new piece by piece, and then write out the new file. But my simple approach got what info I wanted from the orig file, 'saved' it (into a textbox), closed the file, then opened a blank file and then wrote that stuff to it.

    I don't see why you have to close a document, and reopen it multiple times to write to it...just write to it (the pieces), and then save the file.

    Sammi

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,054

    Re: Opening existing document and new document at the same time

    If I had time, I'd gin up an example, but Jeopardy! and Wheel of Fortune is up next!!!!! ~smile~.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    822

    Re: Opening existing document and new document at the same time

    Quote Originally Posted by SamOscarBrown View Post
    If I had time, I'd gin up an example, but Jeopardy! and Wheel of Fortune is up next!!!!! ~smile~.
    Fair 'nuff!
    Thanks all!

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,054

    Re: Opening existing document and new document at the same time

    If I find a few moments this afternoon, I might create an example for you, but it would help if I knew what kind of 'data' you are getting from the original Word document. Do you have a SAMPLE file, and in that file, what you might like to copy into a new document?

    Sam

    PS- forgot it was Saturday yesterday, J and WOF were not on, so watched a movie (Bridge of Spies) instead.

  8. #8
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,054

    Re: Opening existing document and new document at the same time

    Okay....ran a quick one for you...code commented to help you understand: (two command buttons on a form...an original file in the app path)

    Code:
    Option Explicit
    
    Dim sFile As String  'will use to ID original file to load
    
    
    'set up Word to load Original File
    Dim objOrigWord As Word.Application
    Dim objOrigDoc As Word.Document
    Dim objOrigDocContent As Word.Range
    
    
    'set up Word to save to New File
    Dim objNewWord As Word.Application
    Dim objNewDoc As Word.Document
    Dim objNewDocContent As Word.Range
    
    
    Dim myData As String 'In my case, a simple string variable to get data from original file
    
    
    Private Sub cmdLoadExistingFile_Click()
        'Open original file
        sFile = App.Path & "\OriginalFile.doc"
        Set objOrigWord = New Word.Application
        Set objOrigDoc = objOrigWord.Documents.Open(sFile)
        'extract from Original file what you want (in this case, I got ALL the data, but using Range, you should be able to get pieces.
        Set objOrigDocContent = objOrigWord.ActiveDocument.Content
        'set my string to the contents I wanted (you may be doing this in a loop/array, depending upon what data you want.
        myData = objOrigDocContent
        MsgBox "Done"
    End Sub
    
    
    
    
    Private Sub cmdGo_Click()
        'Save New File
        Set objNewWord = New Word.Application
        Set objNewDoc = objNewWord.Documents.Add(DocumentType:=wdNewBlankDocument)
        objNewDoc.Range.Text = myData  'notice my string variable, but you can use an array to store what you get from original file if desired
        objNewDoc.SaveAs FileName:=App.Path & "\NewFile.doc"
        MsgBox "NewFile.doc has been created."
    End Sub
    
    
    
    
    Private Sub Form_Unload(Cancel As Integer)
        'these would be better off accomplished in each of the command buttons (after each function completed), but I just wanted to show you
        'can have multiple documents open simultaneously
        objOrigWord.Documents.Close
        objOrigWord.Quit
        Set objOrigWord = Nothing
        
        objNewWord.Documents.Close
        objNewWord.Quit
        Set objNewWord = Nothing
    End Sub
    EDIT: Of course a Reference to the MS Word Object Library

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,054

    Re: Opening existing document and new document at the same time

    Okay, here's my example using two ranges from the original document and writing them to a new document:

    Code:
    OPTION Explicit
    
    Dim sFile As String  'will use to ID original file to load
    
    'set up Word to load Original File
    Dim objOrigWord As Word.Application
    Dim objOrigDoc As Word.Document
    'Dim objOrigDocContent As Word.Range
    
    
    'set up two ranges from original file
    Dim sRange1 As Word.Range
    Dim sRange2 As Word.Range
    
    
    'set up Word to save to New File
    Dim objNewWord As Word.Application
    Dim objNewDoc As Word.Document
    Dim objNewDocContent As Word.Range
    
    
    
    
    Dim myData As String 'In my case, a simple string variable to get data from original file
    
    
    Private Sub cmdLoadExistingFile_Click()
        'Open original file
        sFile = App.Path & "\OriginalFile.doc"
        Set objOrigWord = New Word.Application
        Set objOrigDoc = objOrigWord.Documents.Open(sFile)
        'extract from Original file what you want (in this case, I got ALL the data, but using Range, you should be able to get pieces.
    '    Set objOrigDocContent = objOrigWord.ActiveDocument.Content
        'GET the two ranges from original file  (note, I simply used paragraphs 1 and 3 of my original document (which has three paragraphs)
        Set sRange1 = objOrigWord.ActiveDocument.Paragraphs(1).Range
        Set sRange2 = objOrigWord.ActiveDocument.Paragraphs(3).Range
        'set my string to the contents I wanted (you may be doing this in a loop/array, depending upon what data you want.
    '    myData = objOrigDocContent
        MsgBox "Done"
    End Sub
    
    
    
    
    Private Sub cmdGo_Click()
        'Save New File
        Set objNewWord = New Word.Application
        Set objNewDoc = objNewWord.Documents.Add(DocumentType:=wdNewBlankDocument)
    '    objNewDoc.Range.Text = myData  'notice my string variable, but you can use an array to store what you get from original file if desired
        objNewDoc.Range.Text = sRange1 & vbCrLf & sRange2  'added the two paragraphs to the new file.
        objNewDoc.SaveAs FileName:=App.Path & "\NewFile.doc"
        MsgBox "NewFile.doc has been created."
    End Sub
    
    
    
    
    Private Sub Form_Unload(Cancel As Integer)
        'these would be better off accomplished in each of the command buttons (after each function completed), but I just wanted to show you
        'can have multiple documents open simultaneously
        objOrigWord.Documents.Close
        objOrigWord.Quit
        Set objOrigWord = Nothing
        
        objNewWord.Documents.Close
        objNewWord.Quit
        Set objNewWord = Nothing
    End Sub

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    822

    Re: Opening existing document and new document at the same time

    The breakthrough for me was realizing that I could open two separate instances of the Word object! Supposed to be retired, but I do earning work in the mornings so that was a weekend project. But I may try to interrupt afternoon snoozing time to try this out with some live docs. Many thanks again.
    Thanks all!

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2012
    Posts
    822

    Re: Opening existing document and new document at the same time

    Making progress. I now find that the new document has spaces between the lines. Is there a command to set linespacing (or paragraph spacing) from VB6? For the future, is there a reference I can download that lays out all the possible methods(?) or even a good book about automating Word from VB6?
    Thanks all!

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,438

    Re: Opening existing document and new document at the same time

    you do not really need 2 word applications, you can open multiple documents in one instance of a word application

    For the future, is there a reference I can download that lays out all the possible methods(?) or even a good book about automating Word from VB6?
    check the faqs for automating office applications in the stickies at the top of the office development forum
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width