[Solved] Conditionally change font style

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
User avatar
AccessShell
Posts: 47
Joined: Tue Apr 05, 2022 2:17 am

[Solved] Conditionally change font style

Post by AccessShell »

I have searched and searched, but there seems to very little in articles about using VB6 with LO and CALC.

So here goes.
I am trying from VB6 to
1. Make a row, always the last row bold if the run date is a Friday.
2. Change the font color of the current cell if the cell above has a greater value.

Thanks
 Edit: Changed subject, was styles 
Make your post understandable by others 
-- MrProgrammer, forum moderator 
Last edited by AccessShell on Sun Apr 17, 2022 1:16 am, edited 1 time in total.
LibreOffice 7.3.3.2 (x64) on Windows 10 Home
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Populate sheet row; Conditionally change font style

Post by FJCC »

I have not written macros in VB for years and only did a few. Dealing with the API should be mostly the same as with LibreOffice Basic and you can use the same resources. The only VB specific information I know if is here: http://www.kalitech.fr/clients/doc/VB_APIOOo_en.html
Your item #1 seems like a good candidate for using conditional formatting instead of a macro.
Your second item also seems like a good candidate for conditional formatting if you do not need the format change to apply only when the cell is the current cell.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
AccessShell
Posts: 47
Joined: Tue Apr 05, 2022 2:17 am

Re: Populate sheet row; Conditionally change font style

Post by AccessShell »

I need to explain this better

Can I do this in VB6 for a CALC cell

Code: Select all

If Format(Date, "dddd") = "Friday" Then
        oSheet.GetCellRangeByName("E6").Formula = "=fontweight.Bold"
    End If
OK, so I know that what I wrote after the = sign in not correct. I already have some data in that cell. Without destroying that data, I want to make the font bold.

And, when I have data i n a cell, and I already have a formula there (such as adding some other cells, can I place another formula there, without destroying the data and first formula? In this case I may need additionally making the call contents bold (it is Friday) and maybe changing the color to red (the value less than the value in cell above it.

Thanks
LibreOffice 7.3.3.2 (x64) on Windows 10 Home
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Populate sheet row; Conditionally change font style

Post by FJCC »

Here are some simple ways to set the font weight and the text color based on value tests. Using a cell style is often the best solution.

Code: Select all

oSheet = ThisComponent.Sheets.getByName("Sheet1")
oCellA1 = oSheet.getCellRangeByName("A1")
IF WeekDay(oCellA1.Value) = 6 THEN 'Friday = 6
  'Manually set the CharWeight
  'oCellA1.CharWeight = com.sun.star.awt.FontWeight.BOLD 'or set CharWeight to 150
  
  'Alternate method, define a cell style that has bold font and apply that.
  oCellA1.CellStyle = "MyBoldStyle"
End IF

'Check if B2 < B1
oCellB2 = oSheet.getCellRangeByName("B2")
oCellB1 = oSheet.getCellRangeByName("B1")
IF oCellB2.Value < oCellB1.Value THEN
  'Manually set the text color
  oCellB2.CharColor = RGB(255,0,0)
  'You could, as above, apply a style to set the color
End IF
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
AccessShell
Posts: 47
Joined: Tue Apr 05, 2022 2:17 am

Re: Populate sheet row; Conditionally change font style

Post by AccessShell »

Thank you FJCC. It took some doing to understand CellStyles and how to make one, or two, etc.
But I finally got it.

Program works as needed.
LibreOffice 7.3.3.2 (x64) on Windows 10 Home
Post Reply