Работа через ts_idispatch
Пример:
Подключиться к открытому документу Word и написать слово, изменить шрифт и написать фразу.
//-----------------------------------------------
// LABPP Word пишем текст в Word и меняем шрифт
// LABPP 2023
//-----------------------------------------------
int main()
{
int iIDispatch_Word;
object("create", "ts_idispatch", iIDispatch_Word);
int ires = ts_idispatch(iIDispatch_Word, "attach", "Word.Application"); // create dispatcher object
if (ires != 0) {
cout << "\nError whan attach to word";
return -1;
}
int iVariant_Selection;
object("create", "ts_variant", iVariant_Selection);
ires = ts_idispatch(iIDispatch_Word, "AutoWrap", iVariant_Selection, "DISPATCH_PROPERTYGET", "Selection", 0);
int iIDispatch_Selection;
object("create", "ts_idispatch", iIDispatch_Selection);
ts_variant(iVariant_Selection, "get_pdispVal", iIDispatch_Selection);
// Для информации об объекте-диспетчере, какие у него есть переменные и функции (не обязательно) ---->
// получить список методов через перенос строки (\n)
string smethodslist;
ires = ts_idispatch(iIDispatch_Selection, "GetIDispatchMethods", smethodslist);
coutvar << smethodslist;
//<----
int iVariant_Null;
object("create", "ts_variant", iVariant_Null);
int iVariant_Text;
object("create", "ts_variant", iVariant_Text);
ts_variant(iVariant_Text, "set_value_simple", "Привет");
ires = ts_idispatch(iIDispatch_Selection, "AutoWrap", iVariant_Null, "DISPATCH_METHOD", "TypeText", 1, iVariant_Text);
int iVariant_Font;
object("create", "ts_variant", iVariant_Font);
ires = ts_idispatch(iIDispatch_Selection, "AutoWrap", iVariant_Font, "DISPATCH_PROPERTYGET", "Font", 0);
int iIDispatch_Font;
object("create", "ts_idispatch", iIDispatch_Font);
ts_variant(iVariant_Font, "get_pdispVal", iIDispatch_Font);
ts_variant(iVariant_Text, "set_value_simple", "GOST 2.304 type A");
ires = ts_idispatch(iIDispatch_Font, "AutoWrap", iVariant_Null, "DISPATCH_PROPERTYPUT", "Name", 1, iVariant_Text);
ts_variant(iVariant_Text, "set_value_simple", "Привет другим шрифтом");
ires = ts_idispatch(iIDispatch_Selection, "AutoWrap", iVariant_Null, "DISPATCH_METHOD", "TypeText", 1, iVariant_Text);
ts_idispatch(iIDispatch_Word, "detach");
object("delete", iIDispatch_Word);
object("delete", iIDispatch_Selection);
object("delete", iVariant_Selection);
object("delete", iVariant_Text);
object("delete", iVariant_Null);
object("delete", iVariant_Font);
object("delete", iIDispatch_Font);
}