Results 1 to 5 of 5

Thread: VB & Word

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Location
    Dublin
    Posts
    30
    I have a main directory and a number of subdirectories on my hard-drive.

    I have a number of MS Word files in the sundirectories. (standard forms and letters etc.) I want to insert my clients name into the footer of each word document in the all the subdirectories.

    How do I select each sub-direcory in turn until there are no subdirectories left.

    How do I select each Word file in turn until there are no Word files left.

    Once I have a Word file open - I have no problem inserting the Client Name text - that I can do. I can also open and close a single Word file - no problem.

    Any help would be great.

    Graham.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    The simpliest and fastest way of doing this without resorting to the API is to use the FileSystemObject.
    Set a reference to Microsoft Scripting Runtime and edit the following code sample:
    Code:
    Private Sub GetWordFiles(sPath As String)
        Dim fso As FileSystemObject
        Dim sFilename As String
        Dim fld As Folder
        
        Set fso = New FileSystemObject
        If fso.FolderExists(sPath) Then
            sPath = sPath & IIf(Right$(sPath, 1) <> "\", "\", "")
            sFilename = Dir(sPath & "*.doc")
            Do While Len(sFilename)
                MsgBox sPath & sFilename
                sFilename = Dir
            Loop
            For Each fld In fso.GetFolder(sPath).SubFolders
                GetWordFiles fld.Path
            Next
        End If
    End Sub
    Good luck!

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Location
    Dublin
    Posts
    30
    Thanks for the help.

    Graham.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Location
    Dublin
    Posts
    30
    Joacim,

    Can I ask for your help again.

    I have used the last bit of code you have given me but I must be doing something stupid.

    I am trying to get the code to run on the click of a command button.

    It won't let me put the definition (sPath As String) in the title of the procedure and I am getting an error on the GetWordFiles Command.

    If you could give me the code for operating from the click of a button I would be most geteful.

    Grahm.

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Either you copy the entire procedure I gave you in the previous post and call it from the Click event:
    Code:
    Private Sub Command1_Click()
        GetWordFiles("c:\TheFolderWhereTheWordFilesAre")
    End Sub
    Or you can rewrite the code into the Click event
    Code:
    Private Sub Command1_Click()
        Dim fso As FileSystemObject
        Dim sFilename As String
        Dim fld As Folder
        Dim sPath As String
    
        'Get the path!
        'Either hardcode the path or ask the user for it!
        sPath = InputBox("Where are the Word files?")
        If Len(sPath) = 0 Then
           'the User Clicked Cancel
            Exit Sub
        End If
        Set fso = New FileSystemObject
        If fso.FolderExists(sPath) Then
            sPath = sPath & IIf(Right$(sPath, 1) <> "\", "\", "")
            sFilename = Dir(sPath & "*.doc")
            Do While Len(sFilename)
                MsgBox sPath & sFilename
                sFilename = Dir
            Loop
            For Each fld In fso.GetFolder(sPath).SubFolders
                GetWordFiles fld.Path
            Next
        End If
    End Sub
    Good luck!

Posting Permissions

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



Click Here to Expand Forum to Full Width