Open Word Document VBA

  • Thread starter FIRSTROUNDKO via OfficeKB.com
  • Start date
F

FIRSTROUNDKO via OfficeKB.com

Hi!

I am trying to adapt some code for my needs however, whilst the message is
correct.

An error message says the file cannot be found?

Thanks for you help in advance.

Sub Something()
Dim cCol As Collection
Dim sStr As String
Dim lCnt As Long
Set cCol = New Collection
sStr = Dir("C:\man\*.doc", vbNormal)
While sStr <> ""
cCol.Add sStr
sStr = Dir
Wend

For lCnt = 1 To cCol.Count
MsgBox cCol.Item(lCnt)
Documents.Open cCol.Item(lCnt)
' your code, beware of what you are doing
ActiveDocument.Close
Next

End Sub
 
O

old man

Hi,

I just ran your code against a folder with fifteen word documents and it
worked fine. I had one document that was read only and it prompted me as to
how to open it and it worked.

You should add error handling to your code such as this (error handling is
quite powerful and you can should investigate what you want to do when your
program encounters a problem - you may want to stop or resume opening the
remaining
files in the folder):

on error goto errhandle1
....

exit sub

errhandle1:

msgbox "Error in opening file: " & cCol.Item(lCnt) & vbcrlf & "Error - "
& err.number & " - " & err.description,vbCritical,"Error in opening group of
Word Docs"

end sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top