Crystal reports export


I'm trying to export a PDF of my report to a destination on my local server, the PDF is consulted by many people in the organization. I kind of want the export process to be done automatically through code at a set time and date. The problem is sometimes I get the error "The process cannot access the file ... because it is being used by another process." This is because someone currently has the file open. Is there a way to force to overwrite the file regardless if it's open?

The code I'm currently using is
  Dim CrExportOptions As ExportOptions
        Dim CrDiskFileDestinationOptions As New DiskFileDestinationOptions()
        Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()

        CrDiskFileDestinationOptions.DiskFileName = "S:\Projects.pdf"
        CrExportOptions = cryRpt.ExportOptions
        With CrExportOptions
            .ExportDestinationType = ExportDestinationType.DiskFile
            .ExportFormatType = ExportFormatType.PortableDocFormat
            .DestinationOptions = CrDiskFileDestinationOptions
            .FormatOptions = CrFormatTypeOptions
        End With

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lcohanDatabase AnalystCommented:
I don't think that is possible and there are multiple discussions/suggestions here: about similar thing.

This is what I would do to avoid locking:
1. generate the PDF file in a "staging" folder by using your code then copy it into the "readers" folder but with a timestamp attached to its name. This way I believe users will "open" - read the file but also see new one(s) created so they'll close it(them) as new ones get created so you could run a "cleanup" on older files than... By this you could also have a history in that "readers" folder for whetever number of days you decide.

2. you could also "email" the PDF instead to a "subscribers" list so each will get its own copy once it gets generated like here:
Ido MilletProfessor of MIS at Penn State Erie and Owner, Millet SoftwareCommented:
Try to export the pdf to a private folder and then initiate a file copy operation from the private folder to the public one.
Brute force attempt:

Wrap the cryRpt.Export() into a try/catch clause. When the exception (narrow it down to that speciall access denied issue) is thrown, just wait for a little time (some seconds, a minute, sth. else) and try again.

If your program needs to go forward, just start a separate thread for the export.

For a somewhat more elegant attempt, just try to delete the target file prior to writing to it. Wrap a try/catch around the delete. After deleting it, nobody can open it again before you write. If the delete attempt throws the exception access denied, just wait. If it fails with "file not found": No problem at all.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Crystal Reports

From novice to tech pro — start learning today.