Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Save workbook To any USB Drive no matter what drive letter

Hi all, I have tried the search and cannot get any of the other VBA
macros to work re this subject!!

I want to be able to press a button on a sheet on my workbook and have
a macro save the workbook to a USB drive which could come up as either
drive D - E or F. No prompts just save a copy.

Any help at all would be appreciated immensely as I dont have a clue
re VBA. I'm sure that someone must have code that works.

This is the last thing I need to complete the project.

Thanks in advance
Stephen
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 202
Default Save workbook To any USB Drive no matter what drive letter

Try this user defined macro code assigned to a button and once it works for
you simply put your saving code in place of the msgbox code.

Sub Get_Drive_Info()
On Error Resume Next
Dim i As Integer

Set fs = CreateObject("Scripting.FileSystemObject")
For i = 67 To 90
Set drspec = fs.GetDrive(Chr(i))
If drspec.drivetype = 1 Then
MsgBox "Drive " & Chr(i) & " is the removable drive"
i = 90
End If
Next i
End Sub

"pano" wrote:

Hi all, I have tried the search and cannot get any of the other VBA
macros to work re this subject!!

I want to be able to press a button on a sheet on my workbook and have
a macro save the workbook to a USB drive which could come up as either
drive D - E or F. No prompts just save a copy.

Any help at all would be appreciated immensely as I dont have a clue
re VBA. I'm sure that someone must have code that works.

This is the last thing I need to complete the project.

Thanks in advance
Stephen

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Save workbook To any USB Drive no matter what drive letter

Using Dennis's Code to get the Drive letter you can use this:

Open the Workbook using Alt+F11 and create a new Workbook_BeforeSave event
and paste the following into the Event SUB

Dim sPath As String
Dim sFilename As String
Dim i As Integer

Application.ScreenUpdating = False
On Error Resume Next

'Backup to Flashdrive
Set fs = CreateObject("Scripting.FileSystemObject")
For i = 67 To 90
Set drspec = fs.GetDrive(Chr(i))
If drspec.drivetype = 1 Then

sPath = Chr(i) & ":\Backups\"
sFilename = "Personal Action Planner " & _
Format(DateSerial(Year(Date), Month(Date), _
Day(Date)), "dd MM yy") & ".xls"

ActiveWorkbook.SaveCopyAs sPath & sFilename

i = 90
End If
Next i

Application.ScreenUpdating = True

'Regards Dylan

"pano" wrote:

Hi all, I have tried the search and cannot get any of the other VBA
macros to work re this subject!!

I want to be able to press a button on a sheet on my workbook and have
a macro save the workbook to a USB drive which could come up as either
drive D - E or F. No prompts just save a copy.

Any help at all would be appreciated immensely as I dont have a clue
re VBA. I'm sure that someone must have code that works.

This is the last thing I need to complete the project.

Thanks in advance
Stephen

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Save workbook To any USB Drive no matter what drive letter

Trouble is a Floppy is also considered as "removable". If fitted, assuming
the floppy is always A or B here is a simplified routine that checks "types"
of drive letters from D-Z. Of course there are other types of removable
drives besides floppy & USB.

Private Declare Function GetDriveType Lib "kernel32" _
Alias "GetDriveTypeA" (ByVal nDrive As String) As Long

Private Const DRIVE_REMOVABLE As Long = 2
'Private Const DRIVE_FIXED = 3
'Private Const DRIVE_REMOTE = 4 ' eg network
'Private Const DRIVE_CDROM = 5
'Private Const DRIVE_RAMDISK = 6

Function Get1stRemovableAfterC(sDrive) As Boolean
Dim nDriveType As Long
Dim i As Long
For i = Asc("D") To Asc("Z")
sLetter = Chr(i) & ":\"
nDriveType = GetDriveType(sLetter)
If nDriveType = DRIVE_REMOVABLE Then
sDrive = sLetter
Get1stRemovableAfterC = True
Exit Function
End If
Next
End Function


Sub test()
Dim sDrive As String

If Get1stRemovableAfterC(sDrive) Then
MsgBox sDrive
Else
MsgBox "not found"
End If

End Sub

Regards,
Peter T



"Nigel" wrote in message
...
The following link shows how to get the first CD-ROM drive, these have an

ID
of 5; a removable drive is ID = 2 so change the code to suit.

http://support.microsoft.com/kb/q180766/


--

Regards,
Nigel




"pano" wrote in message
...
Hi all, I have tried the search and cannot get any of the other VBA
macros to work re this subject!!

I want to be able to press a button on a sheet on my workbook and have
a macro save the workbook to a USB drive which could come up as either
drive D - E or F. No prompts just save a copy.

Any help at all would be appreciated immensely as I dont have a clue
re VBA. I'm sure that someone must have code that works.

This is the last thing I need to complete the project.

Thanks in advance
Stephen






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Save workbook To any USB Drive no matter what drive letter

Hi, this is the macro which I used before the security USB drives came
in which used to be always D drive for the USB key.
I have tried to incorporate it into the Macro Dennis gave to no avail.
Your code Denis does give me the MSG BOX up which knows that it is D
or E, but from their I'm lost
Any Help appreciated

Sub AASAVETOSTICK()
Application.DisplayAlerts = False
ChDir "D:\"
ActiveWorkbook.SaveAs Filename:="D:\July.xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="",
ReadOnlyRecommended:=False _
, CreateBackup:=True
Application.DisplayAlerts = True
End Sub

DENNIS MACRO

Sub Get_Drive_Info()
On Error Resume Next
Dim i As Integer


Set fs = CreateObject("Scripting.FileSystemObject")
For i = 67 To 90
Set drspec = fs.GetDrive(Chr(i))
If drspec.drivetype = 1 Then
MsgBox "Drive " & Chr(i) & " is the removable drive"
i = 90
End If
Next i
End Sub


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Save workbook To any USB Drive no matter what drive letter

Peter

Good point! External CDwriters, digital cameras etc. are also classed as USB
Mass storage devices.

Dylan

"Peter T" wrote:

Trouble is a Floppy is also considered as "removable". If fitted, assuming
the floppy is always A or B here is a simplified routine that checks "types"
of drive letters from D-Z. Of course there are other types of removable
drives besides floppy & USB.

Private Declare Function GetDriveType Lib "kernel32" _
Alias "GetDriveTypeA" (ByVal nDrive As String) As Long

Private Const DRIVE_REMOVABLE As Long = 2
'Private Const DRIVE_FIXED = 3
'Private Const DRIVE_REMOTE = 4 ' eg network
'Private Const DRIVE_CDROM = 5
'Private Const DRIVE_RAMDISK = 6

Function Get1stRemovableAfterC(sDrive) As Boolean
Dim nDriveType As Long
Dim i As Long
For i = Asc("D") To Asc("Z")
sLetter = Chr(i) & ":\"
nDriveType = GetDriveType(sLetter)
If nDriveType = DRIVE_REMOVABLE Then
sDrive = sLetter
Get1stRemovableAfterC = True
Exit Function
End If
Next
End Function


Sub test()
Dim sDrive As String

If Get1stRemovableAfterC(sDrive) Then
MsgBox sDrive
Else
MsgBox "not found"
End If

End Sub

Regards,
Peter T



"Nigel" wrote in message
...
The following link shows how to get the first CD-ROM drive, these have an

ID
of 5; a removable drive is ID = 2 so change the code to suit.

http://support.microsoft.com/kb/q180766/


--

Regards,
Nigel




"pano" wrote in message
...
Hi all, I have tried the search and cannot get any of the other VBA
macros to work re this subject!!

I want to be able to press a button on a sheet on my workbook and have
a macro save the workbook to a USB drive which could come up as either
drive D - E or F. No prompts just save a copy.

Any help at all would be appreciated immensely as I dont have a clue
re VBA. I'm sure that someone must have code that works.

This is the last thing I need to complete the project.

Thanks in advance
Stephen





  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Save workbook To any USB Drive no matter what drive letter

Is anyone able to help with joing the code together so it works????

Thanks
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 30
Default Save workbook To any USB Drive no matter what drive letter

For all who have searched the archives and the net & found nothing
complete.
The following code works. Will save the excel file to personal.xls on
your USB drive no matter what letter drive it is.

Sub Save_to_usb_drive()
On Error Resume Next
Dim i As Integer
Set fs = CreateObject("Scripting.FileSystemObject")
For i = 67 To 90
Set drspec = fs.GetDrive(Chr(i))
If drspec.drivetype = 1 Then

' MsgBox "Drive " & Chr(i) & " is the removable drive"
sPath = Chr(i) & ":\"
sFilename = "Personal.xls " & _
Format(DateSerial(Year(Date), Month(Date), _
Day(Date)), "dd MM yy") & ".xls"
ActiveWorkbook.SaveCopyAs sPath & sFilename
i = 90

End If
Next i
End Sub


Thanks members for all your help and suggestions.....
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Save workbook To any USB Drive no matter what drive letter

Pano

It also adds the current date to the filename.

sFilename = "Personal.xls " & _
Format(DateSerial(Year(Date), Month(Date), _
Day(Date)), "dd MM yy") & ".xls"

Regards
Dylan

"pano" wrote:

For all who have searched the archives and the net & found nothing
complete.
The following code works. Will save the excel file to personal.xls on
your USB drive no matter what letter drive it is.

Sub Save_to_usb_drive()
On Error Resume Next
Dim i As Integer
Set fs = CreateObject("Scripting.FileSystemObject")
For i = 67 To 90
Set drspec = fs.GetDrive(Chr(i))
If drspec.drivetype = 1 Then

' MsgBox "Drive " & Chr(i) & " is the removable drive"
sPath = Chr(i) & ":\"
sFilename = "Personal.xls " & _
Format(DateSerial(Year(Date), Month(Date), _
Day(Date)), "dd MM yy") & ".xls"
ActiveWorkbook.SaveCopyAs sPath & sFilename
i = 90

End If
Next i
End Sub


Thanks members for all your help and suggestions.....

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Import from MS Access - Lose the drive letter..link only to drive Cyhill Excel Programming 6 September 17th 07 06:42 PM
Obtain drive letter assignment of CD/DVD drive? EagleOne Excel Discussion (Misc queries) 1 October 13th 06 01:27 PM
Can I save to hard drive AND my flash drive at the same time? Gizelle Excel Discussion (Misc queries) 3 July 24th 06 08:27 PM
Save to hard drive and backup to thumb drive. sungen99[_34_] Excel Programming 22 January 27th 06 01:17 PM
Identifying drive letter with Save-as to a server excelnut1954 Excel Programming 4 December 29th 05 05:59 PM


All times are GMT +1. The time now is 03:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"