PDA

View Full Version : [SOLVED:] Command to locate any attribute (bold, intalic, underline)



ma_roberge
10-20-2016, 01:36 PM
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

gmaxey
10-20-2016, 05:05 PM
Why don't you simply reapply the correct style to the text:

Selection.Style = "Footnote Reference"

ma_roberge
10-21-2016, 10:48 AM
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

gmaxey
10-21-2016, 01:17 PM
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

ma_roberge
10-23-2016, 07:51 PM
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

Paul_Hossler
10-24-2016, 12:09 PM
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

ma_roberge
10-24-2016, 01:51 PM
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).

Paul_Hossler
10-25-2016, 04:47 PM
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

ma_roberge
10-26-2016, 07:31 AM
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.