Progress bar - copying a file

yoko

Active Member
Joined
Sep 5, 2006
Messages
349
Hi,

I've got this code which copies a file from one location to another. Would it be possible to include a progress bar to show how far its complete as the file is quite large.

The code is...

Code:
Private Sub CommandButton1_Click()

If FileExists("C:\Archive.pst") Then
    cpy_email.Caption = "Copying..."
    CommandButton1.Caption = "Please wait"
    CommandButton1.Enabled = False
    CommandButton2.Enabled = False
    Application.Wait Now + TimeValue("00:00:01")
    On Error GoTo Err_FileInUse
    FileCopy "C:\Archive.pst", "M:\email\Archive.pst"
    Application.Wait Now + TimeValue("00:00:01")
    cpy_email.Caption = "Done"
    MsgBox "Done!", vbOKOnly, "Finished"
    CommandButton2.Enabled = True
    GoTo End_Sub:

Else
     MsgBox "File not found!", vbOKOnly, "Error!"
End If

Err_FileInUse:
            MsgBox "File is in use!", vbOKOnly, "Error"
            CommandButton2.Enabled = True
            CommandButton1.Enabled = True
            CommandButton1.Caption = "Backup!"
End_Sub:
End Sub

Many thanks,
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I realize this is over 2 weeks old but...

No, you cannot show a progress bar for the FileCopy statement. As far as VBA is concerned this is a single indivisible act and one cannot show a progress bar for a single VBA statement. Of course, if FileCopy itself shows a progress bar you will see one. Even if that is the case, you will have no control over the display of the progress bar.
 
Upvote 0
Whilst you can't get the detailed progress with the FileCopy statement
you can invoke Windows shell to give you this progress.

I won't go into great detail with this code but incorporate it into your routine.

Code:
Sub ShellCopy()

Set objShell = CreateObject("Shell.Application")
'//The source Folder to CopyFrom:
Set objFolder = objShell.NameSpace("C:\B")

'//The source Folder to CopyTo:
objFolder.CopyHere "C:\A_Book", &H0&

Set objShell = Nothing
Set objFolder = Nothing


MsgBox "The rest of my code is running NOW!!"


End Sub
 
Upvote 0
Progress Bar - Copy File

Hi,
When you copy large files in windows a dialogue box come up that shows a progress bar. Is there a way to call that box up when using the copyfolder command?

Also in Ivan's code:
Set objFolder = objShell.NameSpace("C:\B")
I used my folder and path after namespace but objFolder = nothing, but I know the folder in there, is using namespace correct?

dforgacs
 
Upvote 0
The problem is that the program is just to copy a file. I actually hide excel when the file is opened so that just a form with two buttons is displayed. Copy and Exit. When I click copy it just freezes until the files copied.

I just wanted something to come up and display that it was doing something and give an idea of how longs its going to take.

Cheers
 
Upvote 0
Progress Bar

Hi,
I figured out what was wrong, I used a string variable set to the file name with path, once I typed in the exact path in namespace it worked.

Is it possible to have the windows copy message box with progress bar come up while the file is being copied?

Thanks!
dforgacs
 
Upvote 0
The problem is that the program is just to copy a file. I actually hide excel when the file is opened so that just a form with two buttons is displayed. Copy and Exit. When I click copy it just freezes until the files copied.

I just wanted something to come up and display that it was doing something and give an idea of how longs its going to take.

Cheers

Yoko, when you say "freezes" does it do this using the code I gave ?
It shouldn't freeze using the above code, it should display the Windows copy dialog. What OS are you using ?
 
Upvote 0
Re: Progress Bar

Hi,
I figured out what was wrong, I used a string variable set to the file name with path, once I typed in the exact path in namespace it worked.

Is it possible to have the windows copy message box with progress bar come up while the file is being copied?

Thanks!
dforgacs

I thought as much.

You cannot use string variables when using this Method.
The variables need to be Variant.
 
Upvote 0
Progress Bar

Ivan,
I thought I had it working but the code runs and the message box comes up but it doesn't copy anything, I'll attach my code. I'm copying from My I:\ drive to my C:\ drive. I've used variants. Maybe you could take a look and tell me where I'm off. Thanks!
dforgacs

Code:
Option Explicit

Public Sub Workbook_Open()
Dim mypath As Variant
Dim newpath As Variant
Dim newpath2 As Variant
Dim mypath2 As Variant
Dim objshell As Object
Dim objfolder As Object


mypath = Application.ActiveWorkbook.Path
newpath = Windows.Application.DefaultFilePath
newpath2 = Left(newpath, 3) & "SLTS Exttract Database"
mypath2 = mypath & "\SLTS Extract Database"

Set objshell = CreateObject("Shell.Application")

Set objfolder = objshell.NameSpace(mypath2)

objfolder.CopyHere newpath2, &H0&

Set objshell = Nothing
Set objfolder = Nothing

MsgBox "The rest of my code is running now!!"

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,338
Messages
6,124,358
Members
449,155
Latest member
ravioli44

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