func writeExample(excel, workbooks *ole.IDispatch, filepath string) { // ref: https://msdn.microsoft.com/zh-tw/library/office/ff198017.aspx // http://stackoverflow.com/questions/12159513/what-is-the-correct-xlfileformat-enumeration-for-excel-97-2003 const xlExcel8 = 56 workbook := oleutil.MustCallMethod(workbooks, "Add", nil).ToIDispatch() defer workbook.Release() worksheet := oleutil.MustGetProperty(workbook, "Worksheets", 1).ToIDispatch() defer worksheet.Release() cell := oleutil.MustGetProperty(worksheet, "Cells", 1, 1).ToIDispatch() oleutil.PutProperty(cell, "Value", 12345) cell.Release() activeWorkBook := oleutil.MustGetProperty(excel, "ActiveWorkBook").ToIDispatch() defer activeWorkBook.Release() os.Remove(filepath) // ref: https://msdn.microsoft.com/zh-tw/library/microsoft.office.tools.excel.workbook.saveas.aspx oleutil.MustCallMethod(activeWorkBook, "SaveAs", filepath, xlExcel8, nil, nil).ToIDispatch() //time.Sleep(2 * time.Second) // let excel could close without asking // oleutil.PutProperty(workbook, "Saved", true) // oleutil.CallMethod(workbook, "Close", false) }
func (mso *MSO) Quit() (err error) { defer Except("Quit", &err, ole.CoUninitialize) if r := recover(); r != nil { //catch panic of which defering Quit. info := fmt.Sprintf("***panic before Quit: %+v", r) fmt.Println(info) err = errors.New(info) } oleutil.MustCallMethod(mso.IdWorkBooks, "Close") oleutil.MustCallMethod(mso.IdExcel, "Quit") mso.IdWorkBooks.Release() mso.IdExcel.Release() mso.IuApp.Release() return }
// This example creates a new spreadsheet, reads and modifies cell values and style. func main() { ole.CoInitialize(0) unknown, errCreate := oleutil.CreateObject("com.sun.star.ServiceManager") checkError(errCreate, "Couldn't create a OLE connection to LibreOffice") ServiceManager, errSM := unknown.QueryInterface(ole.IID_IDispatch) checkError(errSM, "Couldn't start a LibreOffice instance") desktop := oleutil.MustCallMethod(ServiceManager, "createInstance", "com.sun.star.frame.Desktop").ToIDispatch() document := LONewSpreadsheet(desktop) sheet0 := LOGetWorksheet(document, 0) cell1_1 := LOGetCell(sheet0, 1, 1) // cell B2 cell1_2 := LOGetCell(sheet0, 1, 2) // cell B3 cell1_3 := LOGetCell(sheet0, 1, 3) // cell B4 cell1_4 := LOGetCell(sheet0, 1, 4) // cell B5 LOSetCellString(cell1_1, "Hello World") LOSetCellValue(cell1_2, 33.45) LOSetCellFormula(cell1_3, "=B3+5") b4Value := LOGetCellString(cell1_3) LOSetCellString(cell1_4, b4Value) // set background color yellow: oleutil.MustPutProperty(cell1_1, "cellbackcolor", 0xFFFF00) fmt.Printf("Press [ENTER] to exit") fmt.Scanf("%s") ServiceManager.Release() ole.CoUninitialize() }
// LONewSpreadsheet creates a new spreadsheet in a new window and returns a document handle. func LONewSpreadsheet(desktop *ole.IDispatch) (document *ole.IDispatch) { var args = []string{} document = oleutil.MustCallMethod(desktop, "loadComponentFromURL", "private:factory/scalc", // alternative: private:factory/swriter "_blank", 0, args).ToIDispatch() return }
// LOOpenFile opens a file (text, spreadsheet, ...) in a new window and returns a document // handle. Example: /home/testuser/spreadsheet.ods func LOOpenFile(desktop *ole.IDispatch, fullpath string) (document *ole.IDispatch) { var args = []string{} document = oleutil.MustCallMethod(desktop, "loadComponentFromURL", "file://"+fullpath, "_blank", 0, args).ToIDispatch() return }
func main() { ole.CoInitialize(0) unknown, _ := oleutil.CreateObject("Outlook.Application") outlook, _ := unknown.QueryInterface(ole.IID_IDispatch) ns := oleutil.MustCallMethod(outlook, "GetNamespace", "MAPI").ToIDispatch() folder := oleutil.MustCallMethod(ns, "GetDefaultFolder", 10).ToIDispatch() contacts := oleutil.MustCallMethod(folder, "Items").ToIDispatch() count := oleutil.MustGetProperty(contacts, "Count").Value().(int32) for i := 1; i <= int(count); i++ { item, err := oleutil.GetProperty(contacts, "Item", i) if err == nil && item.VT == ole.VT_DISPATCH { if value, err := oleutil.GetProperty(item.ToIDispatch(), "FullName"); err == nil { fmt.Println(value.Value()) } } } oleutil.MustCallMethod(outlook, "Quit") }
func main() { ole.CoInitialize(0) unknown, _ := oleutil.CreateObject("InternetExplorer.Application") ie, _ := unknown.QueryInterface(ole.IID_IDispatch) oleutil.CallMethod(ie, "Navigate", "http://www.google.com") oleutil.PutProperty(ie, "Visible", true) for { if oleutil.MustGetProperty(ie, "Busy").Val == 0 { break } } time.Sleep(1e9) document := oleutil.MustGetProperty(ie, "document").ToIDispatch() window := oleutil.MustGetProperty(document, "parentWindow").ToIDispatch() // set 'golang' to text box. oleutil.MustCallMethod(window, "eval", "document.getElementsByName('q')[0].value = 'golang'") // click btnG. btnG := oleutil.MustCallMethod(window, "eval", "document.getElementsByName('btnG')[0]").ToIDispatch() oleutil.MustCallMethod(btnG, "click") }
func main() { ole.CoInitialize(0) unknown, _ := oleutil.CreateObject("Agent.Control.1") agent, _ := unknown.QueryInterface(ole.IID_IDispatch) oleutil.PutProperty(agent, "Connected", true) characters := oleutil.MustGetProperty(agent, "Characters").ToIDispatch() oleutil.CallMethod(characters, "Load", "Merlin", "c:\\windows\\msagent\\chars\\Merlin.acs") character := oleutil.MustCallMethod(characters, "Character", "Merlin").ToIDispatch() oleutil.CallMethod(character, "Show") oleutil.CallMethod(character, "Speak", "こんにちわ世界") time.Sleep(4000000000) }
func main() { ole.CoInitialize(0) unknown, _ := oleutil.CreateObject("Microsoft.XMLHTTP") xmlhttp, _ := unknown.QueryInterface(ole.IID_IDispatch) _, err := oleutil.CallMethod(xmlhttp, "open", "GET", "http://rss.slashdot.org/Slashdot/slashdot", false) if err != nil { panic(err.Error()) } _, err = oleutil.CallMethod(xmlhttp, "send", nil) if err != nil { panic(err.Error()) } state := -1 for state != 4 { state = int(oleutil.MustGetProperty(xmlhttp, "readyState").Val) time.Sleep(10000000) } responseXml := oleutil.MustGetProperty(xmlhttp, "responseXml").ToIDispatch() items := oleutil.MustCallMethod(responseXml, "selectNodes", "/rss/channel/item").ToIDispatch() length := int(oleutil.MustGetProperty(items, "length").Val) for n := 0; n < length; n++ { item := oleutil.MustGetProperty(items, "item", n).ToIDispatch() title := oleutil.MustCallMethod(item, "selectSingleNode", "title").ToIDispatch() fmt.Println(oleutil.MustGetProperty(title, "text").ToString()) link := oleutil.MustCallMethod(item, "selectSingleNode", "link").ToIDispatch() fmt.Println(" " + oleutil.MustGetProperty(link, "text").ToString()) title.Release() link.Release() item.Release() } items.Release() xmlhttp.Release() }
func main() { ole.CoInitialize(0) unknown, err := oleutil.CreateObject("WMPlayer.OCX") if err != nil { log.Fatal(err) } wmp := unknown.MustQueryInterface(ole.IID_IDispatch) collection := oleutil.MustGetProperty(wmp, "MediaCollection").ToIDispatch() list := oleutil.MustCallMethod(collection, "getAll").ToIDispatch() count := int(oleutil.MustGetProperty(list, "count").Val) for i := 0; i < count; i++ { item := oleutil.MustGetProperty(list, "item", i).ToIDispatch() name := oleutil.MustGetProperty(item, "name").ToString() sourceURL := oleutil.MustGetProperty(item, "sourceURL").ToString() fmt.Println(name, sourceURL) } }
func main() { ole.CoInitialize(0) unknown, _ := oleutil.CreateObject("Excel.Application") excel, _ := unknown.QueryInterface(ole.IID_IDispatch) oleutil.PutProperty(excel, "Visible", true) workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch() workbook := oleutil.MustCallMethod(workbooks, "Add", nil).ToIDispatch() worksheet := oleutil.MustGetProperty(workbook, "Worksheets", 1).ToIDispatch() cell := oleutil.MustGetProperty(worksheet, "Cells", 1, 1).ToIDispatch() oleutil.PutProperty(cell, "Value", 12345) time.Sleep(2000000000) oleutil.PutProperty(workbook, "Saved", true) oleutil.CallMethod(workbook, "Close", false) oleutil.CallMethod(excel, "Quit") excel.Release() ole.CoUninitialize() }
// LOGetCellRangeByName returns a named range (e.g. "A1:B4") func LOGetCellRangeByName(worksheet *ole.IDispatch, rangeName string) (cells *ole.IDispatch) { return oleutil.MustCallMethod(worksheet, "getCellRangeByName", rangeName).ToIDispatch() }
// LOGetCell returns an handle to a cell within a worksheet // LibreOffice Basic: GetCell = oSheet.getCellByPosition (nColumn , nRow) func LOGetCell(worksheet *ole.IDispatch, nColumn int, nRow int) (cell *ole.IDispatch) { return oleutil.MustCallMethod(worksheet, "getCellByPosition", nColumn, nRow).ToIDispatch() }
// LOGetWorksheet returns a worksheet (index starts at 0) func LOGetWorksheet(document *ole.IDispatch, index int) (worksheet *ole.IDispatch) { sheets := oleutil.MustGetProperty(document, "Sheets").ToIDispatch() worksheet = oleutil.MustCallMethod(sheets, "getByIndex", index).ToIDispatch() return }
// LOSaveFile saves the current document. // Only works if a file already exists, // see https://wiki.openoffice.org/wiki/Saving_a_document func LOSaveFile(document *ole.IDispatch) { // use storeAsURL if neccessary with third URL parameter oleutil.MustCallMethod(document, "store") }