Creating folder in MS Word

EPamperin12

New Member
Joined
Jan 8, 2008
Messages
47
I have a form in MS Word that people will fill out for different schools. I want to be able to create a folder that will be used. I have no problems creating the folder.
My problem is if the folder already exists I get an error saying Path/File Access Error.

Code:
Sub TestingSave()
 Dim oFFs As FormFields
 Dim SchoolName As String
 Dim SchoolCode As String
 Dim Folder As String
 Set oFFs = ActiveDocument.FormFields
 
    oFFs("SchoolName").Select
    SchoolName = Selection
    oFFs("SchoolCode").Select
    SchoolCode = Selection
    Folder = "C:" & SchoolName & " (" & SchoolCode & ")"
    
    If Dir(Folder) = "" Then
        MkDir (Folder)
        oFFs("ReturnValue").Select
        Selection = "New Folder Created"
    End If

SaveAs ("H:" & SchoolName & " (" & SchoolCode & ")\Testing1.doc")
End Sub

Any Help is most appreciated.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi

Firstly let me state I have no experience of using VBA with Word. Right, that out of the way, I would have thought the If test would spec whether or not you have such a folder. If this is not happening, then I suspect that youneed to pass a second argument to Dir:

Code:
If Dir(Folder,vbDirectory) = "" Then
'blah, blah, blah

If this doesn't work, I would just use an On Error Resume Next:

Code:
On Error Resume Next
MkDir Folder
On Error Goto 0

In the example above, you don't need to use an If statement at all...
 
Upvote 0

Forum statistics

Threads
1,225,360
Messages
6,184,508
Members
453,237
Latest member
lordleo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top