Consulting

Results 1 to 6 of 6

Thread: Saving files using Relative Filepath Location

  1. #1

    Saving files using Relative Filepath Location

    Hi

    On my excel sheet (located c:\users\documents\branston\savefiles.xlsm) I have a macro which saves some pdf files located elsewhere in my user area. The Filepaths on the excel sheet (pointing to the pdfs) are absolute e.g. c:\files\Test1.pdf, c:\files\Test2.pdf, c:\files\Test3.pdf. No problems saving here.

    However if I move my 'files' folder containing the pdfs to c:\users\documents\branston\files\ and try printing by making the filepaths relative ie. \Test1.pdf, \Test2.pdf, \Test3.pdf.... the files do not save.

    Probably making an obvious error but I've tried various combinations (below) and none work.

    Test1.pdf, Test2.pdf, Test3.pdf....
    \Test1.pdf, \Test2.pdf, \Test3.pdf....
    ..\Test1.pdf, ..\Test2.pdf, ..\Test3.pdf....
    \\Test1.pdf, \\Test2.pdf, \\Test3.pdf....

    Can anyone help?

    Thanks in advance.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Q:
    c:\users\documents\branston\savefiles.xlsm or c:\users\branston\documents\savefiles.xlsm

    I'll assume the latter

    I think you need to change your default folder

    I'd use the third version below since it's a little more general

    ChDir "c:\users\branston\documents\files\" ChDir Environ("USERPROFILE") & "\Documents\files" ChDir CreateObject("Shell.Application").Namespace(CVar(5)).Self.Path & Application.PathSeparator & "files" ...save TEst1.pdf
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Hi Paul

    Erm… I wasn't actually changing anything in the VBA code. Maybe I wasn't clear in my post but I've attached 2 images to show what I mean. The absoluteFilePath image saves the files fine. The second doesn't even though I've copied the topic files into the same folder as my excel sheet. I need it to be completely relative as I'm moving between computers/buildings/servers.

    Some more info below

    c1 is just the range in column D. p2 is the destination folder. Some snippet of codes.

      
    p2 = "C:\users\branston\documents\files\"
      Set rng1 = Worksheets("Main").Range("D5:D" & LastRow)
    For Each c1 In rng1
    FileCopy c1, p2 count = count + 1 ...
    Like I said I have no problems with the absolute filepath and apologies if you've already answered my question in your earlier post(!)
    Attached Images Attached Images

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Well if

    p2 = "C:\users\branston\documents\files"

    and

    Cell D1 = "\files\TOPICS_01.pdf"

    it would seem that your For Each c1 in rng1 loop would be trying to

    FileCopy "\files\TOPICS_01.pdf", "C:\users\branston\documents\files"


    I believe that FileCopy wants the full file name


    So MAYBE you want something like

    FileCopy Thisworkbook.Path & c1, "C:\users\branston\documents" & c1




    Sorry, but I'm still not clear on the source and destination folders
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Thanks Paul.

    If FileCopy wants the full path then that explains it.

    But sorry p2 was meant to be whatever directory exists when I login to another business with my 'files folder'. So it could be c:\etc..etc.\files\ or z:\etc..etc.\files\ or any other drive.

    I will try
    FileCopy Thisworkbook.Path & c1, "C:\users\branston\documents" & c1 but I think I really need FileCopy Thisworkbook.Path & c1, p2 & c1 as I could be on any network drive.

    Apologies if my post is not clear but thank you for your help so far - much appreciated.

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Your post #3 has p2 = .... \files and the col D entry starts with \files\...

    I don't think you need the \files in p2
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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