Ammending VBA code to create a new folder instead of a word doc/

MParke17

New Member
Joined
Aug 14, 2015
Messages
29
Hi All,

I have this code which creates word document based on the information within a column. It uses the data in the column as the name for the word docs.

I'd like some help to amend the code so that instead of creating word docs it will create a new folder.

Thanks,
Matt

Here's the code:

Private Sub CommandButton1_Click()
Dim i As Integer
Dim wordApp As Object
Dim wordDoc As Object

'Create a Word application object
Set wordApp = CreateObject("Word.Application")

For i = 1 To Me.Range("DocumentList").Rows.Count
'Create the new document
Set wordDoc = wordApp.Documents.Add

'Save the document with the relevant file name
wordDoc.SaveAs ThisWorkbook.Path & "\" & Me.Range("DocumentList").Cells(i).Text & ".doc"

'Setup the hyperlink to the document
With Me.Range("DocumentList").Cells(i).Offset(0, 1)
.Value = "Hyperlink"
.Hyperlinks.Add Me.Range("DocumentList").Cells(i).Offset(0, 1), ThisWorkbook.Path & "\" & Me.Range("DocumentList").Cells(i).Text & ".doc"
End With
Next i

wordApp.Quit

Set wordDoc = Nothing
Set wordApp = Nothing
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi MParke17,

Below is a snippet of code I have used in the past to check if a folder exists and, if not, create it. This particular code has the folder location "hard coded" into it, but I am sure it can be amended to use column values instead. Unfortunately you will need help from someone else to get that amendment in place however...

Code:
    Dim fsoFSO
    Set fsoFSO = CreateObject("Scripting.FileSystemObject")
    If fsoFSO.FolderExists("C:\DBAH") Then
    Else
        fsoFSO.CreateFolder ("C:\DBAH")
    End If
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,236
Members
448,555
Latest member
RobertJones1986

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