OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Can anyone program a macro to insert a special character?

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Impress
View previous topic :: View next topic  
Author Message
aharel
Newbie
Newbie


Joined: 12 Aug 2005
Posts: 2

PostPosted: Fri Aug 12, 2005 12:56 pm    Post subject: Can anyone program a macro to insert a special character? Reply with quote

Hi,
Does anyone know how to program a macro so it inserts a special character into the
currently edited text box?
Macros recorded in write don't work (presumably because they don't find the text box).

Thanks,
A.
Back to top
View user's profile Send private message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Sat Aug 13, 2005 8:27 am    Post subject: Reply with quote

... sounded like a simple task, but turned out to be quite difficult. Try the below code, and let me know if it fails ...

Good luck,

ms777


Code:
Public publAscii as Long

sub main

oDoc   = ThisComponent
lAscii =     CLng("&H263A")
call InsertSpecialCharacter(oDoc, lAscii)

end sub


sub InsertSpecialCharacter(oDoc as Object, lAscii as Long)

oSel = oDoc.CurrentSelection
If IsNull(oSel) then
  msgbox "nothing selected"
  exit sub
  endif
if oSel.supportsService("com.sun.star.text.TextCursor") then
  oSelShape = oSel.Text
  else
  oSelShape = oSel.getByIndex(0)
  endif
' at this point. oSelShape should always contain a TextShape

'we need setString and getString...
if not HasUnoInterfaces(oSelShape, "com.sun.star.text.XTextRange") then
  msgbox "not expected error 1"
  exit sub
  endif

oFrame     = oDoc.CurrentController.Frame
publAscii  = lAscii

' This is for the case that the text inside the selected shape is currently being edited
sStringOld = oSelShape.String
oToolkit   = oFrame.ContainerWindow.Toolkit
oList      = createUnoListener("TopW_", "com.sun.star.awt.XTopWindowListener")
oToolkit.addTopWindowListener(oList)
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(oFrame, ".uno:Bullet", "", 0, Array())
oToolkit.removeTopWindowListener(oList)
sStringNew = oSelShape.String

' This is for the case that the text inside the selected shape is currently _not_ being edited
if (sStringOld=sStringNew) then 'the dispatch did not change anything
  oSelShape.String = oSelShape.String+Chr(publAscii)
  endif
end sub

'the TopWindowListener is needed to get access to the SpecialCharacters Dialog

sub TopW_windowOpened(e as Object)
call ExecuteSpecialCharactersAccessibleContext(e.source.AccessibleContext, publAscii)
end sub

sub TopW_windowClosing(e as Object)
end sub

sub TopW_windowClosed(e as Object)
end sub

sub TopW_windowMinimized(e as Object)
end sub

sub TopW_windowNormalized(e as Object)
end sub

sub TopW_windowActivated(e as Object)
end sub

sub TopW_windowDeactivated(e as Object)
end sub


'This function handles the special Characters Dialog
sub ExecuteSpecialCharactersAccessibleContext(oAC as Object, lAscii as Long)

oAccessibleTable = oAC.getAccessibleChild(0).getAccessibleChild(1) 'com.sun.star.accessibility.XAccessibleTable

'search in the AsccessibleTable for the nearest Character
lLow = 0
lUp  = oAccessibleTable.AccessibleChildCount-1
lMidOld = -1
While lUp > lLow
  lMid = Int(0.5*(lUp+lLow))
  if (lMidOld <> lMid) then
    sDescr = oAccessibleTable.getAccessibleChild(lMid).AccessibleDescription
    lNum = CLng("&H" + Mid(sDescr, InStr(sDescr, "0x")+2, 255))
    if lNum>=lAscii then lUp = lMid
    if lNum<=lAscii then lLow = lMid
    lMidOld = lMid
  else
    lUp=lMid
    lLow=lMid
    endif
  wend
oAccessibleTable.selectAccessibleChild(lMid)

'press OK Button
oOKButton = oAC.getAccessibleChild(2).AccessibleContext
oOKButton.doAccessibleAction(0)
end sub
Back to top
View user's profile Send private message
aharel
Newbie
Newbie


Joined: 12 Aug 2005
Posts: 2

PostPosted: Mon Aug 15, 2005 9:50 am    Post subject: Reply with quote

Thanks ms777,

It works!

Just a stupid question: how do I find the correct code for the character I want (let's say, a greek lower-case phi symbol)? It looks a bit like unicode, but 263A in Unicode is a smiley, not a diamond. I also tried the string that appear in the popup box when I insert symbol manually, but they didn't work.

Thanks,
Amnon
Back to top
View user's profile Send private message
ms777
Super User
Super User


Joined: 07 Feb 2004
Posts: 1355

PostPosted: Mon Aug 15, 2005 2:23 pm    Post subject: Reply with quote

Hi Amnon,

currently, the font is not being changed by the macro. So if the last letter before the inserted one is in Arial, the inserted one will also be Arial.

On my system (W98SE), the original macro with &H263A gives a smiley in Arial and TimesNewRoman. For the lower letter phi, change &H263A to &H03C6 (works on both charsets).

To find out the code for a letter, do the Insert Special Character manually, browse for the letter, and write down the text in the lower right corner (U+....)

Good luck,

ms777
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Impress All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group