PDA

View Full Version : msgBox to naming folder



coxonitus
04-29-2011, 05:58 AM
hi guys,

who could help me out.
i'm using this code:

copy Macro
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String

FromPath = "C:\project/test"
ToPath = "G:\Project\test\" '<< Change

'If you want to create a backup of your folder every time you run this macro
'you can create a unique folder with a Date/Time stamp.
'ToPath = "C:\Users\Ron\" & Format(Now, "yyyy-mm-dd h-mm-ss")
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If
Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If
FSO.CopyFolder Source:=FromPath, Destination:=ToPath
MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End Sub

What i want is, that the user could get the option with a messagebox, if the enter their name, de folder will be automatically stored in the right place
lets say: G\project\peter
and another user : G\project\charlie

could anyone please help me out here.

many thnx

Kenneth Hobs
04-29-2011, 06:26 AM
You can use an InputBox() to get a string or name in this case. Other options to not prompt the user would be to use Environ("username") or Application.UserName. I prefer the Environ() method. Another option would be to use a browse dialog and let them click their folder from the G:\Projects root folder.

coxonitus
04-29-2011, 06:28 AM
hi kenneth,
thanx for reacting.
could u help me out with the environ, ik think that would be a better option.

thnx

Kenneth Hobs
04-29-2011, 06:41 AM
Sub fsoFun()
' copy Macro
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String

FromPath = "C:\project\test"

'debug.print Environ("username")
Select Case Environ("username")
Case "umtr121"
ToPath = "G:\Project\Ken\"
Case "umtr122"
ToPath = "G:\Project\Pat\"
Case Else
ToPath = "G:\Project\Misc\"
End Select
'ToPath = "G:\Project\test\" '<< Change

'If you want to create a backup of your folder every time you run this macro
'you can create a unique folder with a Date/Time stamp.
'ToPath = "C:\Users\Ron\" & Format(Now, "yyyy-mm-dd h-mm-ss")
If Right(FromPath, 1) = "\" Then
FromPath = Left(FromPath, Len(FromPath) - 1)
End If
If Right(ToPath, 1) = "\" Then
ToPath = Left(ToPath, Len(ToPath) - 1)
End If

Set FSO = CreateObject("scripting.filesystemobject")
If FSO.FolderExists(FromPath) = False Then
MsgBox FromPath & " doesn't exist"
Exit Sub
End If

FSO.CopyFolder Source:=FromPath, Destination:=ToPath
MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath
End Sub

coxonitus
04-29-2011, 11:31 AM
hi Kenneth,
i just pasted your code, but it doesn't select the username.
actually it does the same as before, maybe i overlooked something.

my idea:
is that a popup appears, put in the username, and then it will copy the file in that particular folder. I have to install that for the user, if it's possible offcourse?

could you help me out here

Kenneth Hobs
04-29-2011, 12:07 PM
Obviously, you would have to change the Select Cases to meet your people's usernames. You can uncomment the debug line to see what your username is in the Immediate Window or in the Immediate Window type:
?Environ("username") and press Enter key.

Another way to get a username is by Windows Start > Run > %username% > OK.

For the InputBox method:
ToPath = InputBox("Type your name for the folder To.", "ToFolder Name")
ToPath = "G:\Project\" & ToPath & "\"
MsgBox ToPath