Error in a line of vba code (1 Viewer)

gstylianou

Registered User.
Local time
Today, 17:07
Joined
Dec 16, 2013
Messages
357
Hello,

I'm trying to create a function so to copy from master folder another subfolder into C:\Program Files (x86) but i have some errors. First of all following is the function


Public Function CopyFolderAndSetPermissions(ByVal sourceFolder As String, ByVal targetFolder As String, ByVal permissionLevel As String)

Dim objFSO As Scripting.FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFolder As Object
objFolder = "C:\Program Files (x86)\NutriSoft" Here is the error 91 Object variable or with block variables not set


permissionLevel = "Users - Full"
targetFolder = "C:\NutriSetup Plus_v2.0\Resources\APP\NutriSoft"

'Check if the folder exists
If objFSO.FolderExists(targetFolder) Then
'Copy the Nutrisoft folder to C:\Program Files (x86)
objFSO.CopyFolder sourceFolder, targetFolder

'Get the newly copied folder
Set objFolder = objFSO.GetFolder(targetFolder)

'Set permissions for the folder
objFolder.SetPermissions "Everyone", permissionLevel
Else
MsgBox "The folder " & targetFolder & " does not exist!"
End If
End Function

I tried to call the function from Form1 using the following code

Private Sub Command0_Click()
Call CopyFolderAndSetPermissions("C:\NutriSetup Plus_v2.0\Resources\APP\NutriSoft", "C:\Program Files (x86)", "admin")
End Sub

Please help..
 
Last edited:

Minty

AWF VIP
Local time
Today, 15:07
Joined
Jul 26, 2013
Messages
10,371
🤦‍♂️Sigh - the error code is what? and on what line?
Please edit your post to use code tags. and intend it correctly as well.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:07
Joined
Oct 29, 2018
Messages
21,473
Before you can use an object variable, you have to Set it first.
 

Minty

AWF VIP
Local time
Today, 15:07
Joined
Jul 26, 2013
Messages
10,371
The folder is a string path, not an Object, so that might be a starting point.
If it was an object you would have to use
SET...
 

Users who are viewing this thread

Top Bottom