GetObject

GetObject liefert eine Referenz auf ein Objekt, das in einem UniPlot-Dokument eingebettet ist. Falls die Funktion mit dem Namen einer Applikation aufgerufen wird, liefert die Funktion eine Referenz auf eine Instanz der Applikation.

o = GetObject(hOleObject)
o = GetObject(ssApplicationName)

Returnwert

o ist eine Referenz auf ein COM-Objekt, falls der erste Parameter ein Handle auf ein OLE-Objekt ist (hOleObject). Falls der erste Parameter der Name einer Applikation ist, liefert die Funktion eine Referenz auf eine Instanz der Applikation. Falls die Funktion keine Instanz finden kann, wird die Applikation gestartet.

Parameter

hOleObject

ist die Zugriffsnummer des eingebetteten Objekts.

ssApplicationName

ist der Name der Application, z. B. Word.Application oder Excel.Application.

Kommentar

Falls die Server-Applikation nicht antwortet, wird nach 30 Sekunden ein Dialogfeld angezeigt. Der Defaultwert für time out kann mit OLESetTimeout(nSeconds) gesetzt werden.

Beispiel

Das folgende Beispiel geht davon aus, das Sie Excel 2000 oder Excel 97 auf Ihrem Rechner installiert haben.

def _GetObjectByName(ssName)
{
    hObj = ObjGetFirst()
    if (ObjGetName(hObj) == ssName) {
        return hObj;
    }
    while ((hObj = ObjGetNext()) != 0) {
        if (ObjGetName(hObj) == ssName) {
            return hObj;
        }
    }
    return 0;
}
def test()
{
    hDoc = DocCreate(GetRootDirectory() + "Samples\Ole\Simple.ipw")
//  hDoc = DocCreate("d:\uniplotr\Samples\Ole\Simple.ipw");
    if (hDoc == 0) error("can't open document");
    hOleObject = _GetObjectByName("magic0101");
    if (hOleObject == 0) error("can't find Excel table");
    excel = GetObject(hOleObject);
    if (excel == 0) error("can't execute Excel");
    excel.ActiveSheet.Cells(1,1).Value = "x";
    excel.ActiveSheet.Cells(1,2).Value = "y";
    excel.ActiveSheet.Range("a2:a31").Value = [2:31]';
    excel.ActiveSheet.Range("b2:b31").Value = rand(31, 1);
    DocReplot(hDoc);
}

Das folgende Beispiel zeigt eine Funktion, die alle Seiten eines Dokuments in ein MS-Word-Dokument einfügt.

def SendToMSWord(hDoc)
{
    ShowWaitCursor(TRUE);
    obj = GetObject("Word.Application")
//  obj = CreateObject("Word.Application")
    if (obj == 0) {
        error(sprintf(_s("Cannot start %s"), "Word"));
    }
    obj.Documents.Add();
    ad = obj.ActiveDocument;
    j = 1;
    hPageAct = DocGetActivePage(hDoc);
    nPageSel = DocGetPageNumber(hDoc, hPageAct);
    nPage = DocGetPageCount(hDoc);
    AppProgressCreate(0, nPage, 1, FALSE);
    for (i in nPage:1) {
        hPage = DocGetPageHandle(hDoc, i);
        ssFile = GetTempFileName();
        PageSaveAsMetafile(hPage, ssFile);
        ad.Shapes.AddPicture(ssFile, 0, 1).ConvertToInlineShape();
        DeleteFile(ssFile);
        AppProgressStepIt();
    }
    AppProgressDestroy();
    ShowWaitCursor(FALSE);
    DocSelectPage(hDoc, nPageSel);
    obj.Visible = 1;
    return TRUE;
}

Das folgende Beispiel zeigt ein kleines Interface für Excel. Mit dem Interface können einzelne Werte aus einer Excel-Datei gelesen und geschrieben werden.

def excel_com_open(ssFileName)
{
   obj = [.];
   ox = GetObject("Excel.Application");
   if (ox == 0) {
      MessageBox(sprintf(_s("Cannot start %s"), "MS-Excel"), ["ICONHAND"]);
              ShowWaitCursor(FALSE);
              return FALSE;
       }
   if (strtod(ox.Application.Version()) < 8.0) {
      ox=0;
      MessageBoxError(_s("Only MS-Excel 97, MS-Excel 2000 or newer is supported"));
      ShowWaitCursor(FALSE);
      return FALSE;
   }
   ssXLSTemplateFileName = MakeFullPath(ssFileName);
   ssXLSTemplateFileName = strfindreplace(ssXLSTemplateFileName, "\\\\", "\\");
   if (ox.Workbooks.Open(ssXLSTemplateFileName) == 0) {
      MessageBoxError(_s("Cannot open file:") + " " + ssXLSTemplateFileName);
      ShowWaitCursor(FALSE);
      return FALSE;
   }
   obj.ox = ox;
   return obj;
}

def excel_com_select_worksheet(obj, ssSheetName)
{
   nTable = obj.ox.ActiveWorkbook.Worksheets.count();
   for (i in 1:nTable) {
      ssName = obj.ox.ActiveWorkbook.Worksheets(i).Name;
      if (ssName == ssSheetName) {
         obj.oSheet = obj.ox.ActiveWorkbook.Worksheets(i);
         return TRUE;
      }
   }
   obj.oSheet = 0;
   return FALSE;
}

def excel_com_write(obj, pValue, iRow, iCol)
{
   obj.oSheet.Cells(iRow, iCol).Value = pValue;
}

def excel_com_read(obj, iRow, iCol)
{
   return obj.oSheet.Cells(iRow, iCol).Value;
}

def excel_com_close(obj)
{
   obj.ox.ActiveWorkbook.Save();
   obj.ox.ActiveWorkbook.Close();
   obj.ox = 0;
}

/*
Die Excel-Datei muss eine Tabelle mit den angegebenen Namen
(z.B. Tabelle1) enthalten.
*/
def Test()
{
   obj = excel_com_open("d:/test_excel.xls");
   if (obj == 0) {
      error();
   }
   excel_com_select_worksheet(obj, "Tabelle1"); // Tabelle auswählen
   excel_com_write(obj, "Hallo", 2,3);  // Wert "Hallo" in Zelle C2 schreiben
   excel_com_write(obj, 123.543, 2,4);  // Zahl 123.543 in Zelle D2 schreiben
   ssVal = excel_com_read(obj, 2,3);  // Zelle lesen
   ssVal = excel_com_read(obj, 2,4);
   excel_com_close(obj);  // geänderte Datei speichern und schließen
}

Das folgende Beispiel zeigt ein kleines Interface für Excel. Mit dem Interface können einzelne Werte aus einer Excel-Datei gelesen und geschrieben werden.

def excel_com_open(ssFileName)
{
   obj = [.];
   ox = GetObject("Excel.Application");
   if (ox == 0) {
      MessageBox(sprintf(_s("Cannot start %s"), "MS-Excel"), ["ICONHAND"]);
              ShowWaitCursor(FALSE);
              return FALSE;
       }
   if (strtod(ox.Application.Version()) < 8.0) {
      ox=0;
      MessageBoxError(_s("Only MS-Excel 97, MS-Excel 2000 or newer is supported"));
      ShowWaitCursor(FALSE);
      return FALSE;
   }
   ssXLSTemplateFileName = MakeFullPath(ssFileName);
   ssXLSTemplateFileName = strfindreplace(ssXLSTemplateFileName, "\\\\", "\\");
   if (ox.Workbooks.Open(ssXLSTemplateFileName) == 0) {
      MessageBoxError(_s("Cannot open file:") + " " + ssXLSTemplateFileName);
      ShowWaitCursor(FALSE);
      return FALSE;
   }
   obj.ox = ox;
   return obj;
}

def excel_com_select_worksheet(obj, ssSheetName)
{
   nTable = obj.ox.ActiveWorkbook.Worksheets.count();
   for (i in 1:nTable) {
      ssName = obj.ox.ActiveWorkbook.Worksheets(i).Name;
      if (ssName == ssSheetName) {
         obj.oSheet = obj.ox.ActiveWorkbook.Worksheets(i);
         return TRUE;
      }
   }
   obj.oSheet = 0;
   return FALSE;
}

def excel_com_write(obj, pValue, iRow, iCol)
{
   obj.oSheet.Cells(iRow, iCol).Value = pValue;
}

def excel_com_read(obj, iRow, iCol)
{
   return obj.oSheet.Cells(iRow, iCol).Value;
}

def excel_com_close(obj)
{
   obj.ox.ActiveWorkbook.Save();
   obj.ox.ActiveWorkbook.Close();
   obj.ox = 0;
}

/*
Die Excel-Datei muss eine Tabelle mit den angegebenen Namen
(z.B. Tabelle1) enthalten.
*/
def Test()
{
   obj = excel_com_open("d:/test_excel.xls");
   if (obj == 0) {
      error();
   }
   excel_com_select_worksheet(obj, "Tabelle1"); // Tabelle auswählen
   excel_com_write(obj, "Hallo", 2,3);  // Wert "Hallo" in Zelle C2 schreiben
   excel_com_write(obj, 123.543, 2,4);  // Zahl 123.543 in Zelle D2 schreiben
   ssVal = excel_com_read(obj, 2,3);  // Zelle lesen
   ssVal = excel_com_read(obj, 2,4);
   excel_com_close(obj);  // geänderte Datei speichern und schließen
}

id-69498