[Solved] How to run a Writer macro in a Calc macro?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by OldStd »

Hi Tibor.

Thank you for the tip and the odt document.

I downloaded it 3 times, the first 2 from the handphone as I was downloading something using the handphone for the first time. Then I found that the macro was not included in the download. The third was downloaded using my PC.

It is [ParaStyleName] that I didn't know of before.

Let me play with it for a while and see if it works with my example, too.
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by OldStd »

Hi. Tibor.

With the inspiration from your example,
I have successfully converted the [ ".uno:StyleApply"] into API codes.

The following is the code sample applied to my sample document, where the customized numbering style is named as “Test_Numbering_01”, stored in a template:

Code: Select all

oSel.NumberingStyleName = oStyle.name

Code: Select all

Sub ChangeDocStyle
dim oFamilies as object, oStyle as object, oSel as object
rem oDoc2 is a global variable declared and used in an earlier sub-routine
oSel = fnSel_All(oDoc2)
oFamilies = oDoc2.StyleFamilies.getByName("NumberingStyles")
oStyle = oFamilies.getByName("Test_Numbering_01")
oSel.NumberingStyleName = oStyle.name
End Sub
Openoffice 4.15
Windows 10
User avatar
Zizi64
Volunteer
Posts: 11361
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by Zizi64 »

oSel.NumberingStyleName = oStyle.name
The value of the expression "oStyle.name" is a string: the name of a specific style. You can apply it directly:

Code: Select all

oSel.NumberingStyleName = "Test_Numbering_01"
You must get the style OBJECT itself only when you want modify some properties of the style. Therefore the next lines are not necessary in this case:

Code: Select all

oFamilies = oDoc2.StyleFamilies.getByName("NumberingStyles")
oStyle = oFamilies.getByName("Test_Numbering_01")
The reduced macro code:

Code: Select all

Sub ChangeDocStyle
dim  oSel as object
oSel = fnSel_All(oDoc2)
oSel.NumberingStyleName = "Test_Numbering_01"
End Sub
...

The final resolution depends on the condition, if you want to use a simple numbering style on a short part of the text, or you want to build a whole structure of the chapter numbering in your document.
Read: do not apply the indents by direct formatting method (even by a macro) but use the indent property (and the predefined indent value) of the applied style/s/.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by OldStd »

Hi. Tibor.

Thanks for the coaching.

I guess those codes were there as I struggled to have a grab on the meanings of these terms when I read the sample codes and blindly copied and pasted them.

Code: Select all

oFamilies = oDoc2.StyleFamilies.getByName("NumberingStyles")
oStyle = oFamilies.getByName("Test_Numbering_01") 
I like your simplified codes. Elegant.

I still don't quite understand what you mean by
but use the indent property (and the predefined indent value) of the applied style
From what I understand, so far, after we choose a built-in style template, or a manually defined template, we still need to apply the indents manually, by using the indent icon, or [heading] methods, pushing [ctrl 1] to [ctrl 10].

The point is, after applying the paragraph [heading] styles by pushing the [ctrl n] shortcut keys, the indenting on the page is not updated. If we do it manually, we have to go the predefined style and double-click it to reformat the selected area. This is here that I run the macro to update the indenting of the selected area.

Question: Using the following codes,

Code: Select all

oSel.NumberingStyleName = "Test_Numbering_01" 
equivalent to double-clicking manually on the style, is it considered
apply the indents by direct formatting method (even by a macro)
Or is it considered
use the indent property (and the predefined indent value) of the applied style
or neither​?
Openoffice 4.15
Windows 10
User avatar
Zizi64
Volunteer
Posts: 11361
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by Zizi64 »

You can see in my last sample document, that I had applied different paragraph styles to the text, based on the first characters of the text located in the paragraph. And the different styles was adjusted (manually) differently: the text color, the Chapter numbering level, and the indent value...

It is better to use more than one different styles if you want different indent values - instead of the manually/programatically DIRECT adjusted indent values on same style for every paragraph.
When you want to modify the indent value of an indent "level" in the future, it will be enough to modify (manually or programatically) the indent property of the relevant style. And not needed modify the paragraphs one by one... This is the advantage of the styles.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by OldStd »

Hi. Tibor.

Thanks for the tips.

I have come across "Chapter numbering" somewhere before in my recent reading. I need to follow up on this.
I'll play around with styles for a while and revise the relevant sections in the eBooks to consolidate my learning.

Thanks and regards
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by OldStd »

Hi. Tibor.

I have tried using the [Outline Numbering] and made some amendments to the levels, adapted your Macro, and converted my old sample document with # symbols to an outlined numbered document:
  • I. The macro is doing the following:
    • I.1. Load the sample document
    • I.2. Select all the contents and get the transferable
    • I.3. Load an existing writer document to contain the transferable
    • I.4. Insert the transferable to the destination document and save it
    • I.5. It then loops through all the paragraphs
      • I.5.1. Apply [level] style based on the number of # symbols at the beginning of the paragraphs
      • I.5.2. If there are no # symbols then apply the style [Body text indent]
    • I.6. When the above are completed, the # symbols are removed.
  • The macro was adapted from your sample codes.
    This exercise provided an opportunity for me to learn much about coding in API and also making use of outline numbering using headings and levels, other than using list numbering.
Attachments
OO_Test_04.7z
Sample document included in the 4 files attached
(57.01 KiB) Downloaded 167 times
Openoffice 4.15
Windows 10
User avatar
Zizi64
Volunteer
Posts: 11361
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by Zizi64 »

Yes, it now seems as a real MACRO . ;) :)

Next steps:
- Try to use PASSED parameters instead of the GLOBAL variables.
- And try to write a main routine that will call all of the "sub" routines in the appropriate order. (Do not call the "next step" routine from the end of a subroutine)

With these methods you can make routines that can be used several times, for several tasks.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by OldStd »

Hi. Tibor.
Yes.Sir.
I'll try your suggestions and see how the macros will turn out.
Thanks for the tips.
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by OldStd »

Hi. Tibor.

I have removed the [global variables], and used the [passed parameters] in the subroutines and functions, and arranged the subroutines in running order, inside the [main] subroutine as suggested.

The codes of the [Sub main] is displayed here.

Code: Select all

Sub Main
dim oDst_Doc as object
oDst_Doc = Transfer_text_from_SrcDoc_to_Dst_Doc
ApplyThreeStyle_v2 (oDst_Doc)
remove_all_sharps (oDst_Doc)
oDst_Doc.store()
 oDst_Doc.CurrentController.Frame.ContainerWindow.toFront()
 oDst_Doc.unlockControllers() 
 oDst_Doc.CurrentController.Frame.ContainerWindow.Visible = True
rem wait 6000
rem msgbox ("Closing destination document now!")
rem oDst_Doc.close(true)
End Sub
As the codes loop through a few hundreds of paragraphs, I try to make the processing time a little less by turning the screens to [invisible], and screen updating to [turned off] during the looping.

To turn the screen visible/invisible:

Code: Select all

ThisComponent.CurrentController.Frame.ContainerWindow.Visible = False
ThisComponent.CurrentController.Frame.ContainerWindow.Visible = True
ThisComponent.CurrentController.Frame.ContainerWindow.toFront()
To turn the screen updating on/off

Code: Select all

ThisComponent.lockControllers()
ThisComponent.unlockControllers()
Attachments
OO_Test_05.7z
Using passed parameter and no global variables
(59 KiB) Downloaded 164 times
Openoffice 4.15
Windows 10
User avatar
Zizi64
Volunteer
Posts: 11361
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by Zizi64 »

Tips for the easier testing by the forum users (see the modified macro code in the .ods file):
OO_Test_05_zizi64.zip
(69.47 KiB) Downloaded 173 times
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by OldStd »

Hi. Zizi64.
Thanks for the tips.
On my own, it will take me a long, long time before I'd stumble across the methods of getting the current folder and its url.
It is very helpful and it certainly makes it easier for another person to test my codes in his PC with very different file/folder structure.
Best regards.
Openoffice 4.15
Windows 10
User avatar
RoryOF
Moderator
Posts: 34618
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by RoryOF »

You should look at the OO BASIC manual for methods of converting from any file structure into the URL structure used internally by OO; this will allow your code be platform independent. From memory this is in the OO BASIC manual in a section entitled "Working with Documents".
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by OldStd »

Hi. RoryOF.

Thanks for the tip.
I'll look it up and see what I can do with it.

Best regards.
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by OldStd »

Hi. RoryOF.
I have done some searching. Your recommendation is found in the following eBook/Website I have already implemented the function in my recent codes.
With that said, my second reading brings home what you have emphasized: Making the codes [platform independent].

Thanks & regards.
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by OldStd »

Hi. Everyone.
 Edit: How to convert the dispatcher functions into API codes 

In my original example, as I didn't know about [Outline Numbering] making use of [Heading] levels, I chose to use the [Numbering List] style. I was doing the whole thing quite inefficiently:
  • I. First I copied the unprocessed sample of text from [writer] doc to a [Calc] worksheet.
    As writer and calc have different structure, a conversion using [paste special] is required.
    This calls for a series of [dispatcher] functions.
    • I.1.1. fnDispatch(oDoc1, "Copy")
      I.1.2. fnDispatch(oDoc2, "ClipboardFormatItems", array( "SelectedFormat",10))
    II. After the text is pasted to the writer doc.
    I have to turn the text from table to text,
    using the function:
    • II.1. fnDispatch(oDoc2, "ConvertTableToText" , array("Delimiter", "#"))
I have since converted the [SelectAll] function into API codes.
Some advice on how to convert these 3 dispatcher functions into API codes is much appreciated.
The function was adapted from slide 16/45 of OooCon2005.
http://www.openoffice.org/marketing/ooo ... day_b3.pdf
My fnDispatch

Code: Select all

function fnDispatch(sCommand as string, optional mArgs)
oFrame = ThisComponent.getCurrentController.getFrame
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
'on error resume next
if isMissing(mArgs) then
fnDispatch = oDispatcher.executeDispatch(oFrame, ".uno:" & sCommand, "", 0, array())
else
nArgs = uBound(mArgs) \ 2
dim Args(nArgs) as new com.sun.star.beans.PropertyValue
for i = 0 to nArgs
Args(i).name = mArgs(i * 2)
Args(i).value = mArgs(i * 2 + 1)
next
fnDispatch = oDispatcher.executeDispatch(oFrame, ".uno:" & sCommand, "", 0, Args())
end if
end function
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by OldStd »

Sudoku Games. Anyone?
Hi. Tibor and Everyone.
Thank you for your tips and guidance in my learning to program in OO Basic.
It looks like this example has served its purpose in helping me learn some basic skills in programming OO Basic in API codes.
Hopefully, a few examples of [Dispatch functions] that tidy up the codes are acceptable.
I always feel that having a [pet project] would make learning easier as the project forces us to dig for items that help to solve some problems at hand.
  • I. Recently, a childhood classmate of mine told me he is playing Sudoku games everyday.
    • I.1. I am now wondering whether I could use the OpenOffice to make some sort of [Sudoku solver] that can at least solve some easy to medium Sudoku games?
      • I.1.1. Any suggestions from anyone as to what application to use: writer, calc, base, or impress?
        I.1.2.What do I need to learn to start the project going?
        I.1.3. Any suggestions of some other [pet project] to adopt will be much appreciated.
    II. Should I post this under a different topic or a sub-forum?
Openoffice 4.15
Windows 10
User avatar
Zizi64
Volunteer
Posts: 11361
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by Zizi64 »

II. Should I post this under a different topic or a sub-forum?
It is an another question, another problem, please create a new topic for it .
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
Posts: 11361
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by Zizi64 »

Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by OldStd »

Hi. Tibor.
Thanks for the info.
I'll take a look at the links and create a new post later.
Best regards.
Openoffice 4.15
Windows 10
OldStd
Posts: 116
Joined: Tue Sep 11, 2018 8:46 pm

Re: [Solved] How to run a Writer macro in a Calc macro?

Post by OldStd »

Hi. Tibor and Everyone.

I have created a new post for the topic on Sudoku at the following site.

viewtopic.php?f=5&t=95466

See you there.
Openoffice 4.15
Windows 10
Post Reply