locked
Filecopy works in Immediate window but not as code lines RRS feed

  • Question

  • Hi

    I have a code like this:

    strFolder = Forms![ALLA VAL]![EgenPathFakt] & "tempBill\"
    strFile = Dir(strFolder & "\*.pdf")
    NewFolder = [Forms]![ALLA VAL]![EgenPathFakt]

    FileCopy strFolder & strFile, NewFolder & strFile

    It does not copy the file. But if I run it, row by row, in Immediate window. Then the code works fine. Why?

    Here is a copy paste from Immediate:

    strFolder = Forms![ALLA VAL]![EgenPathFakt] & "tempBill\"
    debug.Print strFolder
    c:\atest\tempBill\
    strFile = Dir(strFolder & "\*.pdf")
    debug.Print strFile
    Faktura 1 to Supply Company AG.pdf
    NewFolder = [Forms]![ALLA VAL]![EgenPathFakt]
    debug.Print NewFolder
    c:\atest\
    debug.Print strFile
    Faktura 1 to Supply Company AG.pdf
    FileCopy strFolder & strFile, NewFolder & strFile

    And Yes the filecopy works and copy the file


    Cheers // Peter Forss Stockholm

    Wednesday, June 3, 2020 8:16 PM

Answers

  • OK I got it.

    my code lines (as above) is part of a "Sub EmailInvoice_Click()"
    The line before them is "Call Faktura_till_PDFtempbill"

    And there is no return from "Call Faktura_till_PDFtempbill"

    When I move the lines:

    strFolder = Forms![ALLA VAL]![EgenPathFakt] & "tempBill\"
    strFile = Dir(strFolder & "\*.pdf")
    NewFolder = [Forms]![ALLA VAL]![EgenPathFakt]
    FileCopy strFolder & strFile, NewFolder & strFile

    From "Sub EmailInvoice_Click()" to "Call Faktura_till_PDFtempbill" it works.
    Sorry for my misstake!


    Cheers // Peter Forss Stockholm

    Thursday, June 4, 2020 7:32 AM

All replies

  • debug.Print strFile
    Faktura 1 to Supply Company AG.pdf
    FileCopy strFolder & strFile, NewFolder & strFile

    Hi Peter,

    As your strFile contains spaces, you could try to enclose the filespecifications in quotes:

            FileCopy """" & strFolder & strFile & """",""""" & NewFolder & strFile & """"

    Imb.

    Edit: In my applications FileCopy works as expected, using filespecifications without spaces.

    Imb.

    • Edited by Imb-hb Wednesday, June 3, 2020 9:39 PM edit
    Wednesday, June 3, 2020 9:35 PM
  • Hi Imb

    Thanks for helping. I have tried a couple of combinations in immediate window
    1 Yours:
    FileCopy """" & strFolder & strFile & """",""""" & NewFolder & strFile & """"
    Gives: Run time error 52 Bad file name or number.

    FileCopy """" & strFolder & strFile & """,""" & NewFolder & strFile & """"
    Gives: Compile error Argument not optional

    FileCopy "" & strFolder & strFile & "", ""& NewFolder & strFile & ""
    Gives: Run time error 75 Path/File access error.


    Cheers // Peter Forss Stockholm

    Thursday, June 4, 2020 5:02 AM
  • This combination works in immediate window

    FileCopy strFolder & strFile, NewFolder & strFile

    But why does not the same line work as a part of the code flow?????


    Cheers // Peter Forss Stockholm

    Thursday, June 4, 2020 5:05 AM
  • Hi again

    I have change the file name so it is without spaces.

    Now the file name is:

    Faktura_1_to_Supply_Company_AG.pdf

    the Filecopy does not work as a part of the code.

    But it works in Immediate window!


    Cheers // Peter Forss Stockholm

    Thursday, June 4, 2020 7:02 AM
  • OK I got it.

    my code lines (as above) is part of a "Sub EmailInvoice_Click()"
    The line before them is "Call Faktura_till_PDFtempbill"

    And there is no return from "Call Faktura_till_PDFtempbill"

    When I move the lines:

    strFolder = Forms![ALLA VAL]![EgenPathFakt] & "tempBill\"
    strFile = Dir(strFolder & "\*.pdf")
    NewFolder = [Forms]![ALLA VAL]![EgenPathFakt]
    FileCopy strFolder & strFile, NewFolder & strFile

    From "Sub EmailInvoice_Click()" to "Call Faktura_till_PDFtempbill" it works.
    Sorry for my misstake!


    Cheers // Peter Forss Stockholm

    Thursday, June 4, 2020 7:32 AM