VB.NET e C# compilare Input TextField (Reflection)

Creare una macro - Scrivere uno script - Usare le API
Rispondi
AngelBlueSky
Messaggi: 5
Iscritto il: venerdì 2 maggio 2014, 13:55

VB.NET e C# compilare Input TextField (Reflection)

Messaggio da AngelBlueSky »

Salve ho un programma e gestisco campi di input in questo modo in VB.net

Dim enuTF As Object, aTextField As Object
enuTF = OODoc.TextFields.createEnumeration
Do While enuTF.hasMoreElements
aTextField = enuTF.nextElement
If aTextField.supportsService("com.sun.star.text.TextField.Input") Then

If (aTextField.getPropertyValue("Content") = "aa") Then
aTextField.setPropertyValue("Content", "My new content aa")
aTextField.setPropertyValue("Hint", "My new content aa")
End If

If (aTextField.getPropertyValue("Content") = "bb") Then
aTextField.setPropertyValue("Content", "My new content bb")
aTextField.setPropertyValue("Hint", "My new content bb")
End If


End If
Loop
OODoc.TextFields.refresh()

ora sto riscrivendo lo stesso programma in C# ma non riesco ad implementare la stessa funzione

Riesco ad enumerarli in questo modo utilizzando i Bookmark (ma non penso sia il modo corretto):

object oBookmarks = OODoc.GetType().InvokeMember("getBookmarks", BindingFlags.InvokeMethod, null, OODoc, new object[0]);
object ocount = oBookmarks.GetType().InvokeMember("getCount", BindingFlags.InvokeMethod, null, oBookmarks, new object[0]);
int nCount = (int)ocount;
for (int n = 0; n < nCount; ++n)
{
Object[] XArgs2 = new Object[1];
XArgs2[0] = n;
object oMark = oBookmarks.GetType().InvokeMember("getByIndex", BindingFlags.InvokeMethod, null, oBookmarks, XArgs2);
MessageBox.Show( oMark.GetType().InvokeMember("getName", BindingFlags.InvokeMethod, null, oMark, new object[0]).ToString());
}

Preferisco utilizzare Reflection per poter compilare AnyCpu.

Grazie
OpenOffice 4.0.1 su Windows 7
patel
Volontario attivo
Volontario attivo
Messaggi: 4030
Iscritto il: venerdì 30 aprile 2010, 8:04
Località: Livorno

Re: VB.NET e C# compilare Input TextField (Reflection)

Messaggio da patel »

scusa l'ignoranza, ma cosa c'entra in tutto questo OpenOffice ?
-------------------
Libre Office 7.5.3.2 su Windows 11
allega un file di esempio, guadagnerai tempo tu e lo farai risparmiare a chi ti aiuta
AngelBlueSky
Messaggi: 5
Iscritto il: venerdì 2 maggio 2014, 13:55

Re: VB.NET e C# compilare Input TextField (Reflection)

Messaggio da AngelBlueSky »

Visto che la domanda è inerente alle API di openoffice non vedo perchè questa mia domanda debba essere fuori luogo. Io preferisco programmare OPENOFFICE usando reflection (non sono tenuto a compilare X86 usando CLI) e con VB.net è abbastanza semplice (il createobject di VB.net è "più" flessibile rispetto a C#, inoltre in rete ci sono diversi esempi). Ora vorrei riscrivere alcune funzioni in C# ma non è così immediato.
In sostanza la domanda è semplice, come faccio ad enumerare i campi "com.sun.star.text.TextField.Input" di un documento e modificarne il valore "content" in C# utilizzando Reflection.
Grazie
OpenOffice 4.0.1 su Windows 7
AngelBlueSky
Messaggi: 5
Iscritto il: venerdì 2 maggio 2014, 13:55

Re: VB.NET e C# compilare Input TextField (Reflection)

Messaggio da AngelBlueSky »

Ok I made It.

OOServiceManager = Activator.CreateInstance(Type.GetTypeFromProgID("com.sun.star.ServiceManager"));

Object[] OODesktopArgs = new Object[1];
OODesktopArgs[0] = "com.sun.star.frame.Desktop";
OODesktop = OOServiceManager.GetType().InvokeMember("createinstance", BindingFlags.InvokeMethod, null, OOServiceManager, OODesktopArgs);

String url = @"private:factory/swriter";
Object[] OODocArgs = new Object[4];
OODocArgs[0] = @"file:///" + (Application.StartupPath + @"\aa.dot").Replace(@"\", @"/").Replace(@"\", @"/"); ;
OODocArgs[1] = "_blank";
OODocArgs[2] = 0;
Object loadproperty1 = CreatePropertyValue("Hidden", false); // Executes the OpenOffice without UI
OODocArgs[3] = new Object[] { loadproperty1 };
OODoc = OODesktop.GetType().InvokeMember("loadComponentFromUrl", BindingFlags.InvokeMethod, null, OODesktop, OODocArgs);

Object[] OOFieldArgs = new Object[1];
OOFieldArgs[0] = "com.sun.star.text.TextField.Input";

OOField = OODoc.GetType().InvokeMember("createinstance", BindingFlags.InvokeMethod, null, OODoc, OOFieldArgs);

Object OOTextFields = OODoc.GetType().InvokeMember("getTextFields", BindingFlags.InvokeMethod, null, OODoc, new object[0]);

Object x = OOTextFields.GetType().InvokeMember("createEnumeration", BindingFlags.InvokeMethod, null, OOTextFields, new object[0]);

while ((bool)x.GetType().InvokeMember("hasMoreElements", BindingFlags.InvokeMethod, null, x, null))
{
Object ocuntte = x.GetType().InvokeMember("nextElement", BindingFlags.InvokeMethod, null, x, new Object[] { });
ocuntte.GetType().InvokeMember("getPropertyValue", BindingFlags.InvokeMethod, null, ocuntte, new Object[] { "Content" });
MessageBox.Show(ocuntte.GetType().InvokeMember("getPropertyValue", BindingFlags.InvokeMethod, null, ocuntte, new Object[] { "Content" }).ToString());
}

anche usando i bookmark è possibile sostituire il testo nei Input Text

object oBookmarks = OODoc.GetType().InvokeMember("getBookmarks", BindingFlags.InvokeMethod, null, OODoc, new object[0]);
object ff = oBookmarks.GetType().InvokeMember("getCount", BindingFlags.InvokeMethod, null, oBookmarks, new object[0]);
int nCount = (int)ff;
for (int n = 0; n < nCount; ++n)
{
Object[] XArgs2 = new Object[1];
XArgs2[0] = n;
object oMark = oBookmarks.GetType().InvokeMember("getByIndex", BindingFlags.InvokeMethod, null, oBookmarks, XArgs2);

try
{
Object[] XArgs22 = new Object[1];
XArgs22[0] = "bb";
object oMark2 = oBookmarks.GetType().InvokeMember("getByName", BindingFlags.InvokeMethod, null, oBookmarks, XArgs22);

object oMark23 = oMark2.GetType().InvokeMember("getAnchor", BindingFlags.InvokeMethod, null, oMark2, new object[0]);

Object[] XArgs223 = new Object[1];
XArgs223[0] = "sadfgasdgas";
oMark23.GetType().InvokeMember("setString", BindingFlags.InvokeMethod, null, oMark23, XArgs223);
}
catch
{
;
}
}
OpenOffice 4.0.1 su Windows 7
patel
Volontario attivo
Volontario attivo
Messaggi: 4030
Iscritto il: venerdì 30 aprile 2010, 8:04
Località: Livorno

Re: VB.NET e C# compilare Input TextField (Reflection)

Messaggio da patel »

Non ho detto che la domanda era fuori luogo, ho chiesto una spiegazione premettendo la mia ignoranza, ma tu non ti sei espresso in modo semplice, quindi non ho capito niente.
-------------------
Libre Office 7.5.3.2 su Windows 11
allega un file di esempio, guadagnerai tempo tu e lo farai risparmiare a chi ti aiuta
AngelBlueSky
Messaggi: 5
Iscritto il: venerdì 2 maggio 2014, 13:55

Re: VB.NET e C# compilare Input TextField (Reflection)

Messaggio da AngelBlueSky »

Ok chiedo scusa, pensavo che visto che si tratta della sezione "uno API" si potesse un pò comprendere la mia domanda.

Cmq openoffice, libreoffice, word ecc ecc si possono programmare (automatizzare), includendo le loro interop (non lo faccio per office perchè mi vincola ad una determinata versione di office e quindi uso reflection). Per openoffice è possibile includere come reference le varie dll (CLI) cli_basetypes.dll, cli_oootypes.dll, cli_ure.dll ecc ecc (5 in tutto) e poi programmare nel seguente modo molto più semplice anche perchè ben documentato (metto solo alcune righe di esempio):
XComponentContext oStrap = uno.util.Bootstrap.bootstrap();
XMultiServiceFactory oServMan = oStrap.getServiceManager() as XMultiServiceFactory;
XComponentLoader oDesk = (XComponentLoader)oServMan.createInstance("com.sun.star.frame.Desktop");

però si è vincolati alla compilazione "x86" (che può andare benissimo) però mi trovo a volte ad integrarmi con progetti compilati in "AnyCpu" e quindi devo utilizzare Reflection e il Late Binding di tali oggetti.
Per Openoffice è relativamente semplice farlo in VB.NET perchè ci sono in giro molti tutorial mentre utilizzando C# le cose si complicano perchè c'è molto poco in rete.
Cmq ho risolto e ora riesco ad aprire documenti anche word con campi modulo e compilarli sempre tramite Openoffice via Reflection:
questo è il codice d'esempio:

OOServiceManager = Activator.CreateInstance(Type.GetTypeFromProgID("com.sun.star.ServiceManager"));
Object[] OODesktopArgs = new Object[1];
OODesktopArgs[0] = "com.sun.star.frame.Desktop";
OODesktop = OOServiceManager.GetType().InvokeMember("createinstance", BindingFlags.InvokeMethod, null, OOServiceManager, OODesktopArgs);

String url = @"private:factory/swriter";
Object[] OODocArgs = new Object[4];
OODocArgs[0] = @"file:///" + (Application.StartupPath + @"\aa.dot").Replace(@"\", @"/").Replace(@"\", @"/"); ;
OODocArgs[1] = "_blank";
Object loadproperty1 = CreatePropertyValue("Hidden", false);
OODocArgs[3] = new Object[] { loadproperty1 };
OODoc = OODesktop.GetType().InvokeMember("loadComponentFromUrl", BindingFlags.InvokeMethod, null, OODesktop, OODocArgs);

Object[] OOFieldArgs = new Object[1];
OOFieldArgs[0] = "com.sun.star.text.TextField.Input";

OOField = OODoc.GetType().InvokeMember("createinstance", BindingFlags.InvokeMethod, null, OODoc, OOFieldArgs);

Object OOTextFields = OODoc.GetType().InvokeMember("getTextFields", BindingFlags.InvokeMethod, null, OODoc, new object[0]);

Object x = OOTextFields.GetType().InvokeMember("createEnumeration", BindingFlags.InvokeMethod, null, OOTextFields, new object[0]);

while ((bool)x.GetType().InvokeMember("hasMoreElements", BindingFlags.InvokeMethod, null, x, null))
{
Object ocuntte = x.GetType().InvokeMember("nextElement", BindingFlags.InvokeMethod, null, x, new Object[] { });

if(ocuntte.GetType().InvokeMember("getPropertyValue", BindingFlags.InvokeMethod, null, ocuntte, new Object[] { "Hint" }).ToString()=="aaseg")
{

Object[] arg8 = new Object[2];
arg8[0] = "Content";
arg8[1] = "xxxxxxxxxxxxx";
ocuntte.GetType().InvokeMember("setPropertyValue", BindingFlags.InvokeMethod, null, ocuntte, arg8);

}
if (ocuntte.GetType().InvokeMember("getPropertyValue", BindingFlags.InvokeMethod, null, ocuntte, new Object[] { "Hint" }).ToString() == "bbseg")
{
Object[] arg8 = new Object[2];
arg8[0] = "Content";
arg8[1] = "yyyyyyyyyyy";
ocuntte.GetType().InvokeMember("setPropertyValue", BindingFlags.InvokeMethod, null, ocuntte, arg8);
}
}

OOTextFields.GetType().InvokeMember("refresh", BindingFlags.InvokeMethod, null, OOTextFields, new object[0]);
OpenOffice 4.0.1 su Windows 7
Rispondi