Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export Access report as PDF

I'm doing an ASP.NET application in C# that connects to an Access database and now I want that it exports a report as PDF. Here is my code that only connects to Access, I was thinking of running an access macro or running a command with Microsoft.Office.Interop.Access.Application DoCmd.OutputTo but I do not know how to use it exactly. Any help?

string user = "myUser";
string pass = "myPass";
string host = "myHost";
OleDbConnection connection = new OleDbConnection(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = myPath");
string _command = ...; //I don't know what put here

try
{
    OleDbCommand command = new OleDbCommand(_command, connection);
    command.CommandText = ...; //I don't know what put here
    int temp = command.ExecuteNonQuery();
    Console.WriteLine("PDF created!");
 }
like image 675
Martina Nonloso Avatar asked Mar 26 '26 07:03

Martina Nonloso


1 Answers

I resolved my problem with the automation, I followed this https://support.microsoft.com/en-us/help/317114/how-to-automate-microsoft-access-by-using-visual-c (suggested me by Erik von Asmuth) and now I can export my report in PDF. My code now:

using Microsoft.Office.Interop.Access;

Application access = new Microsoft.Office.Interop.Access.Application();
access.OpenCurrentDatabase(@"C:\Desktop\MyDB.accdb", false);

access.DoCmd.OutputTo(AcOutputObjectType.acOutputReport, 
    "Dati", //ReportName
    ".pdf", //OutputType
    @"C:\Desktop\Test.pdf",  //outupuFile
    System.Reflection.Missing.Value,
    System.Reflection.Missing.Value,
    System.Reflection.Missing.Value,
    AcExportQuality.acExportQualityPrint
);
like image 200
Martina Nonloso Avatar answered Mar 28 '26 21:03

Martina Nonloso



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!