Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

opening multiple files in VBA v6

Options
  • 14-10-2005 11:22am
    #1
    Moderators, Arts Moderators Posts: 35,471 Mod ✭✭✭✭


    I'm trying to do somethign that should be relatively simple, but I'm completely lost in any of the code segments I've found which relate to this.

    I want to write a macro to open all the DOC files in a user-given directory and concatenate them into one file.

    It can be as rudimentary or flashy as you want, as long as it does the file opening bit (I should be able to manage the concatenation part).
    Bear in mind this is VB6 so no handy CommonDialog objects.

    Either of these three methods would be fine, if I just knew how to code it!

    a) display an inputbox asking for the path to the files, all files in that path are opened
    b) display a file browser so I can select the folder whose contents are to be opened
    c) display a file browser so I can select individual files to open.

    Any help greatly appreciated.


Comments

  • Moderators, Politics Moderators Posts: 39,788 Mod ✭✭✭✭Seth Brundle




  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Dialogs(wdDialogFileOpen).Show
    

    Will display you basic file open dialog (same as common dialog). Don't know how you'll browse for folders instead of files but I have done the same thing in Excel so it can be done. When you find out post your code, could be handy to have at some stage I'm sure.


  • Moderators, Arts Moderators Posts: 35,471 Mod ✭✭✭✭pickarooney


    Cheers EvilPhil - that was what I was looking for.
    In the meantime I found another workaround which seems to do the business as well.
    Sub Concatenate()
    
    Dim SourcePath As String
    Dim NextFile As String
    Dim sFName As String
    Dim SaveName As String
    Dim MyDataObj As New DataObject
    
    Dialogs(wdDialogFileOpen).Show
    
    SourcePath = InputBox("Enter Path to Source File(s)")
       
    Documents.Add DocumentType:=wdNewBlankDocument
    ActiveDocument.ActiveWindow.Caption = "Concatenated.doc"
       
       
    sFName = Dir(SourcePath & "\" & "*.doc")
    
    Do While sFName <> ""
        
        NextFile = SourcePath & "\" & sFName
        
        Documents.Open FileName:=NextFile, ConfirmConversions:=False, _
            ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
            PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
            WritePasswordTemplate:="", Format:=wdOpenFormatAuto
        
        ActiveDocument.ActiveWindow.Caption = "CopyFrom"
            
        'add the filename before pasting each file
        Windows("Concatenated.doc").Activate
        MyDataObj.SetText sFName
        MyDataObj.PutInClipboard
        Selection.Paste
        
        Windows("CopyFrom").Activate
        Selection.WholeStory
        Selection.Copy
        Windows("Concatenated.doc").Activate
        Selection.Paste
        Windows("CopyFrom").Activate
        ActiveDocument.Close
        sFName = Dir
        
    Loop
        
        SaveName = SourcePath & "\" & "Concatenated.doc"
        Windows("Concatenated.doc").Activate
        ActiveDocument.SaveAs (SaveName)
        ActiveDocument.Close
        
    End Sub
    


Advertisement