Hi all. I was just wondering, is there a way to get a macro to automatically increment the number of a section when you put it in by macro instead of manually? When you are doing sections manually, Writer automatically increments the number of the next section to the next number, so the code is in Writer itself somewhere. Like if you are doing a document and you already have Section1, Section2, Section3 -- then when you add a new section, in the New Section Dialog box or whatever, it will automatically have the name of your new section in there and incremented to "Section4". Just wondering if there is a method or function or anything which could be implemented in a macro so that it automatically incremented the Section number like Writer does automatically when you are doing them manually. I need this for my hidden section macros I talk about in this post viewtopic.php?f=20&t=103195&p=499409#p499409 which work great for the most part.
I can live without this functionality, if I have to, it would just make my macros about perfect if I could automatically increment the Section number in the macro itself, or by doing a 3rd macro that did this for me. It would pretty much fully automate the process -- as it is now, I am having to go in and add the numbers to the Sections manually, either each time I do the 'hiddenSection' macro or after I have put in a few sections and then go in and assign them all the correct number. Hope someone knows a way to do this, it would be great. Thanks
[Solved] Way to make a macro increment the number in section
[Solved] Way to make a macro increment the number in section
Last edited by SteveH_66 on Fri Oct 09, 2020 6:27 am, edited 1 time in total.
OS: Windows 10 X86_64 (build 19045); UI render: Skia/Raster; Libre Office: Version: 25.8.4.2 (X86_64)
Build ID: 290daaa01b999472f0c7a3890eb6a550fd74c6df; Apache OpenOffice: AOO4116m3(Build:9816) - Rev. 277251aa7e
Build ID: 290daaa01b999472f0c7a3890eb6a550fd74c6df; Apache OpenOffice: AOO4116m3(Build:9816) - Rev. 277251aa7e
Re: Way to make a macro increment the number in section?
Here is some minimal code to insert a text section with a name that increments with the number of sections, that can be either visible or hidden, and that is located at the current location of the cursor. I am not sure exactly what you want to do but I hope this will get you started.
Code: Select all
Sub TextSec
TextSection = ThisComponent.createInstance("com.sun.star.text.TextSection")
SectionCount = ThisComponent.TextSections.Count
TextSection.setName("SectionNumber" & SectionCount + 1
TextSection.isVisible = TRUE
oVC = ThisComponent.CurrentController.ViewCursor
oText = ThisComponent.Text
oText.insertTextContent(oVC.Start, TextSection, FALSE
End SubOpenOffice 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.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Way to make a macro increment the number in section?
Hi FJCC, your code will be a huge help to me, once I figure out how to integrate the function of my macros into your code, or how to take pieces of your code and integrate them into my macros. My macros are recorded macros, done as I was setting up the 2 actions I need to be able to do, as most macros by novices are recorded macros as you know -- since the learning curve is so steep for non-programmer. So of course my macro code looks a lot different than yours -- also I was using LO ver 4.2.3.3 for this, don't know if that makes any difference.FJCC wrote:Here is some minimal code to insert a text section with a name that increments with the number of sections, that can be either visible or hidden, and that is located at the current location of the cursor. I am not sure exactly what you want to do but I hope this will get you started.Code: Select all
Sub TextSec TextSection = ThisComponent.createInstance("com.sun.star.text.TextSection") SectionCount = ThisComponent.TextSections.Count TextSection.setName("SectionNumber" & SectionCount + 1 TextSection.isVisible = TRUE oVC = ThisComponent.CurrentController.ViewCursor oText = ThisComponent.Text oText.insertTextContent(oVC.Start, TextSection, FALSE End Sub
Just in case you were not interested enough to read the post I linked to where I described how I do the 2 macros I use and what they are for that I linked to in my OP, but are interested enough to know what I will be trying to do with your code, here is what I do with them. By the way, some of this is my original work, some of it was gotten from elsewhere, I couldn't find where I got it -- so when my system crashed and I lost them, I had to recreate them on my own and finally did -- with a lot of help from some other members/gurus here, many thanks to those folks
I have one macro, called "hideText", here is the code :
Code: Select all
sub hideText
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem ----------------------------------------------------------------------
dim args1(5) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Type"
args1(0).Value = 8
args1(1).Name = "SubType"
args1(1).Value = 2
args1(2).Name = "Name"
args1(2).Value = "hide"
args1(3).Name = "Content"
args1(3).Value = "1"
args1(4).Name = "Format"
args1(4).Value = 10000
args1(5).Name = "Separator"
args1(5).Value = " "
dispatcher.executeDispatch(document, ".uno:InsertField", "", 0, args1())
end sub
Code: Select all
sub hiddenSection
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem ----------------------------------------------------------------------
dim args1(6) as new com.sun.star.beans.PropertyValue
args1(0).Name = "RegionName"
args1(0).Value = "Section"
args1(1).Name = "RegionCondition"
args1(1).Value = "hide==1"
args1(2).Name = "RegionHidden"
args1(2).Value = true
args1(3).Name = "RegionProtect"
args1(3).Value = false
args1(4).Name = "LinkName"
args1(4).Value = ""
args1(5).Name = "FilterName"
args1(5).Value = ""
args1(6).Name = "SubRegion"
args1(6).Value = ""
dispatcher.executeDispatch(document, ".uno:InsertSection", "", 0, args1())
end sub
'hiddenSection' makes a section with the "Region Condition" Value "hide==1", so the section will be hidden if 'hideText' variable field is set to 1 in the document. As you may notice by looking at the code, the 'hiddenSection' "Region Name" value is "Section" -- when it was originally created "Region Name" value was "Section1" because that was the name the Section was originally created as -- I just deleted the 1, so all the hidden Sections would be named "Section" to cut down on confusion and possible macro problems. Then, I was having to change the Sections from Section to "Section1" etc -- either at the time I did the macro or go in after a few macros and modify them all so they had the right Section numbers.
So anyway, how the 2 macros work is that you put the cursor at the end of a line where you want a hidden section when outlining (because you want longer detailed text in there - say character descriptions or whatever) and run 'hideText' and it places that Variable field there with it set to 1. Then, you put a "Unnumbered Entry" under that line, and run 'hiddenSection' macro and it puts in a Section which you can hide/reveal just by changing the 'hideText' variable number from 1 to any other, because it's Hide Value was set to 'hide==1' when you recorded the macro. So it would look like this :
Code: Select all
[list=]
1. Chapter 1
1. Paragraph 1
1. Scene 1 [0]
This is the hidden Section, with text for scene 1.
2. Scene 2
2. Paragraph 2
[/list]OS: Windows 10 X86_64 (build 19045); UI render: Skia/Raster; Libre Office: Version: 25.8.4.2 (X86_64)
Build ID: 290daaa01b999472f0c7a3890eb6a550fd74c6df; Apache OpenOffice: AOO4116m3(Build:9816) - Rev. 277251aa7e
Build ID: 290daaa01b999472f0c7a3890eb6a550fd74c6df; Apache OpenOffice: AOO4116m3(Build:9816) - Rev. 277251aa7e
Re: Way to make a macro increment the number in section?
Here is a slightly modified version of my code that sets the section to be hidden when the variable named Var1 is set to 1. It seems that the section is visible when first inserted even if Var1 is 1. I had to toggle the variable to get the effect to work.
Code: Select all
TextSection = ThisComponent.createInstance("com.sun.star.text.TextSection")
SectionCount = ThisComponent.TextSections.Count
TextSection.setName("SectionNumber" & SectionCount + 1
TextSection.isVisible = FALSE
TextSection.Condition = "Var1==1"
oVC = ThisComponent.CurrentController.ViewCursor
oText = ThisComponent.Text
oText.insertTextContent(oVC.Start, TextSection, FALSE
end subOpenOffice 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.
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Re: Way to make a macro increment the number in section?
FJCC, your macro is working for my needs beautifully! Thank you so very much for modifying your code to help me out like that, it is such an awesome improvement to my outlining technique.
On the thing with the Section being visible even with Var1 set to 1 when it is first inserted, I noticed the same thing with my Set variable and my 'hiddenSection' macro. It must be some kind of bug or oddity in the Writer code. It is a very small hiccup to have, in return for the outlining functionality this method gives one. Again, thank you very much
P.S. Do you think this would be very hard for the developers to implement as a feature somewhere in Writer? If it would not be and you have any influence with the developers maybe you could suggest they do it. You would make a lot of people who use outlining but "don't like/can't learn" outlining with Navigator and want a simple method of outlining very happy. I have followed discussions for years on bugs and such filed asking for pretty much something like this for many years. Just a thought.
On the thing with the Section being visible even with Var1 set to 1 when it is first inserted, I noticed the same thing with my Set variable and my 'hiddenSection' macro. It must be some kind of bug or oddity in the Writer code. It is a very small hiccup to have, in return for the outlining functionality this method gives one. Again, thank you very much
P.S. Do you think this would be very hard for the developers to implement as a feature somewhere in Writer? If it would not be and you have any influence with the developers maybe you could suggest they do it. You would make a lot of people who use outlining but "don't like/can't learn" outlining with Navigator and want a simple method of outlining very happy. I have followed discussions for years on bugs and such filed asking for pretty much something like this for many years. Just a thought.
OS: Windows 10 X86_64 (build 19045); UI render: Skia/Raster; Libre Office: Version: 25.8.4.2 (X86_64)
Build ID: 290daaa01b999472f0c7a3890eb6a550fd74c6df; Apache OpenOffice: AOO4116m3(Build:9816) - Rev. 277251aa7e
Build ID: 290daaa01b999472f0c7a3890eb6a550fd74c6df; Apache OpenOffice: AOO4116m3(Build:9816) - Rev. 277251aa7e