none
Strange Selection.Find.Font Issue (Word 2003) RRS feed

  • Question

  • Hello

    I have encountered an odd situation (very rare), and wanted to elicit thoughts on it.  It is not a major issue, and I just wanted to raise it for fun.

    Some background: I have written a program (an HTA file running JavaScript which uses ActiveXObjects to automate Word), that converts Word documents faithfully to HTML.   I am doing this as it gives me more control over the HTML output than Word's own export option, and  I have got it running almost pixel perfect, with a better output in some cases than Word's own.

    When converting an outline or bulleted list, I first use the ConvertNumbersToText function (because I later need to analyse the tab distances between the bullet and the text).  The use of ConvertNumbersToText is important to the problem.

    I then use Range.Find to isolate sequences of Characters with the same Font.Name, which I can then convert to HTML using "<span>" tags. It is much quicker than iterating the Characters collection.

    To use Range.Find, I start with a collapsed Range at the start of the Paragraph. I then query with Range.Font.Name the font of the next character. I then use Range.Find (setting .Font.Name with the queried font name) to expand the collapsed range to cover the sequence of neighbouring Characters.  I repeat this process for all Font.Name(s) used in the Paragraph.

    All well and good.

    Then, in one particular case, my automatic bullet used 25CD (Unicode hollow circle) in Courier New font.  (Courier New also seemed to be part of the problem.)

    Range.Font.Name duly reported "Courier New" for the bullet.

    BUT, Range.Find.Font.Name = "Courier New" failed to find the bullet!!!

    I compared this with the Find and Replace Dialog box, which duly found the bullet.

    I then found that if I selected the bullet, and mannually re-applied "Courier New", Range.Find worked.

    So clearly, ConvertNumbersToText was using some formatting structure (hidden Char style?) which hid the "Courier New" setting from Range.Find, but not Range.Font.Name or the dialog box.

    What on earth?

    So I then experimented with Selection.Find instead, and noted something:-

    -  If I choose the font name in the Find and Replace Dialog box, the Dialog box reports "Font: Courier New".

    -  However, if I use Selection.Find.Font.Name = "Courier New", the Dialog box reports "Font: (Default) Courier New".

    The words "(Default)" appear, which seem to mean that Selection.Find.Font.Name (and therefor Range.Find.Font.Name) is doing something odd, and this "(Default)" combined with whatever ConvertNumbersToText is doing, creates the problem. 

    The upshot is that is means that Find could not see the 25CB character (following ConvertNumbersToText, not if you use insert Symbol).

    I worked round it, by iterating the Characters in this exceptional case.

    But has anyone else noted this "(Default)" issue when using Selection.Find.Font.Name?

    Julian

     

    Thursday, November 10, 2011 4:12 PM

Answers

  • Hi Julian

    Although you're doing this with code and it would seem logical to post on MSDN, the better place for a discussion on this would probably be on "Answers":

    http://answers.microsoft.com/en-us/office/forum/word

    The reason is that most of the "font specialists" will more likely be found there, than here, and I think those are the folks that may be able to track this down.

    FWIW, sometime back in the Office 2000-2003 days right-to-left languages were integrated into Word. At that point, we began to notice oddities in Find-Replace, most of which were fixed, eventually. This may be a remnant... What happened was that, in order to accomodate RTL font formatting definitions suddenly included a "BIDI" font format along with the usual font "name". The BIDI was for Asian/RTL. We could see this by saving the documents to RTF or HTML :-) The problem was that we couldn't specify the BIDI in the Find/Replace UI, only the name. So the fonts were never found (unless part of a style).

    It's not said that what you're seeing is related. But your problem reminds me of those times...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Julian Turner Friday, November 11, 2011 8:58 AM
    Friday, November 11, 2011 7:31 AM
    Moderator
  • Dear Cindy

    Will do.  

    In terms of coding solutions, I can see the BIDI conflict now programatically using Font.Name and Font.NameBi (thanks to your lesson), which report "Courier New" and "Arial" respectively, so can now anticipate this in the future.

    Regards

    Julian

    Friday, November 11, 2011 5:23 PM

All replies

  • Hi Julian

    Although you're doing this with code and it would seem logical to post on MSDN, the better place for a discussion on this would probably be on "Answers":

    http://answers.microsoft.com/en-us/office/forum/word

    The reason is that most of the "font specialists" will more likely be found there, than here, and I think those are the folks that may be able to track this down.

    FWIW, sometime back in the Office 2000-2003 days right-to-left languages were integrated into Word. At that point, we began to notice oddities in Find-Replace, most of which were fixed, eventually. This may be a remnant... What happened was that, in order to accomodate RTL font formatting definitions suddenly included a "BIDI" font format along with the usual font "name". The BIDI was for Asian/RTL. We could see this by saving the documents to RTF or HTML :-) The problem was that we couldn't specify the BIDI in the Find/Replace UI, only the name. So the fonts were never found (unless part of a style).

    It's not said that what you're seeing is related. But your problem reminds me of those times...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Julian Turner Friday, November 11, 2011 8:58 AM
    Friday, November 11, 2011 7:31 AM
    Moderator
  • Dear Cindy

    Thank you for your message. 

    You have solved it!

    I have subsequently done some additional testing.  I have a set of styles I use for my documents.  When I create a fresh document with those styles, the problem does not manifest itself.  But the document (rather the bullet paragraphs) which originally manifested the problem still manifested it, even when copied to my fresh document!  I.e. in my fresh document I ended up with exactly the same styled paragraphs, with the copied paragraphs from my problem document only showing the problem!

    So I saved to HTML, and this is what I got.

    For my fresh paragraphs, the bullet was:-

    <span style='mso-bidi-font-size:10.0pt;line-height:120%;font-family:"Courier New"'>&#9675;<spanstyle='mso-tab-count:1'>  </span></span>

    For my problem paragraphs, the bullet was:-

    <span style='font-family:"Courier New";mso-bidi-font-family:Arial'>&#9675;<span tyle='mso-tab-count:1'>  </span></span>

    I have absolutely no idea what I did (and most problems must start with me the user) that introduced the conflicting "mso-bidi-font-family:Arial' font!

    Brilliant.  Thank you again.

    Julian 

    Friday, November 11, 2011 8:58 AM
  • Hi Julian

    <<You have solved it!>>

    I'm astonished that this turned out to be the issue, but glad I was able to help with my ruminations about the "bad old days" :-)


    Cindy Meister, VSTO/Word MVP
    Friday, November 11, 2011 9:25 AM
    Moderator
  • Hi Cindy

    Nothing wrong with keeping the past in mind, given the future is built on it!  Reminds me of Raymond Chen's blog title "The Old New Thing".

    I am still intrigued as to why, if I choose the font name in the Find and Replace Dialog box, the Dialog box reports "Font: Courier New", but if I use Selection.Find.Font.Name = "Courier New", the Dialog box reports "Font: (Default) Courier New" (and the font picker dialog box shows an empty name field).  But I don't think it makes any difference in 99.999999% of cases.

    Best wishes

    Julian

     

    Friday, November 11, 2011 11:56 AM
  • Hi Julian

    << am still intrigued as to why, if I choose the font name in the Find and Replace Dialog box, the Dialog box reports "Font: Courier New", but if I use Selection.Find.Font.Name = "Courier New", the Dialog box reports "Font: (Default) Courier New" (and the font picker dialog box shows an empty name field).  But I don't think it makes any difference in 99.999999% of cases.>>

    I haven't a clue :-) But you might try posing the question as a discussion point in the Answers forum. If nothing else, it may get a number of people scratching their heads!


    Cindy Meister, VSTO/Word MVP
    Friday, November 11, 2011 3:53 PM
    Moderator
  • Dear Cindy

    Will do.  

    In terms of coding solutions, I can see the BIDI conflict now programatically using Font.Name and Font.NameBi (thanks to your lesson), which report "Courier New" and "Arial" respectively, so can now anticipate this in the future.

    Regards

    Julian

    Friday, November 11, 2011 5:23 PM
  • Hi Julian

    <<In terms of coding solutions, I can see the BIDI conflict now programatically using Font.Name and Font.NameBi (thanks to your lesson), which report "Courier New" and "Arial" respectively, so can now anticipate this in the future.>>

    Thanks for adding that bit of information to this thread :-) I'm sure it will help others, some time in the future.

    And it's always fun for me to learn something new about Word!


    Cindy Meister, VSTO/Word MVP
    Saturday, November 12, 2011 5:19 PM
    Moderator
  • As it helped me, thanks. I was experiencing some strange behaviour in a procedure that stores all used fontnames into an array. Adding the NameBi property into the loop solved the problem. An example to illustrate the solution:

    (...)
    Do While Selection.Start + 1 < ActiveDocument.Characters.Count
        FontName = Selection.Font.Name
        FontNameBi = Selection.Font.NameBi
        FoundFont = False
        For i = 0 To UBound(FontList)
            If FontList(i) = FontName Then FoundFont = True
        Next i
        If Not FoundFont Then
            FontCount = FontCount + 1
            ReDim Preserve FontList(FontCount - 1)
            FontList(FontCount - 1) = FontName
        End If
        Selection.Find.ClearFormatting
        Selection.Find.Format = True
        Selection.Find.Font.Name = FontName
        Selection.Find.Font.NameBi = FontNameBi
        Selection.Find.Execute Replace:=wdReplaceAll
        Selection.MoveRight wdCharacter, 1
        Selection.MoveLeft wdCharacter, 1
    Loop
    (...)

     

    Wednesday, November 30, 2011 1:34 PM