none
Find and replace with French accent characters RRS feed

  • Question

  • Using Word 2007.

    I have a macro to find and replace certain words that should contain French accent characters, with the correct characters.
    The words are in many places within the document and in different fonts, including PMingLiU.

    The find/replace works except all of the replacement accent characters (only, not entire word) are changed to Arial font, instead of retaining the original font.

    Here is the code with sample replacements:

    Const conFrenchChars As String = "Romanee,Romanée,Cuvee,cuvée"
    Dim arChars() As String
    Dim intIdx As Integer

       arChars = Split(conFrenchChars, ",")
       For intIdx = 0 To UBound(arChars) - 1 Step 2
          Selection.Find.ClearFormatting
          Selection.Find.Replacement.ClearFormatting
          With Selection.Find
              .Text = arChars(intIdx)
              .Replacement.Text = arChars(intIdx + 1)
              .Forward = True
              .Wrap = wdFindContinue
              .Format = False
              .MatchCase = False
              .MatchWholeWord = False
              .MatchWildcards = False
              .MatchSoundsLike = False
              .MatchAllWordForms = False
          End With
          Selection.Find.Execute Replace:=wdReplaceAll
       Next

    Can anyone tell me how to make the replacement and retain the original fonts?

    Thursday, March 17, 2011 12:35 AM

Answers

  • Thanks for the response Bruce. I wish MS would fix this forum. I did not receive the email notification that I should have when you posted.

    I am glad that you were able to duplicate the problem. What are the chances of Microsoft fixing it? About the same as me winning the Mega Millions lottery :)?

    Sorry, but your suggestion doesn't work. You use wdReplaceAll, which makes all of the replacements, so after the replace executes, there is no selection to restore font to. Additionally, as I stated in my original post, the words appear in several different fonts throughout the doc, so a single font replacement won't work.

    As I stated in my last post, I did find a workaround. Here is the simplified version, if anyone is interested.

    Dim arChars() As String
    Dim intIdx As Integer
    Dim strFontName As String
    Const conFrenchChars As String = "Romanee,Romanée,Cuvee,cuvée"

       arChars = Split(conFrenchChars, ",")
       For intIdx = 0 To UBound(arChars) - 1 Step 2
          Selection.Find.ClearFormatting
          Selection.Find.Replacement.ClearFormatting
          Selection.Start = 0
          Selection.Collapse Direction:=wdCollapseStart
          With Selection.Find
              .Text = arChars(intIdx)
              .Replacement.Text = arChars(intIdx + 1)
              .Forward = True
              .Format = False
              .MatchCase = False
              .MatchWholeWord = False
              .MatchWildcards = False
              .MatchSoundsLike = False
              .MatchAllWordForms = False
             .Wrap = wdFindStop
              Do
                .Execute
                If .Found Then
                  strFontName = Selection.Font.Name
                  Selection.Text = arChars(intIdx + 1)
                  Selection.Font.Name = strFontName
                  Selection.Collapse Direction:=wdCollapseEnd
                End If
             Loop While .Found
          End With
       Next

    I say simplified version because the doc is actually an rtf that was exported from Access. In some cases, the words do contain some French accent characters, but Word still changed those characters to Arial font. Font is correct in WordPad, so it is a Word issue.
    If the selection contains multiple fonts, then Selection.Font.Name returns an empty string.
    What I did was write another function that takes the Selection object as a parameter, steps through each character in the selection and returns the most prevalent font name.

    Maybe not too efficient, but works.

     

    • Marked as answer by Alphonse G Sunday, March 20, 2011 1:21 PM
    Friday, March 18, 2011 2:48 PM

All replies

  • Hi,
    I am not very clear about your problem.

    Do you mean after replacing the text, the Freench accent characters are changed to Arial font? What character do you want to replace and what is the original font?


    I think you may reapply the font style to the new replacement character.
    Hope this can help you.


    Regards,


    Be happy.

    • Edited by Mike_HelpYou Thursday, March 17, 2011 6:47 AM typo
    Thursday, March 17, 2011 6:47 AM
  • Thanks for the reply Mike.

    There are several words that should (but do not) contain the French accent characters. So, I am seacching for the word and replacing it with the same word, but containing the French accent character. E.G. Search for Romanee and replace with Romanée. I make sure to clear formatting in both find and replace. 

    Whether I use the code that I posted, or do a manual find/replace, the French accent characters are changed to Arial font.
    E.G. If Romanee is PMingLiU font, after it is replaced with Romanée, the characters Roman are still PMingLiU, but é is Arial and the last e is PMingLiU.

    There are several different fonts used throughout the doc, so I can't just specify a replacement font, which doesn't work either (did try it).

    Thursday, March 17, 2011 12:17 PM
  • I did develop a workaround.

    Instead of a global find/replace, I find each instance, check the font name of the found selection, then replace the selection with the correct word and set the font name of the selection back to the original font name.

    Not too efficient, but it works.

    Does anyone have a better solution?

    Thursday, March 17, 2011 4:57 PM
  • Hi Alphonse,

    Thank you for posting.

    I tested and reproduced the scenario with your code. I found it is a strange thing, if I copy the word and replace with the new word, the accent character will remain the original font. However, if I enter the word via keyboard, after replacing the new word, the accent character, the accent character can't keep the original format.

    As for this problem, I have a workaround which getting the origin font name and then reapply the previous font name to the new word, below is the code which I have modified:

    Sub ReplaceText()
    Const conFrenchChars As String = "Romanee,Romanée,Cuvee,cuvée"
    Dim arChars() As String
    Dim intIdx As Integer
    Dim fontName As String

       arChars = Split(conFrenchChars, ",")
       For intIdx = 0 To UBound(arChars) - 1 Step 2
          Selection.Find.ClearFormatting
          Selection.Find.Replacement.ClearFormatting
          With Selection.Find
              .Text = arChars(intIdx)
              .Replacement.Text = arChars(intIdx + 1)
              .Forward = True
              .Wrap = wdFindContinue
              .Format = False
              .MatchCase = False
              .MatchWholeWord = False
              .MatchWildcards = False
              .MatchSoundsLike = False
              .MatchAllWordForms = False
          End With
          ' get previous font name
          fontName = Selection.Font.Name
          Selection.Find.Execute Replace:=wdReplaceAll
         
          ' reapply previous font name
          Selection.Font.Name = fontName
       Next

    End Sub

    I hope this can help you and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 18, 2011 3:23 AM
  • Thanks for the response Bruce. I wish MS would fix this forum. I did not receive the email notification that I should have when you posted.

    I am glad that you were able to duplicate the problem. What are the chances of Microsoft fixing it? About the same as me winning the Mega Millions lottery :)?

    Sorry, but your suggestion doesn't work. You use wdReplaceAll, which makes all of the replacements, so after the replace executes, there is no selection to restore font to. Additionally, as I stated in my original post, the words appear in several different fonts throughout the doc, so a single font replacement won't work.

    As I stated in my last post, I did find a workaround. Here is the simplified version, if anyone is interested.

    Dim arChars() As String
    Dim intIdx As Integer
    Dim strFontName As String
    Const conFrenchChars As String = "Romanee,Romanée,Cuvee,cuvée"

       arChars = Split(conFrenchChars, ",")
       For intIdx = 0 To UBound(arChars) - 1 Step 2
          Selection.Find.ClearFormatting
          Selection.Find.Replacement.ClearFormatting
          Selection.Start = 0
          Selection.Collapse Direction:=wdCollapseStart
          With Selection.Find
              .Text = arChars(intIdx)
              .Replacement.Text = arChars(intIdx + 1)
              .Forward = True
              .Format = False
              .MatchCase = False
              .MatchWholeWord = False
              .MatchWildcards = False
              .MatchSoundsLike = False
              .MatchAllWordForms = False
             .Wrap = wdFindStop
              Do
                .Execute
                If .Found Then
                  strFontName = Selection.Font.Name
                  Selection.Text = arChars(intIdx + 1)
                  Selection.Font.Name = strFontName
                  Selection.Collapse Direction:=wdCollapseEnd
                End If
             Loop While .Found
          End With
       Next

    I say simplified version because the doc is actually an rtf that was exported from Access. In some cases, the words do contain some French accent characters, but Word still changed those characters to Arial font. Font is correct in WordPad, so it is a Word issue.
    If the selection contains multiple fonts, then Selection.Font.Name returns an empty string.
    What I did was write another function that takes the Selection object as a parameter, steps through each character in the selection and returns the most prevalent font name.

    Maybe not too efficient, but works.

     

    • Marked as answer by Alphonse G Sunday, March 20, 2011 1:21 PM
    Friday, March 18, 2011 2:48 PM
  • I don't know the solution, but I can not reproduce the problem (Word 2007 on Vista), so there must be one!

    It may be something in the document (especially if it's an RTF) or it may be something environmental. Do you see this problem with all fonts, or just PMingLiU, or what? Can you reproduce it on a clean document? What happens if you are typing in the font and enter é? Not sure what else to ask!

    Essentially, when you enter a character, Word checks the flags in the Font to see if it contains the subset that the character belongs to (e acute is in "Latin-1 Supplement", which is fairly common) and, if so, it should do nothing; if not, it uses some secret logic to decide on a font to use that does include the subset. If you don't have the font on your machine, I believe what happens depends on the substitute font used.

    I am not aware of any Word settings that could affect this, but different effects may be seen with different versions of the font that may exist on different versions of Windows.


    Enjoy,
    Tony
    www.WordArticles.com
    Saturday, March 19, 2011 9:38 AM
  • Thanks for the reply Tony,

    Sorry for not responding sooner, but apparently email notifications from this forum are not working AGAIN!

    The problem was discovered on Windows Server 2008 R2, but I am working in Windows XP SP3 where it is the same.

    I did try converting to a doc or docx and the problem is the same.

    Yes, if I type in the character manually, Word still changes it to Arial. This is within the PMingLiU. Does not happen within Times New Roman.

    It might have something to do with the way Word reads/converts an rtf, because it does not occur in a clean document. 

    None of this, however, is any help because I am stuck with the rtf generated by Access. It is a very complex layout (determined by live data) and would take a great deal of time to try to recreate using automation.

    After the rtf is output from Access I run a macro that changes some font sizes (not available in Access), cleans up formatting, converts to two column format, etc. and corrects those French accent characters.

    Sunday, March 20, 2011 1:20 PM