Macro for hidden text

How do I create a macro to format hidden text when text selected?
I can create a macro to make text bold, or other simple formatting changes. I use Record macro, then format/text/bold, stop recording, and when dialog box pops up I save macro.
But when I Record macro, then select format/character/hidden, stop recording, dialog box does not pop up so nothing saved, or at least I cannot see it. Any ideas?

My tip:
Use the Styles (and/or the Templates) for this task instead of the macros.

2 Likes

Try:

Professional text composition with Writer

2 Likes

I see that the hidden text is invisible when created. This makes it unusable.The point of hidden text is to not be printed in printed document. If invisible I cant remember what I have written. Any way to see the hidden text? In MSword there is Option to have hidden text visible.

To see hidden text:

  1. Check tick the Hidden Paragraphs box in the dialogue box Tools - Options - LibreOffice Writer - View. This setting is unchecked by default.
  2. Enable View - Formatting Marks Ctrl+F10

Text still invisible.
I used macro below:

Sub hideText
dim oDoc as object, oVCur as object*
oDoc=ThisComponent*
oVCur=oDoc.CurrentController.ViewCursor 'visible cursor*
oVCur.CharHidden=True 'set to hidden*
End Sub

This macro still makes text invisible at default. I want it to display at default. I checked Tools/option/writer/view. I also checked writer/formatting/display hidden text. It disappears when I run above macro. **I cannot keep it visible. ** NB Toolbar/View/Field hidden para is checked.
Also, if I check toolbar/format/character/font effect/hidden text text also disappears.

Maybe it is time to read the help on hiding text. There are built-in ways to do what I presume you want to do, see Hiding Text

You might find the Writer Guide useful too, but particularly the book Designing with LibreOffice, download from English documentation | LibreOffice Documentation - LibreOffice User Guides

Okay. Good advice.

To see the hidden text: Tools/ Options → LibreOffice Writer/ Formatting Aids → Hidden characters
And then: View - Formatting Marks (Ctrl+F10)

The macro exists for example via ViewCursor - I didn’t find the UNO command for Hidden text.

Sub hideText
	dim oDoc as object, oVCur as object
	oDoc=ThisComponent
	oVCur=oDoc.CurrentController.ViewCursor 'visible cursor
	oVCur.CharHidden=True 'set to hidden
	'oVCur.CharHidden=False 'remove hidden property
End Sub

But the Styles are really better than direct formatting mainly in large document.

2 Likes

Thanks. Your code worked well. May I ask if you could give me the code for turning hidden format off ?

There is commented line 'oVCur.CharHidden=False 'remove hidden property :-).

1 Like

You can also test this macros. setHiddenStyle set the Character Style and not the direct formatting. And toggleHiddenStyle change the visibility and background color in the Style for better clarity. You can comment two lines oStyle.CharBackColor to change only visibility. This change of visibility also will make the characters printable.

global const cStyle="My Hidden Text" 'name of character style for Hidden Text

Sub setHiddenStyle 'set the Hidden Character Style to selection (it creates style if not exists)
	dim oDoc as object, document as object, dispatcher as object, oStyles as object, oStyle as object
	oDoc=ThisComponent
	oStyles=oDoc.StyleFamilies.getByName("CharacterStyles")
	if NOT oStyles.hasByName(cStyle) then 'style doesn't exist, so create it
		oStyle=oDoc.createInstance("com.sun.star.style.CharacterStyle")
		oStyle.setPropertyValue("CharHidden", True)
		oStyles.insertByName(cStyle, oStyle)
	end if
	rem UNO command to set Character Style
	document=oDoc.CurrentController.Frame
	dispatcher=createUnoService("com.sun.star.frame.DispatchHelper")
	dim args1(1) as new com.sun.star.beans.PropertyValue
	args1(0).Name="Template"
	args1(0).Value=cStyle
	args1(1).Name="Family"
	args1(1).Value=1
	dispatcher.executeDispatch(document,  ".uno:StyleApply",  "",  0,  args1() )
End Sub

Sub toggleHiddenStyle 'show/hide Character Style
	dim oDoc as object, oStyles as object, oStyle as object, b as boolean
	oDoc=ThisComponent
	oStyles=oDoc.StyleFamilies.getByName("CharacterStyles")
	if oStyles.hasByName(cStyle) then 'style exists
		oStyle=oStyles.getByName(cStyle)
		b=oStyle.CharHidden
		if b then 'change the properties to Visible
			oStyle.CharHidden=False
			oStyle.CharBackColor=RGB(255, 50, 250)
		else 'change the properties to Hidden
			oStyle.CharHidden=True
			oStyle.CharBackColor=-1
		end if	
	end if	
End Sub