//---------------------------------------------------------------------------
void OfficeSaveAsDoc(AnsiString Office, Variant Document, AnsiString FileName)
{
 //сохранить документ как
  try
    {
     if (Office=="OO")
      {
        Variant SaveParam;
        int Bounds[2] = {0,0};
        Variant ServiceManager = Variant::CreateObject("com.sun.star.ServiceManager");
        Variant MyStruct = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
        MyStruct.OlePropertySet("name","FilterName");
        MyStruct.OlePropertySet("value","MS Word 97");
        SaveParam = VarArrayCreate(Bounds, 1, varVariant);
        SaveParam.PutElement(MyStruct, 0);
        Document.OleFunction ("storeAsURL", convertToURL(FileName), SaveParam);
        ServiceManager = Unassigned;
      }
     if (Office=="MSO")
      {
        Document.OleProcedure("SaveAs", FileName);
      }
    }
  catch (Exception &E)
    {
     ShowMessage("Ошибка при сохранении документа как " + E.Message);
    }
}
//---------------------------------------------------------------------------
Variant OfficeOpenDoc(AnsiString Office, Variant *app, AnsiString FileName)
{
// открыть документ
  try
    {
     Variant vApp, Document;
     if (Office=="OO")
      {
       Variant Desktop;
       Variant OpenParams;
       int Bounds[2] = {0,0};

       vApp = Variant::CreateObject("com.sun.star.ServiceManager");
       Desktop = vApp.OleFunction("createInstance", "com.sun.star.frame.Desktop");
       OpenParams = VarArrayCreate(Bounds, 1, varVariant);
       Variant MyStruct = vApp.OleFunction("Bridge_GetStruct","com.sun.star.beans.PropertyValue");
       MyStruct.OlePropertySet("name","Hidden");
       MyStruct.OlePropertySet("value",true);
       OpenParams.PutElement(MyStruct, 0);
       Document = Desktop.OleFunction("LoadComponentFromURL", convertToURL(FileName), "_blank", 0, OpenParams );
      }
     if (Office=="MSO")
      {
       Variant word_docs;
       vApp = Variant::CreateObject("word.application");
       word_docs = vApp.OlePropertyGet("Documents");
       Document = word_docs.OleFunction("Open" ,FileName.c_str());
      }
	 *app = vApp;
	 return Document;
    }
  catch (Exception &E)
    {
     ShowMessage("Ошибка при открытии документа " + E.Message);
     return 0;
    }
}
//---------------------------------------------------------------------------
void OfficeFindReplace(AnsiString Office, Variant app, Variant Document, AnsiString sFind, AnsiString sReplace)
{
//найти и заменить с параметрами
  try
    {
     if (Office=="OO")
      {
      /*
       //найти и заменить без параметров
       Variant FindParam = Document.OleFunction("createSearchDescriptor");
       FindParam.OlePropertySet("SearchString", sFind);
       FindParam.OlePropertySet("ReplaceString", sReplace);
       Document.OleFunction("ReplaceAll", FindParam);
       */
       Variant ServiceManager;
       ServiceManager = Variant::CreateObject("com.sun.star.ServiceManager");

       Variant MyStruct3 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct3.OlePropertySet("name",        "SearchItem.StyleFamily");
       MyStruct3.OlePropertySet("value",        2);
       Variant MyStruct4 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct4.OlePropertySet("name",        "SearchItem.CellType");
       MyStruct4.OlePropertySet("value",        0);
       Variant MyStruct5 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct5.OlePropertySet("name",        "SearchItem.RowDirection");
       MyStruct5.OlePropertySet("value",        true);
       Variant MyStruct6 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct6.OlePropertySet("name",        "SearchItem.AllTables");
       MyStruct6.OlePropertySet("value",        false);
       Variant MyStruct7 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct7.OlePropertySet("name",        "SearchItem.Backward");
       MyStruct7.OlePropertySet("value",        false);
       Variant MyStruct8 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct8.OlePropertySet("name",        "SearchItem.Pattern");
       MyStruct8.OlePropertySet("value",        false);
       Variant MyStruct9 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct9.OlePropertySet("name",        "SearchItem.Content");
       MyStruct9.OlePropertySet("value",        false);
       Variant MyStruct10 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct10.OlePropertySet("name",        "SearchItem.AsianOptions");
       MyStruct10.OlePropertySet("value",        false);
       Variant MyStruct11 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct11.OlePropertySet("name",        "SearchItem.AlgorithmType");
       MyStruct11.OlePropertySet("value",        0);

       Variant MyStruct2 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct2.OlePropertySet("name",       "SearchItem.SearchFlags");
       MyStruct2.OlePropertySet("value",      65552); // флаг 65536 - часть или все слово
       Variant MyStruct = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct.OlePropertySet("name",        "SearchItem.SearchString");
       MyStruct.OlePropertySet("value",       sFind);
       Variant MyStruct1 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct1.OlePropertySet("name",       "SearchItem.ReplaceString");
       MyStruct1.OlePropertySet("value",      sReplace);

       Variant MyStruct12 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct12.OlePropertySet("name",        "SearchItem.Locale");
       MyStruct12.OlePropertySet("value",        255);
       Variant MyStruct13 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct13.OlePropertySet("name",        "SearchItem.ChangedChars");
       MyStruct13.OlePropertySet("value",        2);
       Variant MyStruct14 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct14.OlePropertySet("name",        "SearchItem.DeletedChars");
       MyStruct14.OlePropertySet("value",        2);
       Variant MyStruct15 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct15.OlePropertySet("name",        "SearchItem.InsertedChars");
       MyStruct15.OlePropertySet("value",        2);
       Variant MyStruct16 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct16.OlePropertySet("name",        "SearchItem.TransliterateFlags");
       MyStruct16.OlePropertySet("value",        1024);
       Variant MyStruct17 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct17.OlePropertySet("name",        "SearchItem.Command");
       MyStruct17.OlePropertySet("value",        3);
       Variant MyStruct18 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct18.OlePropertySet("name",        "Quiet");
       MyStruct18.OlePropertySet("value",        true);

       int Bounds[2] = {0,18}; // на 18 шт
       Variant SearchParam = VarArrayCreate(Bounds, 1, varVariant);

       SearchParam.PutElement(MyStruct3, 3);
       SearchParam.PutElement(MyStruct4, 4);
       SearchParam.PutElement(MyStruct5, 5);
       SearchParam.PutElement(MyStruct6, 6);
       SearchParam.PutElement(MyStruct7, 7);
       SearchParam.PutElement(MyStruct8, 8);
       SearchParam.PutElement(MyStruct9, 9);
       SearchParam.PutElement(MyStruct10, 10);
       SearchParam.PutElement(MyStruct11, 11);

       SearchParam.PutElement(MyStruct, 0);
       SearchParam.PutElement(MyStruct1, 1);
       SearchParam.PutElement(MyStruct2, 2);

       SearchParam.PutElement(MyStruct12, 12);
       SearchParam.PutElement(MyStruct13, 13);
       SearchParam.PutElement(MyStruct14, 14);
       SearchParam.PutElement(MyStruct15, 15);
       SearchParam.PutElement(MyStruct16, 16);
       SearchParam.PutElement(MyStruct17, 17);
       SearchParam.PutElement(MyStruct18, 18);


       Variant Dispatcher, Frame, CurrentController;
	   CurrentController = Document.OleFunction("getCurrentController");
       Frame = CurrentController.OleFunction("getFrame");
	   Dispatcher= ServiceManager.OleFunction("createInstance", "com.sun.star.frame.DispatchHelper");
       Dispatcher.OleFunction("executeDispatch", Frame, ".uno:ExecuteSearch", "", 0, SearchParam);
      }
	 if (Office=="MSO")
      {
		Variant this_selection, this_find;
		this_selection = app.OlePropertyGet("Selection") ;
	   //	this_selection.OleFunction("GoTo", 4294967295, 0, 0, Document);
		this_find = this_selection.OlePropertyGet("Find") ;
        //---Запуск обработки
		this_find.OleProcedure ("Execute",
		/* FindText          */      sFind.c_str(),
		/* MatchCase         */      false,
		/* MatchWholeWord    */      false, //  false - часть или все слово
        /* MatchWildcards    */      false,
        /* MatchSoundsLike   */      false,
        /* MatchAllWordForms */      false,
        /* Forward           */      true,
        /* Wrap              */      1,
        /* Format            */      false,
		/* ReplaceWith       */      sReplace.c_str(),
        /* Replace           */      2
		);
      }
    }
  catch (Exception &E)
    {
	 ShowMessage("Ошибка при поиске и замене " + E.Message);
    }
}
//---------------------------------------------------------------------------
void OfficePrintParam(AnsiString Office, Variant app, Variant Document, int vCopies, AnsiString vRangeText, AnsiString vPrinter)
{
//Печать страницы vRangeText  в vCopies экземплярах и выбор принтера vPrinter
//для ОО после выбора принтера документ надо сохранять заново
  try
    {
     if (Office=="OO")
      {
       Variant ServiceManager;
       ServiceManager = Variant::CreateObject("com.sun.star.ServiceManager");
       Variant MyStruct = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct.OlePropertySet("name",        "Copies");
       MyStruct.OlePropertySet("value",        vCopies);
       Variant MyStruct_1 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct_1.OlePropertySet("name",        "RangeText");
       MyStruct_1.OlePropertySet("value",        vRangeText); //Тут можно указать диапазон "1,5-7"
       Variant MyStruct_2 = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
       MyStruct_2.OlePropertySet("name",        "Collate");
       MyStruct_2.OlePropertySet("value",        false);

       int Bounds[2] = {0,2};
       Variant PrintParam = VarArrayCreate(Bounds, 1, varVariant);
       PrintParam.PutElement(MyStruct, 0);
       PrintParam.PutElement(MyStruct_1, 1);
       PrintParam.PutElement(MyStruct_2, 2);

       //выбор принтера
        Variant PrintDescStruct = ServiceManager.OleFunction("Bridge_GetStruct", "com.sun.star.beans.PropertyValue");
        PrintDescStruct.OlePropertySet("Name", "Name");
        PrintDescStruct.OlePropertySet("Value", vPrinter);
        int PrintDescBounds[2] = {0,0};
        Variant PrintDesc = VarArrayCreate(PrintDescBounds, 1, varVariant);
        PrintDesc.PutElement(PrintDescStruct, 0);
        Document.OleFunction("setPrinter", PrintDesc);

       Variant Dispatcher, Frame, CurrentController;
       CurrentController = Document.OleFunction("getCurrentController");
       Frame = CurrentController.OleFunction("getFrame");

       Dispatcher= ServiceManager.OleFunction("createInstance", "com.sun.star.frame.DispatchHelper");
       //с пустыми или не верными параметрами PrintParam выскакивает окно печати!
       Dispatcher.OleFunction("executeDispatch", Frame, ".uno:Print", "", 0, PrintParam);

      //нужно это или нет, не знаю
       PrintParam = Unassigned;
       PrintDesc  = Unassigned;
      }
     if (Office=="MSO")
      {
	   app.OlePropertySet("ActivePrinter", vPrinter);
       OleVariant
           tr = true,
           fl=false,
           copies= vCopies,
		   _Range= 4, //wdPrintRangeOfPages
		   _Item=  0, //wdPrintDocumentContent
		   _PageType= 0, //wdPrintAllPages
           _FileName=(AnsiString)"",
           _page=(AnsiString)vRangeText;
	   app.OleFunction("PrintOut", fl, fl, _Range, _FileName, "",
									"", _Item, copies, _page, _PageType, fl,
									tr, "", "");
      }
    }
  catch (Exception &E)
    {
     ShowMessage("Ошибка при печати с параметрами " + E.Message);
    }
}