Sub Click(Source As Button)
Set objServiceManager = CreateObject("com.sun.star.ServiceManager")
Set objDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop")
'设置对象隐藏,有些时候,是不需要用户看见过程的.
' Dim args(0)
' Set args(0) = MakePropertyValue("Hidden",True)
%REM
'打开本地指定路径下的一个xls文件.
Dim url As String
url = "file:///C:/template.xls"
Set objDocument = objDesktop.loadComponentFromURL(url,"_blank",0,args)
%END REM
'创建一个新的open office scalc文档
Dim args()
Set objDocument = objDesktop.loadComponentFromURL("private:factory/scalc", "_blank", 0, args)
Dim oos As Variant
Set oos = objDocument.sheets
%REM
'新增一张工作表,命名为:Comments,并获取该工作表对象.
Call oos.insertNewByName("Comments", oos.getCOunt())
Set os = oos.getByName("Comments")
%END REM
'获取scalc中的第一个工作表.
Set os = oos.getByIndex(0)
Dim oCell As Variant
Set oCell = os.getCellByPosition(0,0)
oCell.setValue(2000) '设置单元格的值
oCell.CellBackColor = 16764057 '设置单元格的背景
'保存文件为MS EXCEL 97格式,确保MS EXCEL能打开该文件.
Dim mProps(1)
Set mProps(0) = MakePropertyValue("FilterName","MS Excel 97")
Set mProps(1) = MakePropertyValue("Overwrite",True) '当指定目录下存在同名文件事,覆盖重写.
path = "file:///C:/test.xls" '保存路径.
objDocument.storeAsURL path,mProps 'save
objDocument.Close(True) 'close the application
End Sub
================================================
Function MakePropertyValue(cName, uValue) As Variant
Dim oPropertyValue As Variant
Set oSM = CreateObject("com.sun.star.ServiceManager")
Set oPropertyValue = oSM.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
oPropertyValue.Name = cName
oPropertyValue.Value = uValue
Set MakePropertyValue = oPropertyValue
End Function