Consulting

Results 1 to 9 of 9

Thread: Command to locate any attribute (bold, intalic, underline)

  1. #1

    Command to locate any attribute (bold, intalic, underline)

    I have the following bit of code that, after locating a footnote call number, turns off a font attribute to make sure that no number has inherited the attribute of the text preceding it. It does the job, but I am wondering if there are commands that will encompass any font attribute (and effects such as strikethrough) and turns them off, whatever they are. It seems that Selection.ClearCharacterAllFormatting does too much since the superscript used for call numbers would disappear as well. Thanks in advance.

    Do While Selection.Find.Execute = True
            If Selection.Font.Bold Or Selection.Font.Italic Or Selection.Font.Underline Then
                Selection.Font.Bold = False
                Selection.Font.Italic = False
                Selection.Font.Underline = False
            End If
    Loop

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Why don't you simply reapply the correct style to the text:

    Selection.Style = "Footnote Reference"
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    Greg,

    I don't know if I did something wrong when implementing your suggestion, but I get error 5834 (Item with specified name does not exist) when running the macro. Thanks in advance for any help.

    Sub RemoveFormattingFromNoteNumbers()
    '
    ' Delete attributes from all note call numbers
    '
        Selection.HomeKey Unit:=wdStory
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "^f"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchWildcards = False
        End With
        Do While Selection.Find.Execute = True
            If Selection.Font.Bold Or Selection.Font.Italic Or Selection.Font.Underline Then
    '            Selection.Font.Bold = False
    '            Selection.Font.Italic = False
    '            Selection.Font.Underline = False
                Selection.Style = "Footnote Reference"
            End If
    '        If Selection.Style <> "Normal" Then Selection.Style = "Footnote Reference"
        Loop
        Selection.Collapse Direction:=wdCollapseEnd
    End Sub

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    When a footnote is created Word applies a style to the footnote reference. Here (US English) that style name is "Footnote Reference." That error only occurs here if there is not such style named "Footnote Reference" in the document. You may need to change "Footnote Reference" to whatever style name your version of Word uses when creating footnotes.

    Sub RemoveFormattingFromNoteNumbers()
        Selection.HomeKey Unit:=wdStory
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "^f"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchWildcards = False
          Do While .Execute = True
            Selection.Style = "Footnote Reference"
            Selection.Collapse Direction:=wdCollapseEnd
          Loop
       End With
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    Greg,

    Your code does the job, but, as you imply, it is language specific. In my case (French), one has to use "Appel note de bas de p." for footnotes and "Appel de note de fin" for endnotes. For my macro to work irrespective of the installed language, one would have to check for the language and provide the applicable commands; this would clearly be going overboard. I conclude that the code I proposed in my initial post finally provides the most workable solution even though one could wish for something more programmatically idiomatic instead of using three separate commands. Thanks for all your input, which is much appreciated.

    I have now wrapped my code inside a For Each loop to process both footnotes and endnotes, as follows:
    Sub RemoveFormattingFromNoteNumbers()
    '
    ' Delete attributes from all note call numbers
    '
        Dim varNoteTypes As Variant
        Dim varNoteType As Variant
        varNoteTypes = Array("^f", "^e")
        For Each varNoteType In varNoteTypes
            Selection.HomeKey Unit:=wdStory
            Selection.Find.ClearFormatting
            Selection.Find.Replacement.ClearFormatting
            With Selection.Find
                .Text = varNoteType
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchWildcards = False
            End With
            Do While Selection.Find.Execute = True
                If Selection.Font.Bold Or Selection.Font.Italic Or Selection.Font.Underline Then
                    Selection.Font.Bold = False
                    Selection.Font.Italic = False
                    Selection.Font.Underline = False
                End If
            Loop
            Selection.Collapse Direction:=wdCollapseEnd
            Selection.Style = "Normal"
        Next
    End Sub

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    It should be easy enough to find the current language and set the styles accordingly

    Maybe something this

    Option Explicit
    
    Private Declare Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" (ByVal Locale As Long, _
        ByVal LCType As Long, _
            ByVal lpLCData As String, _
                ByVal cchData As Long) As Long
    Public Const LOCALE_USER_DEFAULT As Long = &H400
    Public Const LOCALE_SABBREVLANGNAME As Long = &H3
     
    Sub RemoveFormattingFromNoteNumbers()
         '
         ' Delete attributes from all note call numbers
         '
        Dim varNoteTypes As Variant
        Dim varNoteType As Variant
        
        Dim sLang As String, sFootnoteStyle As String, sEndnoteStyle As String
        
        
        sLang = pvtGetInfo(LOCALE_SABBREVLANGNAME)
        MsgBox sLang
    
        Select Case sLang
            Case "ENU"
                sFootnoteStyle = "Footnote Reference"
                sFootnoteStyle = "Footnote Reference"
            Case "FR"       '????? note sure
                sFootnoteStyle = "Appel note de bas de p."
                sFootnoteStyle = "Appel de note de fin"
        End Select
        
        Selection.HomeKey Unit:=wdStory
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "^f"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchWildcards = False
            Do While .Execute = True
                Selection.Style = sFootnoteStyle
                Selection.Collapse Direction:=wdCollapseEnd
            Loop
        End With
        Selection.HomeKey Unit:=wdStory
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = "^e"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchWildcards = False
            Do While .Execute = True
                Selection.Style = sEndnoteStyle
                Selection.Collapse Direction:=wdCollapseEnd
            Loop
        End With
    End Sub
    
     Function pvtGetInfo(ByVal lInfo As Long) As String
        Dim Buffer As String
        Dim ret As String
        Buffer = String$(256, 0)
        ret = GetLocaleInfo(LOCALE_USER_DEFAULT, lInfo, Buffer, Len(Buffer))
        If ret > 0 Then
            pvtGetInfo = Left$(Buffer, ret - 1)
        Else
            pvtGetInfo = vbNullString
        End If
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    Paul,

    I tried your code, which works if you change the second Case statement from FR to FRC. The problem remains that, if the macro is to be universal, one would need Case statements for all languages and the exact names of the styles used for notes. And I don't know if there are codes for each variety of a language: Canada, Belgium, France, Switzerland, etc. The three-letter codes ENU and FRC suggest so (English, United [States], French Canada). That would make a lot of possibilities to cover.

    On the other hand, it seems that one simple command will return 1036 for French, which should be enough to use in a Case statement with the appropriate style strings.
    Application.LanguageSettings.LanguageID(msoLanguageIDUI)
    Thanks for showing me another way of getting to the same place (with much more advanced code).

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I didn't know about Application.LanguageSettings.LanguageID(msoLanguageIDUI)

    GetLocaleInfo() can return a lot of other locale-specific settings


    Maybe you could just use the enumerations for the builtin styles instead of the name. That would make it (probably) independent of language


    Option Explicit
    
    Sub demo()
        Selection.Style = ActiveDocument.Styles(wdStyleFootnoteReference)
    
        MsgBox ActiveDocument.Styles(wdStyleFootnoteReference).NameLocal
    End Sub

    Making Greg's something like this


    Sub RemoveFormattingFromNoteNumbers() 
        Selection.HomeKey Unit:=wdStory 
        Selection.Find.ClearFormatting 
        Selection.Find.Replacement.ClearFormatting 
        With Selection.Find 
            .Text = "^f" 
            .Replacement.Text = "" 
            .Forward = True 
            .Wrap = wdFindContinue 
            .Format = False 
            .MatchWildcards = False 
            Do While .Execute = True 
                Selection.Style = ActiveDocument.Styles(wdStyleFootnoteReference)
                Selection.Collapse Direction:=wdCollapseEnd 
            Loop 
        End With 
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    Paul,

    Your command works very well, and fast. Unless something is wrong in my tests, the command also resets to normal the appearance of endnote call numbers, as much as one would assume that Selection.Style = ActiveDocument.Styles(wdStyleEndnoteReference) would be needed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •