VBA to create folder from Excel and save Word document in that folder
I have Excell file from where I create and populate bookmarks in Word document. Now I would like to automatically create new folder in c:\ directory named "Test" and subfolder named like cell G13 and to automatically save created word document in that subfolder. I`m not programmer and all this code is scraped from Internet so it would be great if somebody could write needed code and not just give me instructions. I have found code for creating folder but it requires that folder "Test" is already created under c:\ directory. Is there a way to check if folder "Test" exist and if not to create folder and then to check if subfolder exist and if not to create subfolder named like cell G13.
My code for creating and populating Word document:
Code:
Option Explicit
Sub zapisnikKP()
Dim pappWord As Object
Dim docWord As Object
Dim wb As Excel.Workbook
Dim xlName As Excel.Name
Dim TodayDate As String
Dim Path As String
Set wb = ActiveWorkbook
TodayDate = Format(Date, "mmmm d, yyyy")
Path = wb.Path & "\zapisnikKP.dot"
On Error GoTo ErrorHandler
'Create a new Word Session
Set pappWord = CreateObject("Word.Application")
On Error GoTo ErrorHandler
'Open document in word
Set docWord = pappWord.Documents.Add(Path)
docWord.SaveAs2 "Zapisnik o zaprimanju KP-" & Excel.Names("imePrezimeZrtve").RefersToRange.Value & ".doc"
'Loop through names in the activeworkbook
For Each xlName In wb.Names
'if xlName's name is existing in document then put the value in place of the bookmark
If docWord.Bookmarks.Exists(xlName.Name) Then
docWord.Bookmarks(xlName.Name).Range.Text = Range(xlName.Value)
End If
Next xlName
'Activate word and display document
With pappWord
.Visible = True
.ActiveWindow.WindowState = 0
.Activate
End With
'Release the Word object to save memory and exit macro
ErrorExit:
Set pappWord = Nothing
Exit Sub
'Error Handling routine
ErrorHandler:
If Err Then
MsgBox "Error No: " & Err.Number & "; There is a problem"
If Not pappWord Is Nothing Then
pappWord.Quit False
End If
Resume ErrorExit
End If
End Sub
Code for creating new folder:
Code:
If Len(Dir("c:\Test\" & [G13], vbDirectory)) = 0 Then
MkDir "c:\Test\" & [G13]
End If