View Full Version : Solved: CopyFolder into Dynamic Parent Directory

01-31-2006, 11:30 AM
I have the following code which is only part of a whole routine. What I'm tyring to do is prompt the user to enter a name for a new directory that I am going to create and then copy two subdirectories into. I have included only one subdirectory since they are just about identical. My problem is that when I try to CopyFolder into the newly created directory, I get the compile error that a constant expression is required. Please help, it's driving me bonkers.

Sub total()
Call NetDirectory
Call CopyFileNetworkWO

End Sub

Sub NetDirectory()
Dim HistoryDir As String

'****Asks the user to input the Dir name, then creates Dir
Load UserForm3
HistoryDir = UserForm3.TextBox1.Value
MkDir "K:\History\2006\" & HistoryDir
End Sub

Sub CopyFileNetworkWO()
Dim HistoryDir As String
HistoryDir = UserForm3.TextBox1.Value

'****This is copying the files from C:\WithoutCost to J:\WithoutCost
Const DirFromM As String = "c:\withoutcost"
Const DirToM As String = "K:\History\2006\" & HistoryDir & "\" 'This is the problem
Dim fso As FileSystemObject
Dim oFSO As Object
Set oFSO = CreateObject("Scripting.FilesystemObject")

oFSO.CopyFolder DirFromM, DirToM

End Sub

01-31-2006, 11:48 AM

Your problem is that your constant is not really a constant--it's a variable, because it's definition relies on HistoryDir, which is itself a variable.

Declare DirToM as a variable, and you should be OK.


01-31-2006, 12:28 PM
Duh! Thank you very much. It works great!