Results 1 to 11 of 11
  1. #1
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2016
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672

    Change File Extension In VBA

    I have a user selecting a file, and I need to change the file extension to .txt

    I have tried


    Code:
    Dim fileName As String
    Dim newName As String
    Dim i As Integer
    
    Debug.Print (Me.txtFileLocation)
    
    Do While fileName <> ""
        i = InStrRev(fileName, ".")
        newName = Left(fileName, i -1) & ".txt"
    Loop
    
    Debug.Print newName
    but this freezes my access and i never have the new filename printed

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Based on that nothing should happen because fileName is "" . I suspect you left out pertinent code in your post.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2016
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by Micron View Post
    Based on that nothing should happen because fileName is "" . I suspect you left out pertinent code in your post.
    haha, sorry

    You are correct.

    I set fileName = Me.Me.txtFileLocation

    above the loop

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,938
    Why don't you just rename the file?

    https://www.google.com/search?q=rena...t=gws-wiz-serp
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    It seems you don't want to reveal too much. It's probably some other portion that is at fault because even if the variable holds a valid value, that code does nothing.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Use FSO. You can take advantage of fso.GetBaseName, fso.getFileExtension, fso.BuildPath, etc.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  7. #7
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2016
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    not trying to be cryptic at all - this is the code I have.

    I've used FSO to change a file name, or create a directory but i'm struggling on how to change a file extension :/

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Code:
    Fpath = "C:\Users\Sam\Desktop\tinkering\Files\SomeFile.accdb"
    
    Debug.Print Replace(Fpath, fso.GetExtensionName(Fpath), "txt")
    Code:
    result:  C:\Users\Sam\Desktop\tinkering\Files\SomeFile.txt
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2016
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by moke123 View Post
    Code:
    Fpath = "C:\Users\Sam\Desktop\tinkering\Files\SomeFile.accdb"
    
    Debug.Print Replace(Fpath, fso.GetExtensionName(Fpath), "txt")
    Code:
    result:  C:\Users\Sam\Desktop\tinkering\Files\SomeFile.txt
    What am I doing wrong here?

    Code:
        Dim fileName As String
        Dim newName As String
        Dim fso As Object
        
        Set fso = CreateObject("scripting.FileSystemObject")
        
        Debug.Print (Me.txtFileLocation)
        fileName = Me.txtFileLocation
        
        newName = Replace(fileName, fso.GetExtension(fileName), "txt")
        
        Debug.Print newName
        
        Set fso = Nothing
    but I get an error of
    Code:
    Object doesn't support this property or method

  10. #10
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Your using fso.GetExtension but it should be fso.GetExtensionName

    Use early binding until it works and then modify it to late binding. Less mistakes that way.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  11. #11
    jo15765's Avatar
    jo15765 is offline Expert
    Windows 10 Access 2016
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    Quote Originally Posted by moke123 View Post
    Your using fso.GetExtension but it should be fso.GetExtensionName

    Use early binding until it works and then modify it to late binding. Less mistakes that way.
    that did it - thank you

Please reply to this thread with any new information or opinions.

Similar Threads

  1. File address and extension
    By kolait in forum Access
    Replies: 15
    Last Post: 08-18-2021, 06:07 PM
  2. Use FileDialog to get file with txt extension
    By newbieX in forum Programming
    Replies: 4
    Last Post: 01-11-2016, 07:28 PM
  3. Need to import .txt file using the extension only.
    By PJ Crittenden in forum Macros
    Replies: 3
    Last Post: 06-24-2014, 07:10 PM
  4. cannot change extension from accdb to accdr
    By everette in forum Access
    Replies: 4
    Last Post: 05-01-2011, 03:47 AM
  5. access file extension help
    By supertech33 in forum Access
    Replies: 2
    Last Post: 02-17-2010, 03:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums