Share via

VBA code to delete a pdf file, using the path

Anonymous
2020-08-18T14:35:18+00:00

Hi, I would very much appreciate help with the following:

I have a form with comboboxes to filter filter the information of a table, and  generate a report. After I generate the report, I automatically create also a pdf file and save it in a folder in the desktop.

If I make modifications to the report, I  create a pdf with the same path that will replace the original,  wit the editions.

I also have a control to delete the report.  But I dont know what is the vba code that I need to use to delete also the pdf file associated to the report that I deleted.

In other words, I need help with the correct code to delete a pdf file, giving as parameter the path of the file.

Looking forward to any kind help on this. Best regards.  Glenda

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2020-08-18T19:22:16+00:00

If txboxPath contains the path + filename of the PDF file, the lines

Dim strpath As String

strpath = Me.txboxPath

Kill strpath

should work. Do you get an error message? If so, what does it say?

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

Anonymous
2020-08-18T17:42:29+00:00

If you already have the variable setup you can just substitute "PathToFile" for the variable name

Kind Regards,

Elise

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author

ScottGem 68,830 Reputation points Volunteer Moderator
2020-08-18T16:38:49+00:00

You don't need to use the File System Object as Elise suggested. The Kill statement is easier.

But you don't say why using Hans' suggestion didn't work. 

dim strpath as string

strpath = me.txboxPath

Kill strpath

should have worked. Also 

Kill me.txboxPath

should work.

Kill won't work however, if the file is open so knowing what happened when you execute the code will help us help further.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Anonymous
    2020-08-18T15:09:53+00:00

    Hi, I'm Elise, an independent advisor and I'd be happy to help with your issue.

    You need to add a reference to the Scripting.Runtime library, then you can use this code:

    Dim fso as New FileSystemObject, aFile as File
    
    if (fso.FileExists("PathToFile")) then
        aFile = fso.GetFile("PathToFile")
        aFile.Delete
    End if
    

    See this thread for further examples:

    https://stackoverflow.com/questions/67835/delet...

    Please let me know if you need any further assistance.

    Kind Regards,

    Elise

    Note: This is a non-Microsoft website. The page appears to be providing accurate, safe information. Watch out for ads on the site that may advertise products frequently classified as a PUP (Potentially Unwanted Products). Thoroughly research any product advertised on the site before you decide to download and install it.

    Was this answer helpful?

    0 comments No comments
  2. HansV 462.6K Reputation points
    2020-08-18T14:58:55+00:00

    To delete a file use

    Kill "path-and-filename"

    For example:

    Kill "C:\Reports\Test.pdf"

    Was this answer helpful?

    0 comments No comments