Changing programmatically print area in OpenOffice Calc - openoffice-calc

I'm creating a Calc document on the fly with vb6. I need to repeat 1 row and 1 column in every page when i print it.
This is the code:
Dim mPrintOptions(2) As Object
Dim OO_Dispatcher As Object
Set OO_Dispatcher = oServiceManager.createInstance("com.sun.star.frame.DispatchHelper")
Set mPrintOptions(0) = MakePropertyValue(oServiceManager, "PrintArea", "")
Set mPrintOptions(1) = MakePropertyValue(oServiceManager, "PrintRepeatRow", "$A$2")
Set mPrintOptions(2) = MakePropertyValue(oServiceManager, "PrintRepeatCol", "$A$1")
OO_Dispatcher.executeDispatch oDeskTop, ".uno:ChangePrintArea", "", 0, mPrintOptions
I've got this code making a macro in a saved document.
Service manager and Desktop objects are previously instanced. The document is being created fine, but when I send it to the printer it does not repeat the row and the column I specified above.

I've found my solution here:
https://wiki.openoffice.org/wiki/ES/Manuales/GuiaAOO/TemasAvanzados/Macros/StarBasic/TrabajandoConCalc/Imprimiendo
My code finally got like this:
Dim OO_TitulosR As Object
Dim OO_ActiveSheet As Object
Set OO_TitulosR = OO_Document.Bridge_getStruct("com.sun.star.table.CellRangeAddress")
Set OO_ActiveSheet = OO_Document.getCurrentController.getActiveSheet
OO_TitulosR.StartColumn = 0
OO_TitulosR.EndColumn = 0
OO_TitulosR.StartRow = 1
OO_TitulosR.EndRow = 1
OO_ActiveSheet.setTitleColumns OO_TitulosR
OO_ActiveSheet.setTitleRows OO_TitulosR

Related

Copy certain number of Columns in a row

I update sheets on a weekly basis, I import an external file (starting point) using the code below import selected Columns to sheet2- all is good.
Sub Copy_Specific_Columns_ToAnother_Sheet()
Sheets("Data").Range("C:C").Copy Sheets("Sheet2").Range("B:B")
Sheets("Data").Range("D:D").Copy Sheets("Sheet2").Range("C:C")
Sheets("Data").Range("B:B").Copy Sheets("Sheet2").Range("D:D")
Sheets("Data").Range("I:I").Copy Sheets("Sheet2").Range("E:E")
Sheets("Data").Range("K:K").Copy Sheets("Sheet2").Range("F:F")
Sheets("Data").Range("H:H").Copy Sheets("Sheet2").Range("G:G")
Sheets("Data").Range("J:J").Copy Sheets("Sheet2").Range("H:H")
Sheets("Data").Range("AF:AF").Copy Sheets("Sheet2").Range("I:I")
'Clean up sheet with formatting
ActiveSheet.UsedRange.Font.Size = 12
ActiveSheet.UsedRange.Font.Name = "Calibri"
ActiveSheet.UsedRange.EntireColumn.AutoFit
ActiveSheet.UsedRange.EntireRow.AutoFit
Range("a1").EntireRow.RowHeight = 45
Range("a2").EntireRow.RowHeight = 30
End Sub
On sheet2 I create a unique identifier (non macro) and import the sheet 2 details to a Master sheet where I make edits from Columns K onwards.
The code below looks for the unique identifier and pull in new rows
** However when new rows are added it means that my notes from column L onwards are deleted every time i update.
Can the code below be modified so that new rows only update to a specific column (say upto K) leaving my additional notes and entries untouched.. ? ("A" column has the unique identifier, MACRO looks for changes and pulls in new rows)
Sub Update_Data()
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim recRow As Long
Dim lastRow As Long
Dim fCell As Range
Dim i As Long
'Define our worksheets
Set wsSource = Worksheets("Sheet2")
Set wsDest = Worksheets("Master")
Application.ScreenUpdating = False
recRow = 1
With wsSource
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRow
'See if item is in Master sheet
Set fCell = wsDest.Range("A:A").Find(what:=.Cells(i, "A").Value, lookat:=xlWhole, MatchCase:=False)
If Not fCell Is Nothing Then
'Record is already in master sheet
recRow = fCell.Row
Else
'Need to move this to master sheet after last found record
.Cells(i, "A").EntireRow.Copy
wsDest.Cells(recRow + 1, "A").EntireRow.Insert
recRow = recRow + 1
End If
Next i
End With
'Code clean up
Application.CutCopyMode = False
Application.ScreenUpdating = True
'Clean up sheet with formatting
ActiveSheet.UsedRange.Font.Size = 12
ActiveSheet.UsedRange.Font.Name = "Calibri"
ActiveSheet.UsedRange.EntireColumn.AutoFit
ActiveSheet.UsedRange.EntireRow.AutoFit
Range("a1").EntireRow.RowHeight = 45
Range("a2").EntireRow.RowHeight = 30
End Sub

DataGridview I tried an if-else-condition in DataGridView cells

DataGridView
I tried an if-else-condition in DataGridView cells
Dim RowIdx As Integer
RowIdx = dgAttendance.CurrentRow.Index
If dgAttendance.Item(3, RowIdx).Value = "P" Or dgAttendance.Item(3,
RowIdx).Value = "P " Then
dgAttendance.Item(3, RowIdx).Value = "A"
EndIF
Please give the solution in C#. How do i use that if-else-statement in C#
Based on the example you provided in your comments - you should not call .ToString() when setting your value as this removes the reference to your cell and returns an independent value. Try this instead:
dg_attendance.CurrentCell.Value = "A";
You also probably want to access the value of the third cell in your row by something like this:
dg_attendance.CurrentRow[3].Value

Crystal Report converting value to number with symbol

I have an issue with a Crystal Report that I'm creating. I am using fields from a database and am pulling in the result value where the analysis field is equal to certain values.
In the condition the first check looks at the analysis field and checks if its equal to "Conf". The result for this is "<10"
The second check looks at the analysis field and checks if its equal to "Original". The result for this is "20".
I want the results to display in the order above however with the following basic logic it returns the result of 20.
if analysis = "conf" then result
else if analysis = "Original" then result
I was having this issue with multiple records however solved it by converting both results to numbers (toNumber(Result)). However this record has the less than symbol contained within the field value which causes the conf result to "be skipped" and will display the original result instead. I've tried a few things without success. Here is the code for the condition of where I'm at below. I fell this is way to complex logic but I've just added to it as I've had ideas and it shows what I've tried.
if {UNITS} = "CFU_G" then
if {ANALYSIS} = "CONF" and
{RESULT}="" or
{RESULT} = "0" then 0
else if {ANALYSIS} = "CONF"
then if isNumeric({RESULT}) then
tonumber({RESULT}) else
tonumber(Replace ({RESULT}, "<", ""))
else
if {UNITS} = "CFU_G" then
if {ANALYSIS} = "Original" and
{RESULT}="" or
{RESULT} = "0" then 0
else if {ANALYSIS} = "Original"
then if isNumeric({RESULT}) then
tonumber({RESULT}) else
tonumber(Replace ({RESULT}, "<", ""))
Thanks,
Tom
This was the solution I came up with.
Field 1
whileprintingrecords;
stringvar vResult := "";
Field 2
whileprintingrecords;
stringvar vResult;
vResult := if {RESULT.UNITS} = "CFU_G"
and {RESULT.ANALYSIS} = "CRA_LIS_ENU_CONF_MPCRAM29"
then {RESULT.FORMATTED_ENTRY}
else if {RESULT.ANALYSIS} = "CRA_LIST_ENU_MPCRAM29"
and {RESULT.UNITS} = "CFU_G"
and vResult = ""
then {RESULT.FORMATTED_ENTRY}
Field 3
whileprintingrecords;
stringvar vResult;
vResult;

How do I create a list in cells choosing from a combobox and selecting a button using VBA?

I have a combobox filled with values. I want to select a value in the combo box and click the "Add" button to place this value into the some cells below. I can add one item to my list using the following code, but I want to be able to add multiple items. I feel that I am very close, I just need a few tweaks!
Private Sub CommandButtonAddItem_Click()
Dim ws As Worksheet
Dim box As ComboBox
Dim food As String
Dim num As Integer
num = 19
Set ws = Worksheets("sheet1")
Set box = ws.OLEObjects("ComboBox1").Object
food = box.Value
Worksheets("sheet1").Cells(num, 1) = food
If Worksheets("sheet1").Cells(num, 1) = " " Then
Worksheets("sheet1").Cells(num, 1) = food
num = num + 1
End If
End Sub
Try THIS!
If the "default" cell is already occupied, it'll keep going down untill it finds one that's not empty, to then put the value in that cell.
Private Sub CommandButtonAddItem_Click()
Dim ws As Worksheet
Dim box As ComboBox
Dim food As String
Dim num As Integer
num = 19
Set ws = Worksheets("sheet1")
Set box = ws.OLEObjects("ComboBox1").Object
food = box.Value
While Worksheets("sheet1").Cells(num, 1) <> ""
num = num + 1
Wend
Worksheets("sheet1").Cells(num, 1) = food
End If
End Sub

Cell content inside formula

Is it possible to place the content of a cell inside a formula. By formula I mean the math formula editor (insert->object->formula).
To the best of my knowledge, there is no way to reference a cell from a formula. Math formula editor has no knowledge about OO Calc. However, you can create a new formula whenever needed using macros.
Follow thesse steps to make it work:
Put the math formula you want to insert to a cell. For example, put some numbers to cells A1, A2, A3 and put the following to cell C3:
=CONCATENATE("{";A1;"}";"over {";A2;" `+` ";A3;"}";" `=` ";A4).
This will generate something like {1} over {2 `+` 3} `= in C3
Create a macro from the code below. In OO Calc, select
Tools > Macros > Organize Macros > OpenOffice.org Basic > My Macros > Standard
Create a new macro and paste the code below.
Now you can run macro using Tools > Macros > Run Macro. Run either insertFormula which inserts math formula generated from cell C3, or addFormulaListener which will register a listener and regenerate the formula for you whenever contents of C3 changes.
Here is the code. It contains constants formulaCellFrom and formulaCellTo, which specify which cell has the math formula source and which is the target cell where the generated formula object shall be placed. Note that the target cell must be large enough for the generated formula, otherwise the macro won't delete cell's old content when regenerating the formula.
const formulaCellFrom As String = "$C$1"
const formulaCellTo As String = "$C$10"
rem ----------------------------------------------------------------------
rem Adds listener for changes of the math formula
sub addFormulaListener
dim oSheet as Object
dim oCell as Object
rem go to cell containing markup
oSheet = ThisComponent.CurrentController.ActiveSheet
oCell = oSheet.getCellRangeByName(formulaCellFrom)
rem add listener
oListener = CreateUnoListener( "formulaListener_", "com.sun.star.chart.XChartDataChangeEventListener" )
oCell.addChartDataChangeEventListener(oListener)
end sub
rem ----------------------------------------------------------------------
rem Listener for cell changes
sub formulaListener_chartDataChanged
dim oCell as Object
rem remember current cursor position
oCell = ThisComponent.CurrentSelection
rem call insertFormula
call insertFormula
rem restore cursor position
ThisComponent.CurrentController.select(oCell)
end sub
rem ----------------------------------------------------------------------
rem Creates a math formula from text in cell C1 and inserts it into cell C10
sub insertFormula
dim document as object
dim dispatcher as object
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem go to cell containing markup and copy it
dim fromCellArgs(0) as new com.sun.star.beans.PropertyValue
fromCellArgs(0).Name = "ToPoint"
fromCellArgs(0).Value = formulaCellFrom
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, fromCellArgs())
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())
rem go to cell where I want the formula displayed
dim toCellArgs(0) as new com.sun.star.beans.PropertyValue
toCellArgs(0).Name = "ToPoint"
toCellArgs(0).Value = formulaCellTo
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, toCellArgs())
rem delete previous content
dim deleteArgs(0) as new com.sun.star.beans.PropertyValue
deleteArgs(0).Name = "Flags"
rem flags: A = All, S = String, V = Value, D = DateTeim, F = Formula, ...
rem ... N = Notes, T = Formats, O = Objects
deleteArgs(0).Value = "AO"
dispatcher.executeDispatch(document, ".uno:Delete", "", 0, deleteArgs())
rem open Star.Math
oDesk = createUnoService ("com.sun.star.frame.Desktop")
dispatcher.executeDispatch(document, ".uno:InsertObjectStarMath", "", 0, Array())
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem paste clipboard using Array() as place-holder for variable name
dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())
rem exit Star.Math
dispatcher.executeDispatch(document, ".uno:TerminateInplaceActivation", "", 0, Array())
end sub
The code was adapted from this question. Apparently, the macro must be created in My Macros and doesn't work when embedded in the spreadsheet (security measure? it just didn't work for me). The source and target cells are hardcoded but you can modify the macro to suit your needs. I'm not skilled in Visual Basic but such modifications should be easy.