以下のコードは、ボタンイベントでビジネスオブジェクト6.5のインスタンスを開き、レポートを更新してから、レポート内のデータをcsvファイルにダンプし、ビジネスオブジェクトインスタンスを終了するwinformsアプリケーションからのものです。
初めてコードを実行すると完全に機能しますが、もう一度実行すると、行に例外が発生します
boApp.LoginAs(userName, Password, false, null);
スローされる例外は「無効なオブジェクト」です。
これは、boAppが再初期化されていないという事実と、問題となっているのは静的クラスに関する知識の欠如によるものだと思います。
呼び出し方法は次のとおりです。
BO_Control.RefreshBusinessObjects(boReportsFolder, boExportsFolder, boReportName, exportFileName, startDate, endDate);
これはBO_Controlクラスです
static class BO_Control
{
static busobj.Application boApp = new busobj.Application();
static busobj.Document testDoc;
public static void RefreshBusinessObjects(string reportFolder, string exportFolder ,string boReportName, string exportFileName, string startDate, string endDate)
{
DateTime BoStart = DateTime.Now;
boApp.LoginAs(userName, Password, false, null);
boApp.Interactive = false;
boApp.Visible = false;
GetData(reportFolder, boReportName, startDate, endDate);
ExportData(exportFolder, exportFileName);
Console.WriteLine("BO_Export took {0} seconds.", DateTime.Now.Subtract(BoStart));
boApp.Quit();
}
static busobj.Document GetData(string reportFolder, string reportName, string startDate, string endDate)
{
Console.WriteLine(reportFolder + reportName);
testDoc = (busobj.Document)boApp.Documents.Open(reportFolder + reportName, true, false, null, null);
//Report Start Date
testDoc.Variables[1].Value = startDate;
//Report End Date
testDoc.Variables[2].Value = endDate;
//Area. Needs to be a semi-colon delimited string
testDoc.Variables[3].Value = "L;B;H;";
testDoc.Refresh();
return testDoc;
}
static void ExportData(string exportFolder, string exportFileName)
{
testDoc.Reports.get_Item(1).ExportAsText(exportFolder + exportFileName);
//2 = DoNotSaveChanges
testDoc.Close(2);
}
}