Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Check if folder exists and create if not

864 views
Skip to first unread message

Andrew Hancock

unread,
Jun 3, 2001, 4:31:09 PM6/3/01
to
I have a userform on a word template that gives the path for saving the
document in a text box (tbfodername) I would like to insert a command button
which the user could press which would check if the folder
(tbfoldername.text) existed and if not create the folder. I have tried
altering the help file examples and Microsoft word MVP FAQ samples but I am
not having a lot of luck. Any help would be much appreciated.

Thanks, Andrew.


Jonathan West

unread,
Jun 3, 2001, 6:50:19 PM6/3/01
to
Hi Andrew,

There is a routine that you can use at
http://www.mvps.org/vbnet/code/fileapi/folderexists.htm

It is designed for VB rather than VBA, but it should work just fine if you
paste the relevant code in.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email

"Andrew Hancock" <and...@hancockhome.fsnet.co.uk> wrote in message
news:OXWWWsG7AHA.1340@tkmsftngp04...

Jay Freedman

unread,
Jun 3, 2001, 7:10:00 PM6/3/01
to
Hi, Andrew,

Here's a demo. On a userform, put a textbox named tbFolderName, a label
named Label1, and two command buttons named btnCheckIt and btnQuit. Put this
code on the userform's code sheet:

Private Sub btnCheckIt_Click()
Dim fso As Object

If tbFolderName.Text = "" Then Exit Sub

On Error GoTo ErrHdl

' Need a FileSystemObject...
Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FolderExists(tbFolderName.Text) Then
Label1.Caption = "The folder already exists"
Else
' Try to create the folder. This may
' trigger the error handler.
MkDir tbFolderName.Text
Label1.Caption = "Created the folder"
End If
Exit Sub

ErrHdl:
Label1.Caption = Err.Description
End Sub

Private Sub btnQuit_Click()
Unload Me
End Sub

Regards,
Jay

Andrew Hancock <and...@hancockhome.fsnet.co.uk> wrote in message
news:OXWWWsG7AHA.1340@tkmsftngp04...

Mark Tangard

unread,
Jun 3, 2001, 10:05:31 PM6/3/01
to

Jay, I get an error "ActiveX component can't create object" (in the
label, so that part's obviously working). Does this procedure make
use of the Windows Scripting ______ (forgot the last word of whatever
that feature is, but I know it's disabled on this machine)?

Mark

-- Mark Tangard <mtan...@speakeasy.net> ----------------------------
------ WWW: http://www.speakeasy.org/~mtangard ----------------------
------------- "Life is nothing if you aren't obsessed." --John Waters
---------------------------------------------------------------------

Jay Freedman

unread,
Jun 3, 2001, 11:11:28 PM6/3/01
to
Hi, Mark,

Yep, it uses WSH (Windows Scripting Host), probably from wscript.exe or
cscript.exe -- VBA doesn't tell you where it gets these things. And yes, WSH
is considered a security risk because the script kiddies are getting good at
hiding .vbs files as other file types and e-mailing them around the world,
so many folks disable the script engine. The alternative is the API version
that Jonathan pointed to.

Regards,
Jay

Mark Tangard <mtan...@speakeasy.net> wrote in message
news:3B1AECEB...@speakeasy.net...

Andrew Hancock

unread,
Jun 4, 2001, 2:59:26 PM6/4/01
to
Thanks Jay, your version has given me a good head start and works on my
machine. I will customise it to my requirements and test on some end users
to ensure they do not get that error Mark reported. I hope not, I will need
quite a few stiff drinks before I start on the API solution.

Code for other users.

Private Sub btnCheckIt_Click()
Dim fso As Object

If tbFolderName.Text = "" Then Exit Sub

On Error GoTo ErrHdl

' Need a FileSystemObject...
Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FolderExists(tbFolderName.Text) Then
Label1.Caption = "The folder already exists"
Else
' Try to create the folder. This may
' trigger the error handler.
MkDir tbFolderName.Text
Label1.Caption = "Created the folder"
End If
Exit Sub

ErrHdl:
Label1.Caption = Err.Description
End Sub

Private Sub btnQuit_Click()
Unload Me
End Sub

Andrew Hancock <and...@hancockhome.fsnet.co.uk> wrote in message
news:OXWWWsG7AHA.1340@tkmsftngp04...

0 new messages