Perform click of Print Preview button with code


(Fred Meissner) #1

I am trying to auto print preview once the MtlTags report form loads. I found this thread (Perform Click of Clear Tool with code) that is similar, but I’m not sure how to call out the method.

MethodInfo mi = typeof(Erp.UI.Rpt.MtlTags.MtlTagsForm).GetMethod("OnClickPrintPreview",BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(MtlTagsForm, new object[] {});

(Nick Vaughan) #2

This is working code I have to print preview a BAQ report as PDF from a button:

private void preview_Click(object sender, System.EventArgs args)
{
	string format2 = "PDF";
	printpreview("p-notes2","P-NOTES2","Production Notes",format2);
}


private void printpreview(string BAQ, string report, string reportname,string format)
{
	string agentID = "";

	try
	{
		Session otSession = (Session)oTrans.Session;		

		//Set the workstationID
		string workStation = Ice.Lib.Report.EpiReportFunctions.GetWorkStationID(otSession);
		string userID = otSession.UserID;
		
		//Get or Set the AgentID
		using (var aSA = new SysAgentAdapter(oTrans))
		{
			oTrans.PushStatusText("Get System Agent Info", true);

			aSA.BOConnect();
			aSA.GetDefaultTaskAgentID(out agentID);
			if (!string.IsNullOrEmpty(agentID)) { agentID = "SystemTaskAgent"; }
		}

		{
	   	 var baqR = WCFServiceSupport.CreateImpl<Ice.Proxy.Rpt.BAQReportImpl>((Session)oTrans.Session, Epicor.ServiceModel.Channels.ImplBase<Ice.Contracts.BAQReportSvcContract>.UriPath);
			var dynamicReport = WCFServiceSupport.CreateImpl<Ice.Proxy.BO.DynamicReportImpl>((Session)oTrans.Session, Epicor.ServiceModel.Channels.ImplBase<Ice.Contracts.DynamicReportSvcContract>.UriPath);
			var rptMonitor = WCFServiceSupport.CreateImpl<Ice.Proxy.BO.ReportMonitorImpl>((Session)oTrans.Session, Epicor.ServiceModel.Channels.ImplBase<Ice.Contracts.ReportMonitorSvcContract>.UriPath);
	
			// GET DEFAULT REPORT PARAMETERS
			var rptDs = dynamicReport.GetByID(report);
			var baqRptDS = baqR.GetNewBAQReportParam(report);
			baqRptDS.BAQReportParam[0].AutoAction="SSRSPREVIEW";
			baqRptDS.BAQReportParam[0].WorkstationID=workStation;
			baqRptDS.BAQReportParam[0].UserID=userID;
			baqRptDS.BAQReportParam[0].SSRSRenderFormat = format;
			baqRptDS.BAQReportParam[0].AttachmentType = format;
			baqRptDS.BAQReportParam[0].BAQRptID=(report);
			baqRptDS.BAQReportParam[0].ReportID=(report);
			baqRptDS.BAQReportParam[0].Summary = false;
			baqRptDS.BAQReportParam[0].ReportStyleNum = 1;
			baqRptDS.BAQReportParam[0].BAQID=(BAQ);
			baqRptDS.BAQReportParam[0].ReportTitle = reportname;

			rptDs.AcceptChanges();
			StringWriter writer = new StringWriter();
			rptDs.WriteXml(writer);
			baqRptDS.BAQReportParam[0].Filter1 = writer.ToString();

			baqR.SubmitToAgent(baqRptDS, agentID, 0, 0,"Epicor.Mfg.UIRpt.BAQReport");
		}

	}
	catch (Exception ex)
	{
		MessageBox.Show("An error occured trying to print report." + ex.Message);
	
	}
}

This prints it in PDF format. Honestly, I do not know if this will translate to a normal report, but maybe you can tweak it for your situation???