PDA

View Full Version : Solved: Can't Apply Font Attribute



gmaxey
01-25-2013, 01:13 PM
After sitting here surrounded by clumps of hair and bits of bloody scalp, I've thrown in the towel :dunno

I have a template add-in that you can download here:

https://dl.dropbox.com/u/64545773/Test%20TO.dotm (https://dl.dropbox.com/u/64545773/Test%20TO.dotm)

After opening this template, insert and test the Checked Box and Active Button objects. Both work as expected.

Now close the template, restart Word and load the template as an add-in.

After the template loads, insert and test the Checked Box and Active Button objects in a new document. Note that(for me at least), the Checked Box works fine, but the Active Button object displays the wrong characters after the initial toggle!!

The problem is that for some reason, I am unable to apply the “wingdings” font to the symbol programmatically.

After the procedure runs, I can select the field and apply “Wingdings” font with the UI and the correct symbol displays or I can toggle the field code and apply the wingding font with the UI only to the symbol and after toggling the field code again, the correct symbol displays.

The only fix I’ve found, is to abandon what seems to work perfectly well with the template open and what works perfectly well with the Checked Box in either case and insert a building block of the correct symbol. You can apply the fix by closing and restarting Word, opening the template and unstetting the following lines:

oAutoTextButtonOff.Insert Where:=Selection.Range, _ RichText:=True ' and




oAutoTextButtonOn.Insert Where:=Selection.Range,RichText:=True

… then deleting the msgbox code lines.

Close, restart Word, and reload the template.

I've done everything that I can think of but nothing I do will apply the wingding font to the symbol programatically the way it works when the template is opened as a file and with the Checked and Unchecked box symbols. I would be interested in an explanation of this weird behavior.

Thanks!!

Paul_Hossler
01-25-2013, 02:26 PM
Seems to work in Vista and Word 2010, both as a template and as dotm in my Word Startup folder

1. I saved your dotm, opened it to create a document, inserted some checkboxes and radio button. Double clicking the symbols caused the filled CB to open, the unfilled CB to 'check', etc.

2. I put your dotm into my Word Startup, opened Word, created a new document, inserted some checkboxes and radio button. Double clicking the symbols caused the filled CB to open, the unfilled CB to 'check', etc. Picture is as an add in


Let me know if there's something else you'd like me to try

Paul

gmaxey
01-25-2013, 02:40 PM
Paul,

Not the case here. I don't know when you downloaded the file, but when first posting, I forgot to stet out the lines that were inserting the building blocks.

When you started Word after putting the template in the startup folder, did you see the two msgbox notifications about problems with applying the font? If so, then you are seeing the issue and have the version where I forgot to stet out the work around code.

Paul_Hossler
01-25-2013, 02:59 PM
I think I commented and uncommented correctly.

Still seems to work as template and as a Startup


Sub CheckIt()
arrStates = Split("Checked|Unchecked", "|")
ToggleIt "", 111, 254
lbl_Exit:
Exit Sub
End Sub
Sub RadioBtn()
Dim o_Fld As Word.Field
Set o_Fld = Selection.Fields(1)
arrStates = Split("On|Off", "|")
ToggleIt "", 161, 164
If Not o_Fld.Code.Characters(26).Font.Name = "Wingdings" Then
If AscW(o_Fld.Code.Characters(26)) = 161 Then
' MsgBox "Since I couldn't apply the font attribute, the symbol displayed is wrong!!"
oAutoTextButtonOff.Insert Where:=Selection.Range, RichText:=True
Else
' MsgBox "Since I couldn't apply the font attribute, the symbol displayed is wrong!!"
oAutoTextButtonOn.Insert Where:=Selection.Range, RichText:=True
End If
End If
lbl_Exit:
Exit Sub
End Sub


Paul

gmaxey
01-25-2013, 03:17 PM
Well, I'm totally confounded. Here the checkbox works perfectly, but the active button object simply will not work in a new document when the template is loaded either manually or when loaded through Word's startup folder.

Windows 7, Office 2010.

Thanks for having a look and reporting your results. I suppose I'll just have to live with the workaround here locally, as I've exhausted every idea.

Frosty
01-25-2013, 04:03 PM
Works for me as well, Greg. Couple other things to try...
Start > Run > Winword /a
This gives you a clean winword process with no other addins.
In this "no addins" winword process.. go to the developer tab, and load your addin.

See if it works (it does for me in my regular word as well as a "clean" word).

Frosty
01-25-2013, 04:08 PM
Ack... wait... I think I was doing it wrong. After inserting the buttons via the ribbon interface, you're supposed to click on the actual macrobutton fields, right?

Okay-- that fails for me as well...

gmaxey
01-25-2013, 04:43 PM
Jason,

Right. The macrobutton field inserts correctly. It is only after toggling the Active Button (or Inactive button) symbol in the document that the problem occurs.

It really has me baffled. For some reason the "wingdings" font attribute won't apply to the character programatically. After the procedure runs, you can toggle the field code and select the symbol character and apply "wingding" font with the UI and after toggling field code back off it will display correctly. I've tried everything I can think of and I just can't get it applied programatically. Really weird as characters 111 and 254 work just fine. Thanks.

gmaxey
01-25-2013, 05:01 PM
It should come as no surprise that I don't like getting my butt kicked by code :-(

Experimenting further, if I change the symbol pairs:

44/45 mailbox flag down/mail box flag up
231/232 left arrow/right arrow
67/68 thumb up/thumb down
162/165 active button/inactive button (just a different style)

all work as expect

using 161/165 161 fails/165 no problem
using 162/164 162 no problem/164 fails

Now I'm baffled beyond description!

gmaxey
01-26-2013, 08:34 AM
Well I still can't explain the behaviour seen with the symbol pair 161/164. However, I have discovered with the help of a friend that if I use the unicode values for the symbol pair then it will work. So if I replace each instance of 161 and 164 with -3932 and -3935 in my code then all works as expected. Thanks for all the interest and participation.

Sub RadioBtn()
Dim o_Fld As Word.Field
Set o_Fld = Selection.Fields(1)
arrStates = Split("On|Off", "|")
ToggleIt "", -3932, -3935
lbl_Exit:
Exit Sub

To get the Unicode value of a symbol, you can insert it in the document, select it, and run this code:

Sub GetSymbolUnicodeValue()
Dim SelFont As Variant
Dim SelCharNum As Long
Dim sCode As String
Select Case Len(Selection.Range)
Case Is = 0
MsgBox "Nothing selected!"
Exit Sub
Case Is = 1
With Selection
With Dialogs(wdDialogInsertSymbol)
SelFont = .Font
SelCharNum = .CharNum
End With
End With
MsgBox SelFont & " " & SelCharNum
Case Else
MsgBox "Select only the character to evaluate, and run the macro again"
Exit Sub
End Select
End Sub
End Sub

Paul_Hossler
01-26-2013, 08:58 AM
Thanks for posting the solution -- I've had some problems in the InsertSymbol area that I'll see if your solution will solve.

Paul