bool __fastcall TfrmMain::InsertText(Variant *App, Variant *Doc, AnsiString Bkmk, AnsiString Text){ WideString text(Text); if(Bkmk.Length()) GotoBookmark(Doc, Bkmk); App->OlePropertyGet("Selection").OleProcedure("TypeText", text); return true; }//---------------------------------------------------------------------------void __fastcall TfrmMain::tbWordReportClick(TObject *Sender){ const AnsiString asFileName(ExtractFilePath(Application->ExeName) + "files\\act.dot"); AnsiString asDocName(""); int i = 0, j = 0; TDateTime dtReport(Date()); Variant app = Unassigned, ws = Unassigned, range = Unassigned, table = Unassigned, cell = Unassigned; app = CreateOleObject("Word.Application"); if(!app.IsEmpty()) { try{ app.OlePropertySet("Visible", false); ws = app.OlePropertyGet("Documents").OleFunction("Open", asFileName); InsertText(&app, &ws, "DayNow", dtReport.FormatString("dd")); InsertText(&app, &ws, "MonthNow", AnsiLowerCase(dtReport.FormatString("mmmmm"))); dtReport = IncMonth(dtReport, -1); asDocName = ExtractFilePath(Application->ExeName) + "отчет " + AnsiLowerCase(dtReport.FormatString("mmmmm")) + ".doc"; ToStartDay(dtReport); InsertText(&app, &ws, "DateFrom", dtReport.FormatString("dd.mm.yy")); ToLastDay(dtReport); InsertText(&app, &ws, "DateTo", dtReport.FormatString("dd.mm.yy")); table = ws.OlePropertyGet("Tables").OleFunction("Item", 2); for(i = 1; i != sgResults->RowCount; i++) { for( j = 2; j 7; j++) { cell = table.OleFunction("Cell", i+2, j+1); range = cell.OlePropertyGet("Range"); range.OlePropertySet("Text", WideString(sgResults->Cells[j][i])); } } ws.OleProcedure("SaveAs", asDocName); } __finally{ app.OleProcedure("Quit"); app = ws = range = table = cell = Unassigned; ShowMessage("Выполнено!"); } }}//---------------------------------------------------------------------------
bool __fastcall TfrmMain::GotoBookmark(Variant *Word, AnsiString Bkmk){ TVariant What(0xFFFFFFFF), BkmkName(Bkmk); if (Word->OlePropertyGet("Bookmarks").OleFunction("Exists", WideString(Bkmk))) Word->OleFunction("GoTo", &What, EmptyParam, EmptyParam, &BkmkName).OleFunction("Select"); else throw Exception("Закладка '" + Bkmk + "' не найдена!"); return true;}