Solved Not able to delete files in folder (1 Viewer)

MarioM

New member
Local time
Today, 16:09
Joined
Jul 14, 2020
Messages
6
Hi guys!

I am working with some PDF's who are merged by some .dll. Merging process works fine, but if I want, for example, do the process again, I want old files (there are 3 of them) to be overwritten by the new ones, I am getting an error when an old file is about to be deleted (on kill method),

the error says: Run-time error 70, permission denied

I tried to use setAttr myFile, vbNormal before kill method, still getting the same error.
Also when I tried to delete files manually in a directory I get the message that file is open in Microsoft access, so at least I know why I can't delete it, but I DON'T KNOW when access opens those 3 files. In my DoCmd.OutputTo line there is also "False" on Autostart argument...

Files seem to be overwritten only when I reopen whole access application.

Here is my code where the problem occurs:

strFile = GetAppPath & stDocName & "_racunid=" & Me.RacunID & ".pdf"

If Len(Dir(strFile)) > 0 Then
SetAttr strFile, vbNormal
Kill strFile
End If

DoCmd.OpenReport stDocName, acPreview, , "RacunID=" & Me.RacunID
DoCmd.OutputTo acOutputReport, stDocName, acFormatPDF, strFile, False
files(i) = outPath & stDocName & "_racunid=" & Me.RacunID & ".pdf"
DoCmd.Close acReport, stDocName

Also, here is a code from my .dll part: (this part of merging PDF's comes after outputing report to pdf)

Public Sub MergePDFs(ByVal fileNames() As String, ByVal sPath As String)

Dim pdfReaderList As List(Of PdfReader) = New List(Of PdfReader)()
For Each filePath As String In fileNames
Dim pdfReader As PdfReader = New PdfReader(filePath)
pdfReaderList.Add(pdfReader)
Next


Dim document As Document = New Document(PageSize.A4, 0, 0, 0, 0)
Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream(sPath & "Ukupan racun.pdf", FileMode.OpenOrCreate))
document.Open()
For Each reader As PdfReader In pdfReaderList
For i As Integer = 1 To reader.NumberOfPages
Dim page As PdfImportedPage = writer.GetImportedPage(reader, i)
document.Add(iTextSharp.text.Image.GetInstance(page))
Next
Next

document.Close()

End Sub

If anyone has any idea what could cause problem, I appreciate it ...

Thanks in advance!
 

MarioM

New member
Local time
Today, 16:09
Joined
Jul 14, 2020
Messages
6
I got this!!

I forgot to close my files in my .dll

that keep them open ...
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:09
Joined
Feb 28, 2001
Messages
26,999
Glad you caught it yourself. I always push the rule that if you opened it, you should close it. For some things, it is automatic when you leave the subroutine, but for .DLL files, application objects and the files created by either of those things , they can have a life of their own as objects external to Access. Which means the "Exit Sub" automatic cleanup doesn't always work for them.
 

Users who are viewing this thread

Top Bottom