Results 1 to 2 of 2

Renaming a file using VBA

  1. #1
    bfaucher is offline Novice
    Windows 7 32bit Access 2000
    Join Date
    Nov 2011
    Location
    Montreal, Canada
    Posts
    1

    Renaming a file using VBA

    Hi,

    I am sort of a beginner here... I have a report that is set to print using PDFCreator: it automatically names the file as a time date stamp. I want to rename the file to include form data in its filename and move it to another directory. Here is what my code looks like:

    Private Sub Command1_Click()
    On Error GoTo Err_Command1_Click

    Dim stDocName As String
    Dim Directory As String
    Dim Newdir As String
    Dim Filename As String
    Dim Newname As String
    Dim oldfile As String
    Dim newfille As String
    Dim Dossier As String

    stDocName = "Dossier privé - Confirmation"
    DoCmd.OpenReport stDocName, acNormal
    Dossier = [Dossier]
    Directory = "C:\adjudex\documents\temp\"
    Newdir = "C:\adjudex\documents\"
    Filename = Directory & Dir("C:\adjudex\documents\temp", vbNormal)
    Newname = Dossier & "Dossier_privé_Confirmation.pdf"
    Name Filename As Newdir & Newname

    Exit_Command1_Click:
    Exit Sub

    Err_Command1_Click:
    MsgBox Err.Description
    Resume Exit_Command1_Click


    When the code is done, the file is in its original name and place, but it somehow created a directory: C:\adjudex\documents\Dossier_privé_Confirmation.pd f



    I do not understand what I am doing wrong. Could someone out there please help me?

    Thank you in advance.

  2. #2
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Change your code like this:

    Code:
    Newdir = "C:\adjudex\documents\"
    Filename = Directory & Dir("C:\adjudex\documents\temp", vbNormal)
    Newname = Dossier & "Dossier_privé_Confirmation.pdf"
    NewFile = Newdir & Newname
    Name Filename As NewFile
    By the way, I would use strFileName and strNewName and strNewFile instead just so you ensure you do not accidentally use any Access Reserved Words as variable names.

    Oh, I just realized that you also don't have the proper name for the original file. You are asking it to rename the DIRECTORY instead. This:

    Filename = Directory & Dir("C:\adjudex\documents\temp", vbNormal)

    Is not correct. You would need the file extension too

    Filename = Directory & Dir("C:\adjudex\documents\temp.pdf", vbNormal)

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

Similar Threads

  1. Renaming fields
    By WilsonsW in forum Access
    Replies: 10
    Last Post: 03-25-2011, 01:20 PM
  2. Renaming Fields on Table or Query
    By jo15765 in forum Access
    Replies: 4
    Last Post: 12-09-2010, 01:47 PM
  3. Replies: 1
    Last Post: 09-27-2010, 10:10 AM
  4. Edit a table in a query by renaming the query?
    By TheWolfster in forum Queries
    Replies: 2
    Last Post: 07-30-2010, 02:57 PM
  5. Upload file inside .mdb database file
    By havish in forum Access
    Replies: 0
    Last Post: 07-30-2008, 07:35 AM

Posting Permissions

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