G. Mayor has VBA code to do this
here. The animation should show what it does.
I'm going to post two versions of this, since ComObjActive() only works _sometimes_ for me. One version uses it, and it often works. But sometimes something goes haywire. For that reason the second version uses
@flyingDman 's version of Word_Get(). Chances are the first version will work for you.
Note: The cursor needs to be inside the fraction or immediately after the last digit. Something like 32/28.3 is NOT going to work.
ComObjActive() version:
Word_Get() version:
Spoiler Code: Select all
#Requires AutoHotkey v2.0
; ┌────────────────────────────────────────────┐
; │ Mangled from code at: │
; │ http://www.gmayor.com/createfraction.htm │
; └────────────────────────────────────────────┘
theMessage := "
(
Apparently Word is not running.
It must be running and visible for this script to work!
Instructions:
1) Start Word
2) Type (or select) a fraction
3) Position the cursor in the fraction or immediately after the final digit,
3) Hit the hotkey again.
)"
!^#f:: {
if !(WinExist("ahk_class OpusApp")) {
MsgBox(theMessage, "WORD NOT FOUND!", "T5")
return
}
else {
strList := "0123456789/"
newSlash := "⁄" ; Not a slash! This is Unicode #2044
wd := Word_Get()
;WinRestore("ahk_class OpusApp")
rng := wd.Selection.Range
rng.MoveStartWhile(strlist, -1073741823) ; -1073741823 is 'wdBackward'
rng.MoveEndWhile(strlist)
FoundPos := InStr(rng.Text, "/")
If FoundPos == 0 {
MsgBox("No fraction found!", "No fraction selected", "T10")
return
}
else {
fract := StrSplit(rng.Text, "/")
rng.Font.Superscript := true
rng.Text := fract[1]
rng.Collapse(0) ; wdCollapseEnd := 0
rng.Text := newSlash
rng.Font.Superscript := false
rng.Collapse(0)
rng.Text := fract[2]
rng.Font.Subscript := true
rng.Collapse(0)
rng.Font.Subscript := false
rng.Select()
wd.Selection.Font.Subscript := false
}
}
}
Word_Get(winTitle:="ahk_class OpusApp") {
if WinExist(winTitle)
ctlID := ControlGetHwnd("_WwG1")
else
ExitApp
numput('Int64',0x0000000000020400, 'Int64',0x46000000000000C0, IID_IDispatch:=buffer(16))
dllcall("oleacc\AccessibleObjectFromWindow", "ptr",ctlID, "uint",0xFFFFFFF0, "ptr",IID_IDispatch, "ptr*",win:=ComValue(9,0), 'HRESULT')
loop {
try
wrd := win.application
catch {
ControlSend("{escape}", "_WwG1", winTitle)
wrd := 0
}
} until !!wrd
return wrd
}
!Esc::ExitApp()
Hotkey is hard-coded here as Ctrl-Win-Alt-f, pick something that won't interfere with your Word shortcuts.
Bugs likely exist.
Regards,
burque505