OpenOffice Calc macro to add pie chart - macros

I am trying to insert a piechart in open-office using macro. But the code shows error:
Line:
Dim oDiagram As New com.sun.star.chart.PieDiagram
Error:
"Object not accessible. Invalid reference."
I am unable to figure out why. Kindly help. Here is my complete macro code:
Sub Macro1
Dim oRange as Object
Dim oRangeAddress(1) As New com.sun.star.table.CellRangeAddress
Dim oDiagram As New com.sun.star.chart.PieDiagram
Dim oRect As New com.sun.star.awt.Rectangle
Dim cTitle as String
oRange = thisComponent.getCurrentSelection.getRangeAddress
oSheets = ThisComponent.getSheets()
oSheet = oSheets.getByIndex(0)
oCharts = oSheet.Charts
oRect.Width = 10000
oRect.Height = 10000
oRect.X = 8000
oRect.Y = 1000
oRangeAddress(0).Sheet = oRange.Sheet
oRangeAddress(0).StartColumn = 0
oRangeAddress(0).StartRow = 0
oRangeAddress(0).EndColumn = 1
oRangeAddress(0).EndRow = 2
cTitle = "Test Results"
oCharts.addNewByName(cTitle,oRect,oRangeAddress(),TRUE, TRUE)
oChart = oCharts.getByName(cTitle).embeddedObject
oChart.Diagram = oDiagram
oChart.HasMainTitle = True
oChart.Title.String = cTitle
End Sub
Here is the input sheet data:

You can't instantiate a com.sun.star.chart.PieDiagram directly, independent of an already-existing chart. Instead, you'll have to create the chart first, and then create a PieDiagram. Thus, to make the macro work, do the following:
remove the line Dim oDiagram As New com.sun.star.chart.PieDiagram
change the line oChart.Diagram = oDiagram to oChart.Diagram = oChart.createInstance("com.sun.star.chart.PieDiagram").
This results in the following code (i've tested this with OpenOffice.org Calc 4.1.0 on Win7):
Sub Macro1
Dim oRange as Object
Dim oRangeAddress(1) As New com.sun.star.table.CellRangeAddress
Dim oRect As New com.sun.star.awt.Rectangle
Dim cTitle as String
oRange = thisComponent.getCurrentSelection.getRangeAddress
oSheets = ThisComponent.getSheets()
oSheet = oSheets.getByIndex(0)
oCharts = oSheet.Charts
oRect.Width = 10000
oRect.Height = 10000
oRect.X = 8000
oRect.Y = 1000
oRangeAddress(0).Sheet = oRange.Sheet
oRangeAddress(0).StartColumn = 0
oRangeAddress(0).StartRow = 0
oRangeAddress(0).EndColumn = 1
oRangeAddress(0).EndRow = 2
cTitle = "Test Results"
oCharts.addNewByName(cTitle,oRect,oRangeAddress(),TRUE, TRUE)
oChart = oCharts.getByName(cTitle).embeddedObject
oChart.Diagram = oChart.createInstance("com.sun.star.chart.PieDiagram")
oChart.HasMainTitle = True
oChart.Title.String = cTitle
End Sub
Running the macro should give the following result:
NB: The macro won't run on LibreOffice Calc; it only works with OpenOffice.org Calc. I think this a LibreOffice bug, since i coudn't find any differences in the API of OOo and LO.

Related

How to integrate the name of the file into a working word counter macro

I managed to adapt a vba macro (which I also found here) and got it running. So when the macro is started a file dialog asks me for the source file and the output gives me the word count of this file into cell "A1".
Public Sub word_counter()
Dim objWord As Object, objDocument As Object
Dim strText As String
Dim lngIndex As Long
Dim cellrange As String
Dim intChoice As Integer
Dim strPath As String
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
intChoice = Application.FileDialog(msoFileDialogOpen).Show
strPath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
Set objDocument = objWord.documents.Open(strPath)
strText = objDocument.Content.Text
objDocument.Close SaveChanges:=False
For lngIndex = 0 To 31
strText = Replace(strText, Chr$(lngIndex), Space$(1))
Next
Do While CBool(InStr(1, strText, Space$(2)))
strText = Replace(strText, Space$(2), Space$(1))
Loop
Sheets("calc tool").Select
Range("A1") = UBound(Split(strText, Space$(1)))
objWord.Quit
Set objDocument = Nothing
Set objWord = Nothing
End Sub
Now i want to add the filename to the output as text in cell "A2" right next to the word count of this file.
A1: 1234 A2: filename.docx
I tried to add the solution described in the SOF question 12687536
here!
The results were disappointing and i ran into compiling errors or run time error '91'
This was one of my solutions which didn't work out.
Public Sub word_count()
Dim objWord As Object, objDocument As Object
Dim strText As String
Dim lngIndex As Long
Dim cellrange As String
Dim intChoice As Integer
Dim strPath As String
Dim filename As String
Dim cell As Range
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
intChoice = Application.FileDialog(msoFileDialogOpen).Show
strPath = Application.FileDialog( _
msoFileDialogOpen).SelectedItems(1)
Set objDocument = objWord.documents.Open(strPath)
strText = objDocument.Content.Text
objDocument.Close SaveChanges:=False
For lngIndex = 0 To 31
strText = Replace(strText, Chr$(lngIndex), Space$(1))
Next
Do While CBool(InStr(1, strText, Space$(2)))
strText = Replace(strText, Space$(2), Space$(1))
Loop
Sheets("calc tool").Select
Range("A1") = UBound(Split(strText, Space$(1)))
filename = Application.GetOpenFilename
cell = Application.Range("A2")
cell.Value = filename
objWord.Quit
Set objDocument = Nothing
Set objWord = Nothing
End Sub
Any idea how to make this work?
You have to select a sheet before you can use Range().
Thus change
cell = Application.Range("A2")
cell.Value = filename
to
Range("A2") = filename
or better
Application.ActiveSheet.Range("A2").Value = filename
and you write the filename into the cell A2 in your active sheet.

Open Office programming

This is the code I have so far:
REM ***** BASIC *****
Option Explicit
rem-----------------------------------------------------------------------
REM----
Sub Main
Dim YearNm As Long, DayCol As Long, DayRow As Long
Dim DataSheet As Object, oSheet As Object
Dim SelDate As Date
Dim oDoc As String, oSelection As String
Set DataSheet = ThisComponent.CurrentController.ActiveSheet
oDoc = ThisComponent
oSelection = ThisComponent.CurrentSelection
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
'Private Sub Worksheet_Change(ByVal Target As Dim oSheet as Object[n]oSheet = ThisComponent.CurrentController.ActiveSheet[n]oSheet.getCellRangeByName($1))
'If Not Intersect(Target, Dim oSheet as Object[n]oSheet = `
'`ThisComponent.CurrentController.ActiveSheet[n]oSheet.getCellRangeByName($1)("AH6:AH29")) Is Nothing Then
If Not Intersect(Target,Range("AH6:AH29")) Is Nothing Then
'("AH5") = ThisComponent.CurrentController.ThisComponent.CurrentController.ActiveSheet.getCellDim oSheet as Object[n]oSheet = ThisComponent.CurrentController.ThisComponent.ActiveSheet[n}ThisComponent.CurrentController.Select(oSheet.getCellThisComponent.CurrentController.ThisCompponent.CurrentController.ActiveSheet.getCellDim oSheet as Object[n]oSheet = ThisComponent.CurrentController.ActiveSheet.getCellDim oSheet as Object[n]oSheet =ThisComponent.CurrentController.ActiveSheet[n]oSheet.getCellRangeByName($1)ByName(ByName(ByName($2)).Value))ed Date
'Range("AH5") = ThisComponent.CurrentController.ThisComponent.CurrentController.ActiveSheet.getCell = ThisComponent.CurrentController.ThisComponent.ActiveSheet.CurrentController.Select(oSheet.getCellThisComponent.CurrentController.ThisCompponent.CurrentController.ActiveSheet.getCellDim oSheet as Object[n]oSheet = ThisComponent.CurrentController.ActiveSheet.getCellDim oSheet as Object[n]oSheet = ThisComponent.CurrentController.ActiveSheet[n]oSheet.getCellRangeByName($1)ByName(ByName(ByName($2)).Value))as Date)
Range("AH5") = oSelection
'
YearNm = [ScYear]
'Determine if data worksheet exists
On Error Resume Next
Set DataSheet = ThisWorkbook.Sheets("" & YearNm & "")
On Error Goto 0
If DataSheet Is Nothing Then
ThisWorkbook.Sheets.Add(After:=Sheets("Schedule")).Name = YearNm
Set DataSheet = ThisWorkbook.Sheets("" & YearNM & "")
Active
End If
'
DayRow = Tatget.Row 'Row
DayCol = SelDate - DateSerial(YearNm, 1, 1) + 1 'Dtermine Column for Data Sheet
DataSheet.Cells(DayRow, DayCol).Value = Target.Value
End If
End Sub
Private Sub Sheet()
CurrentController.setThisComponent.CurrentController.ActiveSheet(vSheet);ionChange(ByVal Target As Dim oSheet as Object[n]oSheet = ThisComponent.CurrentController.ActiveSheet[n]oSheet.getCellRangeByName($1)("B6:AF29")) Is Nothing Then
If IsDate(Target.Value) = False Then Exit Sub
SlDate = Target.Value 'Selected Date
YearNm = [ScYear]
ThisComponent.CurrentControlller.ThisComponent.CurrentController.ActiveSheet.getCellDim oSheet as Object[n]oSheet = ThisComponent.CcurrentController.ActiveSheet[n]oSheet.getCellRangeByName($1)ByName(("AH5")).Value = SelDate
'Determine if data worksheet exists
On Error Resume Next
Set DataSheet = ThisWorkbook.Sheets("" & YearNm & "")
On Error Goto 0
If DataSheet Is Nothing Then
'
ThisWorkbook.Sheets.Add(After:=Sheets("Schedule")).Name = YearNm
Set DataSheet = ThisWorkbook.Sheets("" & YearNm & "")
Activate
End If
DayRow = Target.Row 'Row
DayCol = SelDate - DateSerial(YearNm, 1, 1) +1 'Determine Column for Data Sheet
ThisComponent.CurrentController.ThisComponent.CurrentController.ActiveSheet.getCellThisComponent.CurrentController.ThisComponent.CurrentController.ActiveSheet.getCellDim oSheet as Object[n]oSheet = ThisComponent.CurrentController.Activesheet[n]oSheet.getCellRangeByName($1)ByName(ByName(("AH6:AH29")).Value = DataSheet.ThisComponent.CurrentController.ThisComponent.CurrentController.ActiveSheet.getCellDim oSheet as Object[n]oSheet =
ThisComponent.Currentcontroller.ActiveSheet[n]oSheet.getCellRangeByName($1)ByName((DataSheet).getCellByPosition((6,DayCol), DataSheet.Cells(29, DayCol)), $3)).Value
End If
End Sub]
Basically what I need to happen is this: when a new day is selected, the value in cell "AH5" change to the selected date long version, "month day, year".
Thank you in advance.
I am trying to convert visual basic to open office.

Charts - OOo basic: how to display percentages in a PieChart

I am writing a macro to generate pie chart in OpenOffice Basic and I want to display the percentage of the different parts on the graph. For example, I want a result as in the following link:
https://learn.microsoft.com/en-us/sql/reporting-services/report-design/display-percentage-values-on-a-pie-chart-report-builder-and-ssrs
Here is my reproducible code:
my data are:
https://i.stack.imgur.com/hwdDz.png
And the Macro:
Sub Macro1
Dim oRange as Object
Dim oRangeAddress(1) As New com.sun.star.table.CellRangeAddress
Dim oRect As New com.sun.star.awt.Rectangle
Dim cTitle as String
oRange = thisComponent.getCurrentSelection.getRangeAddress
oSheets = ThisComponent.getSheets()
oSheet = oSheets.getByIndex(0)
oCharts = oSheet.Charts
oRect.Width = 10000
oRect.Height = 10000
oRect.X = 8000
oRect.Y = 1000
oRangeAddress(0).Sheet = oRange.Sheet
oRangeAddress(0).StartColumn = 0
oRangeAddress(0).StartRow = 0
oRangeAddress(0).EndColumn = 1
oRangeAddress(0).EndRow = 2
cTitle = "Test Results"
oCharts.addNewByName(cTitle,oRect,oRangeAddress(),TRUE, TRUE)
oChart = oCharts.getByName(cTitle).embeddedObject
oChart.Diagram = oChart.createInstance("com.sun.star.chart.PieDiagram")
oChart.HasMainTitle = True
oChart.Title.String = cTitle
End Sub
Thank you a lot for your help! I find the documentation for OOo basic very difficult to understand and rather poor.
Starting from https://forum.openoffice.org/en/forum/viewtopic.php?t=44377, I eventually came up with this code that does what you need.
rowProps = oChart.Diagram.getDataRowProperties(0)
rowProps.DataCaption = com.sun.star.chart.ChartDataCaption.PERCENT
Dim oLocale As New com.sun.star.lang.Locale
rowProps.PercentageNumberFormat = ThisComponent.getNumberFormats().queryKey(_
"0%", oLocale, True)
Documentation on number formats, such as there is, can be found at https://wiki.openoffice.org/wiki/Documentation/DevGuide/OfficeDev/Managing_Number_Formats. Many common tasks in Basic are pretty well documented, but what you are doing is somewhat unusual, hence the rather obscure documentation.
Feel free to keep asking for help -- your questions have been well written. With practice, working with the UNO API becomes easier.

OOo Basic: PieChart, how to change the colour of the graph

I am writing a macro to generate pie chart in OpenOffice Basic but I can't find the method to change the colour of the different part of the pie.
We can take as example the macro of this subject:
OpenOffice Calc macro to add pie chart
That is, my data are:
And my code:
Sub Macro1
Dim oRange as Object
Dim oRangeAddress(1) As New com.sun.star.table.CellRangeAddress
Dim oRect As New com.sun.star.awt.Rectangle
Dim cTitle as String
oRange = thisComponent.getCurrentSelection.getRangeAddress
oSheets = ThisComponent.getSheets()
oSheet = oSheets.getByIndex(0)
oCharts = oSheet.Charts
oRect.Width = 10000
oRect.Height = 10000
oRect.X = 8000
oRect.Y = 1000
oRangeAddress(0).Sheet = oRange.Sheet
oRangeAddress(0).StartColumn = 0
oRangeAddress(0).StartRow = 0
oRangeAddress(0).EndColumn = 1
oRangeAddress(0).EndRow = 2
cTitle = "Test Results"
oCharts.addNewByName(cTitle,oRect,oRangeAddress(),TRUE, TRUE)
oChart = oCharts.getByName(cTitle).embeddedObject
oChart.Diagram = oChart.createInstance("com.sun.star.chart.PieDiagram")
oChart.HasMainTitle = True
oChart.Title.String = cTitle
End Sub
How can I get some green in my chart, instead of blue, for example?
Thank you for your help.
Here is one solution.
Sub Macro1
...
oFirstDiagram = oChart.getFirstDiagram()
oColorScheme = CreateUnoListener("XColorScheme_", "com.sun.star.chart2.XColorScheme")
oFirstDiagram.setDefaultColorScheme(oColorScheme)
End Sub
Function XColorScheme_getColorByIndex(index As Integer) As Long
Dim result As Long
result = &H0000FF ' blue
If index = 0 Then
result = &H00FF00 ' green
ElseIf index = 1 Then
result = &HFFFF00 ' yellow
End If
XColorScheme_getColorByIndex = result
End Function
The only relevant documentation I could find for this approach is the API docs: https://www.openoffice.org/api/docs/common/ref/com/sun/star/chart2/XDiagram.html.
Another way is to put the colors in column C.
Status Count Color
Unfinished 20 =COLOR(0,255,0)
Finished 30 =COLOR(255,0,0)
Then set the Range for Fill Color to use column C. If you want to see code for this second approach, post a comment and I'll look into it.
Yet another way is from https://forum.openoffice.org/en/forum/viewtopic.php?t=36001.
oChart.Diagram.DataRowSource = com.sun.star.chart.ChartDataRowSource.COLUMNS
oChart.FirstDiagram.CoordinateSystems(0).ChartTypes(0).DataSeries(0).Color = &H00FF00
However, this last approach did not change the color when I tried it.

can't able to convert axmschart as bitmap.(vb.net)

i am working on a project which is migrated from vb6 to vb.net.And during working with the crystal reports i got this problem.Actually i have to print the axmschart of my application in reports.So i am using the following code :
gtmpString = Application.StartupPath & gsGraphPicPath
Dim myPic As New Bitmap(_chtAnlysGraph_0.AsBitmap)'''''ERROR here...as bitmap is not a member of AXMSCHART''''''''
PictureBox1.Image = myPic
PictureBox1.Image.Save(gtmpString, System.Drawing.Imaging.ImageFormat.Png)
Dim intCount As Short
Dim dRow As DataRow
Dim dTable As New DataTable
Dim dt As New DataSetResults.AnalysisTableDataTable 'Report filling
dTable = dt.Copy
dRow = dTable.NewRow
For intCount = 1 To msgAnlysData.Rows - 1
dRow = dTable.NewRow
dRow.Item("Sr_No") = msgAnlysData.get_TextMatrix(intCount, 1)
dRow.Item("abs_val") = msgAnlysData.get_TextMatrix(intCount, 2)
dRow.Item("pt_conc") = msgAnlysData.get_TextMatrix(intCount, 3)
dRow.Item("lin_conc") = msgAnlysData.get_TextMatrix(intCount, 4)
dRow.Item("poly_conc") = msgAnlysData.get_TextMatrix(intCount, 5)
'If gtmpString = True Then
gtmpString = Application.StartupPath & gsGraphPicPath
dRow.Item("graph1") = savepic(gtmpString)
' End If
If msgAnlysData.get_TextMatrix(intCount, 2) = Nothing Then
Else
dTable.Rows.Add(dRow)
End If
Next intCount
Help me out here plzz.
Dim myPic As New Bitmap(_chtAnlysGraph_0)