Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OpenOffice Macro Creation for formatting

Status
Not open for further replies.

talhamoin

IS-IT--Management
Mar 3, 2011
14
AE
I want to create a macro in OO writer. I want it to format text as per the tags I assign while editing the file. For example: A quick [bold]brown[/bold] fox jumps.

The macro should go through the document and format all the text that's within these tags and format them as bold.

Any hints or help?
 
Hi

Why a macro for that ? A simple Edit | Find & Replace is enough :

Search for : [highlight]\[bold\](.*?)\[/bold\][/highlight]
Replace with : [highlight]$1[/highlight]
Format... ( while Replace with is focused ) | Font Typeface : [highlight]Bold[/highlight]

Tested with LibreOffice, hopefully will work in OpenOffice.org too.


Feherke.
 
Actually, I will define complete styles in macro.

Like I will use tag [myStyle] [/myStyle].

myStyle will have font face, font size, weight, color, etc.

That's why I need a macro.
 
Hi

I suppose you have more than one such markup tag to replace, so here is a generic solution :
[ul]
[li]create the styles at Format | Styyles and formatting | Character styles[/li]
[li]in the tag2style [tt]sub[/tt] set up tagstyle array with pairs of markup tag text and style name ( note that tags are specified without the brackets ( [] ) and start and end tags have identical text, but this can be changed easily )[/li]
[li]run the tag2style [tt]sub[/tt][/li]
[/ul]
Code:
[b]sub[/b] tag2style
  [b]dim[/b] [COLOR=darkgoldenrod]tagstyle[/color][teal]()[/teal]
  [b]dim[/b] search[teal],[/teal] found
  
  tagstyle[teal]=[/teal][COLOR=darkgoldenrod]array[/color][teal]([/teal] _
    [COLOR=darkgoldenrod]array[/color][teal]([/teal][green][i]"bold"[/i][/green][teal],[/teal][green][i]"boldtext"[/i][/green][teal]),[/teal] _
    [COLOR=darkgoldenrod]array[/color][teal]([/teal][green][i]"italic"[/i][/green][teal],[/teal][green][i]"italictext"[/i][/green][teal])[/teal] _
  [teal])[/teal]
  
  search[teal]=[/teal]ThisComponent[teal].[/teal]CreateSearchDescriptor
  search[teal].[/teal]SearchRegularExpression[teal]=[/teal][b]True[/b]

  [b]for[/b] i[teal]=[/teal][COLOR=darkgoldenrod]LBound[/color][teal]([/teal]tagstyle[teal])[/teal] to [COLOR=darkgoldenrod]UBound[/color][teal]([/teal]tagstyle[teal])[/teal]
    tag[teal]=[/teal][COLOR=darkgoldenrod]tagstyle[/color][teal]([/teal]i[teal])([/teal][purple]0[/purple][teal])[/teal]
    search[teal].[/teal]SearchString[teal]=[/teal][green][i]"[/i][/green][lime][i]\[[/i][/lime][green][i]"[/i][/green][teal]+[/teal]tag[teal]+[/teal][green][i]"[/i][/green][lime][i]\][/i][/lime][green][i].*?[/i][/green][lime][i]\[[/i][/lime][green][i]/"[/i][/green][teal]+[/teal]tag[teal]+[/teal][green][i]"[/i][/green][lime][i]\][/i][/lime][green][i]"[/i][/green]
  
    found[teal]=[/teal]ThisComponent[teal].[/teal][COLOR=darkgoldenrod]FindFirst[/color][teal]([/teal]search[teal])[/teal]
    [b]do[/b] [b]while[/b] [b]not[/b] [COLOR=darkgoldenrod]IsNull[/color][teal]([/teal]found[teal])[/teal]
      found[teal].[/teal]String[teal]=[/teal][COLOR=darkgoldenrod]Mid[/color][teal]([/teal]found[teal].[/teal]String[teal],[/teal][b]Len[/b][teal]([/teal]tag[teal])+[/teal][purple]3[/purple][teal],[/teal][b]Len[/b][teal]([/teal]found[teal].[/teal]String[teal])-[/teal][b]Len[/b][teal]([/teal]tag[teal])*[/teal][purple]2[/purple][teal]-[/teal][purple]5[/purple][teal])[/teal]
      found[teal].[/teal]CharStyleName[teal]=[/teal][COLOR=darkgoldenrod]tagstyle[/color][teal]([/teal]i[teal])([/teal][purple]1[/purple][teal])[/teal]

      found[teal]=[/teal]ThisComponent[teal].[/teal][COLOR=darkgoldenrod]FindNext[/color][teal]([/teal]found[teal].[/teal]End[teal],[/teal]search[teal])[/teal]
    [b]loop[/b]

  [b]next[/b]
  
[b]end[/b] [b]sub[/b]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top