Consulting

Results 1 to 4 of 4

Thread: Open Word file failing

  1. #1

    Open Word file failing

    My problem is, when I try to open the Word files I get a Runtime Error 424: Object Required.
    Any ideas?


    Private Sub CommandButton1_Click()

    Dim fso As Object
    Set fso = CreateObject("Scripting.filesystemobject")

    Dim path As String
    path = "C:\users\me\Desktop\here"

    Dim ofolder As Object
    Set ofolder = fso.GetFolder(path)

    Dim osubfolders As Object
    Set osubfolders = ofolder.subfolders


    For Each osubfolders In ofolder.subfolders

    Dim ofiles As Object

    MsgBox (osubfolders.Name) 'correct subfolder

    For Each ofiles In osubfolders.Files

    MsgBox (ofiles.Name) 'correct file name

    Dim wrd As Object
    Set wrd = CreateObject("word.application")
    wrd.Visible = True 'opens Word

    Dim doc As Object
    Set doc = wrd.Documents

    doc.Open (path & "" & osubfolder.Name & "" & ofiles.Name) '******Runtime Error 424: Object Requirement

    Exit Sub 'for testing

    Next ofiles

    Next osubfolders

    End Sub
    </code>

  2. #2
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    Private Sub CommandButton1_Click()
    
        Dim fso         As Object
        Dim path        As String
        Dim ofolder     As Object
        Dim osubfolder  As Object
        Dim ofile       As Object
        Dim wrd         As Object
        Dim docs        As Object
    
    
        Set fso = CreateObject("Scripting.filesystemobject")
    
    
        path = "C:\users\me\Desktop\here"
    
    
        If Right(path, 1) <> Application.PathSeparator Then
            path = path & Application.PathSeparator
        End If
    
    
        Set ofolder = fso.GetFolder(path)
    
    
    
    
        For Each osubfolder In ofolder.subfolders
    
    
            MsgBox (osubfolder.Name)    'correct subfolder
    
    
            For Each ofile In osubfolder.Files
                
                If LCase(fso.GetExtensionName(ofile.path)) Like "doc*" Then
                    
                    MsgBox (ofile.Name)    'correct file name
    
    
                    Set wrd = CreateObject("word.application")
                    wrd.Visible = True    'opens Word
    
    
                    Set docs = wrd.Documents
    
    
                    docs.Open (ofile.path)
    
    
                    Exit Sub    'for testing
    
    
                End If
    
    
            Next ofile
    
    
        Next osubfolder
        
        
        Set wrd = Nothing
        Set fso = Nothing
    End Sub
    Artik

  3. #3
    Thanks for doc.open(ofiles.path).

    Follow up question:
    I want to print all these files. Is there a better way? Right now I have to open a file, print, then close and repeat. It'll take a while.
    What about Shell? Is it faster? Is there anything else out there that can do this job.
    Thanks again

  4. #4
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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