Hi,
I am in the process of creating a small Add-in to Word 2007 which is basically just a list of Greek symbols I frequently have to use in a CustomTaskPane.
I came up with the idea because every time I have to add a greek symbol I have to manually change the font, type the letter, then change it back again, which I thought could be automated pretty easily.
The original idea of my add-in is pretty much working completely:
I have a set of buttons, each displaying a different greek symbol. Once I click a button, I set the current Font to "Symbol" (which hosts the greek symbols). Then I use TypeText to type the button's Text property (which is the letter) to the document, and finally I change the Font back to "Times New Roman".
In code:
vb.net Code:
Private Function getWordApp() As Word.Application Dim wordApp As Word.Application = DirectCast(Microsoft.VisualBasic.Interaction.GetObject(, "Word.Application"), Word.Application) If wordApp IsNot Nothing Then Return wordApp Else Return Nothing End If End Function Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Z.Click, Y.Click, X.Click, W.Click, V.Click, U.Click, T.Click, S.Click, R.Click, Q.Click, P.Click, O.Click, N.Click, M.Click, L.Click, K.Click, J.Click, I.Click, H.Click, G.Click, F.Click, E.Click, D.Click, C.Click, B.Click, A.Click 'Type button text Dim appWord As Word.Application = getWordApp() If appWord Is Nothing Then Exit Sub Dim btn As Button = DirectCast(sender, Button) SetSymbol(appWord) appWord.Selection.TypeText(btn.Text) SetTimes(appWord) 'Return focus to document (not working!!) appWord.Application.ActiveDocument.Activate() End Sub Private Sub SetSymbol(ByVal appWord As Word.Application) 'Changes font to Symbol If appWord Is Nothing Then Exit Sub appWord.Selection.Font.Name = "Symbol" appWord.Selection.Font.Italic = True 'If CheckBox1.Checked THen appWord.Selection.TypeText(" ") End Sub Private Sub SetTimes(ByVal appWord As Word.Application) 'Changes font to Times If appWord Is Nothing Then Exit Sub appWord.Selection.Font.Name = "Times New Roman" appWord.Selection.Font.Italic = False If CheckBox1.Checked = True Then appWord.Selection.TypeText(" ") End Sub
Now, everything works just fine except for returning focus to the document so I can immediately start typing after inserting the greek symbol.
I have tried various combinations but none are working...:
All just leave the focus on the button I clicked, and I have to click into the document to be able to start typing again...Code:appWord.Application.ActiveDocument.Activate() appWord.Activate() appWord.Application.Activate()
Any help would be much appreciated!




Reply With Quote