+ Reply to Thread
Results 1 to 6 of 6

Using Excel userForm to upload a document into a library

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-02-2005
    Posts
    100

    Using Excel userForm to upload a document into a library

    Hi all,

    I'm looking for a way for a userForm to allow a user to browse to a file and then have excel save the selected file to a particular path.

    I've puzzled out the following so far:

    Sub getAndSaveFile()
    
    Dim userFilePath As String
    Dim libraryFilePath As String
    Dim typeSelect As Integer
    
    userFilePath = Application.GetOpenFilename
    
    If typeSelect = 1 Then
        libraryFilePath = "H:\TEMP\Lib1\"
    ElseIf typeSelect = 2 Then
        libraryFilePath = "H:\TEMP\Lib2\"
    ElseIf typeSelect = 3 Then
        libraryFilePath = "H:\TEMP\Lib3\"
    Else
        libraryFilePath = "H:\TEMP\"
    End If
    
    'insert code to save the selected file to the selected file path
    FileCopy userFilePath, libraryFilePath
    
    end sub
    My problem is thus:

    The Application.GetOpenFilename method gets the full file path of the selected file, which is great for the FileCopy userFilePath variable.

    But how do I truncate the filePath string to include only the file name so I can put it in the expression for the copy destination, i.e.:

    FileCopy userFilePath, libraryFilePath & fileName

    Must I directly modify the string to remove everything (and including) the right-most "/" character in the userFilePath string, or is there a way to modify the Application.GetOpenFilename method to return just the selected file name and not the full file path?

    Thanks!
    -o
    Last edited by Ouka; 06-28-2011 at 01:54 PM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,378

    Re: Using Excel userForm to upload a document into a library

    Hi Ouka,

    Read http://www.ozgrid.com/VBA/GetExcelFileNameFromPath.htm
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Contributor
    Join Date
    06-02-2005
    Posts
    100

    Re: Using Excel userForm to upload a document into a library

    Ah, so string truncation it is.

    Was hoping for a pre-existing method like the one that is available for returning just the file name of the currently open excel file.

    eg:

    ThisWorkbook.Name
    vs
    ThisWorkbook.FullName

    Thanks for reply.

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

    Re: Using Excel userForm to upload a document into a library

    Hello Ouka,

    here is a much easier way using the Dir Statement.
    Sub getAndSaveFile()
    
      Dim FileName As String
      Dim userFilePath As String
      Dim libraryFilePath As String
      Dim typeSelect As Integer
    
        userFilePath = Application.GetOpenFilename
        If userFilePath = "False" Then Exit Sub
        
        If typeSelect = 1 Then
           libraryFilePath = "H:\TEMP\Lib1\"
        ElseIf typeSelect = 2 Then
           libraryFilePath = "H:\TEMP\Lib2\"
        ElseIf typeSelect = 3 Then
           libraryFilePath = "H:\TEMP\Lib3\"
        Else
           libraryFilePath = "H:\TEMP\"
        End If
    
     'insert code to save the selected file to the selected file path
      FileName = Dir(userFilePath)
      FileCopy userFilePath, libraryFilePath & FileName
    
    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!)

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,378

    Re: Using Excel userForm to upload a document into a library

    Does
    ThisWorkbook.Path
    help any?

  6. #6
    Forum Contributor
    Join Date
    06-02-2005
    Posts
    100

    Re: Using Excel userForm to upload a document into a library

    perfect, that was exactly what I was looking for. Both methods work but I'll go with the simple one!

    Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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