Hello, I’m trying to get this macro to work in LibreOffice and erroring at the Create Folder section. ‘Shell “mkdir “”” & sFolderPath & “”, 0’
It’s been a long time since I tinkered with macros.
Would appreciate if anyone could point me in the right direction to troubleshoot this.
Much appreciated
Sub CreateFolders()
Dim oDoc As Object
Dim oSheet As Object
Dim oRootFolder As Object
Dim oFolders As Object
Dim sRootPath As String
Dim i As Integer
'Prompt User to Select Root Folder
oDialog = CreateUnoService("com sun stanui dialogs FolderPicker")
oDialog.setTitle( "Select Location for 'My Home Root Folder")
oDialog .execute()
'Get Selected Path
sRootPath = oDialog .getDirectory()
' Check if user canceled folder selection
If sRootPath = "" Then
MsgBox "Operation canceled by user.", 48, "Canceled"
Exit Sub
End If
' Loop through rows and create folders
For i = 0 To oSheet.Rows.getCount() - 1
' Construct full path for current row
Dim sFolderPath As String
sFolderPath = sRootPath & "/" & oSheet.getCellByPosition(0, i).String & "/"
sFolderPath = sFolderPath & oSheet.getCellByPosition(1, i).String & "/"
sFolderPath = sFolderPath & oSheet.getCellByPosition(2, i).String & "/"
sFolderPath = sFolderPath & oSheet.getCellByPosition(3, i).String
' Create directory if it doesn't exist
If (Not FileExists(sFolderPath)) Then
Shell "mkdir """ & sFolderPath & "", 0
End If
Next i
MsgBox "Folders created successfully.", 64, "Done"
End Sub
Function GetRootFolder() As String
Dim oFileDialog As Object
Dim sPath As String
oFileDialog = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
oFileDialog.setTitle("Select Root Folder")
oFileDialog.setDisplayDirectory("file:///") ' Start at root directory
' Show dialog and get result
If oFileDialog.execute() = 1 Then
sPath = oFileDialog.getFiles()(0)
End If
GetRootFolder = sPath
End Function
Function FileExists(sPath As String) As Boolean
Dim oFile As Object
On Error Resume Next
oFile = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
FileExists = oFile.exists(sPath)
End Function
[erAck: edited to format code as code, see This is the guide - How to use the Ask site? - #6 by erAck]