+ Reply to Thread
Results 1 to 5 of 5

Detect flash drive letter, save file as PDF

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-19-2012
    Location
    NYC
    MS-Off Ver
    Excel 2010
    Posts
    137

    Detect flash drive letter, save file as PDF

    Hello all,

    The following code is supposed to detect the letter of the flash drive if one is plugged in, then save the active workbook in both .xlsm and .pdf formats:
    Sub usbSave()
    On Error Resume Next
    Dim i As Integer
    Dim PDFfileName As String
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    For i = 67 To 90
    Set drspec = fs.GetDrive(Chr(i))
    If drspec.drivetype = 1 Then
    'PDFfileName = Range("H5").Value
     MsgBox "File saved to USB Drive (" & Chr(i) & ":\)"
    spath = Chr(i) & ":\"
    sFilename = Range("H5").Value & ".xlsm"
    PDFfileName = spath & Range("H5").Value & ".pdf"
    ActiveWorkbook.SaveCopyAs spath & sFilename
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=PDFfileName, openafterpublish:=False
    
    i = 90
    
    End If
    Next i
    End Sub
    The .xlsm feature works, the .pdf part does not. Any ideas?

    Best,

    Willardio

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Detect flash drive letter, save file as PDF

    Get rid of .pdf in this line


    PDFfileName = spath & Range("H5").Value & ".pdf"

  3. #3
    Forum Contributor
    Join Date
    09-19-2012
    Location
    NYC
    MS-Off Ver
    Excel 2010
    Posts
    137

    Re: Detect flash drive letter, save file as PDF

    thanks for the suggestion JieJenn, but it didn't make a difference. Still no PDF file saved on the flash.

  4. #4
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Detect flash drive letter, save file as PDF

    Maybe remove On Error Resume next line and step through each code to see where the error occurs.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Detect flash drive letter, save file as PDF

    Hello Willardio,

    You need to check that the drive is available before you save the file. I have made some changes to your macro. Try this out and let me know the results.
    Sub usbSave()
     
        Dim drspec      As Object
        Dim fs          As Object
        Dim i           As Integer
        Dim PDFfileName As String
        Dim sPath       As String
    
            Set fs = CreateObject("Scripting.FileSystemObject")
    
                For i = 67 To 90
                    On Error Resume Next
                    Set drspec = fs.GetDrive(Chr(i))
                        If drspec.DriveType = 1 And drspec.IsReady Then
                            MsgBox "File saved to USB Drive (" & Chr(i) & ":\)"
                            sPath = Chr(i) & ":\"
                            sFilename = Range("H5").Value & ".xlsm"
                            PDFfileName = sPath & Range("H5").Value & ".pdf"
                            ActiveWorkbook.SaveCopyAs sPath & sFilename
                            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfileName, openafterpublish:=False
                            Exit Sub
                        End If
                    On Error GoTo 0
                Next i
                
            MsgBox "No Removable Drives were Accessible or Plugged In."
            
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Automatically detect and change drive letter
    By JamesT1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-26-2013, 11:54 AM
  2. Replies: 2
    Last Post: 12-03-2012, 01:30 PM
  3. Detect if File is opened on USB Drive
    By tekman in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-02-2008, 09:56 PM
  4. Replies: 3
    Last Post: 07-24-2006, 03:30 PM
  5. [SOLVED] Identifying drive letter with Save-as to a server
    By excelnut1954 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-29-2005, 02:10 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1