PDA

View Full Version : Open Word file failing



Andre205th
03-21-2020, 12:32 PM
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>

Artik
03-21-2020, 03:18 PM
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

Andre205th
03-21-2020, 08:41 PM
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

gmayor
03-21-2020, 09:31 PM
You could investigate https://www.gmayor.com/Batch_Print.htm