![]() |
|
#1
|
|||
|
|||
|
This simple macro checks the font size in each paragraph of the doc. But this is not what I want. I need to check if some paragraph contains a string with a different font size from the rest of the paragraph. Can someone help? Thanks!
Example: Macros are not always easy to devise. Code:
Sub FontSize()
Dim aRng As Range, aPara As Paragraph
Set aRng = ActiveDocument.Range
aRng.End = ActiveDocument.Range.End
For Each aPara In aRng.Paragraphs
If aPara.Range.Font.Size <> 12 Then
aPara.Range.Select
MsgBox "Check font size"
End If
aRng.Collapse wdCollapseEnd
Next aPara
End Sub
|
|
#2
|
||||
|
||||
|
RobiNew
You have been asking a lot of questions on this site with a strong flavour of bad formatting practices. Can I ask why you go to so much effort to avoid standardizing the document through applying styles and removing local formatting wherever possible? In the case of this particular question, why haven't you used vba to examine a range and see what Word returns if the range has mixed font sizes. You will get the same value which translates as Undefined each time your range has mixed sizes. You can then use that value or the constant that is built in for this purpose wdUndefined.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#3
|
|||
|
|||
|
A couple of things. First, I agree with Andrew. Secondly, you are making use of more declared variables than you need. Thirdly, and this is just a matter of style and a recommendation, I would change variable naming convention.
Range is an object. Paragraph is an object. Range is a property of document object. Paragraphs is a property of the document object. Range is also a property of the paragraph object. So you can replace all this mash: Set aRng = ActiveDocument.Range aRng.End = ActiveDocument.Range.End For Each aPara in aRng.Paragraps with For Each aPara in ActiveDocument.Paragraphs Code:
Sub FontSize()
Dim oPar As Paragraph
For Each oPar In ActiveDocument.Paragraphs
If oPar.Range.Font.Size = wdUndefined Then oPar.Range.HighlightColorIndex = wdBrightGreen
Next oPar
End Sub
Since Range is a object, why not use oRng instead of aRng, oPar instead of aPar strWhatever for strings, varWhatever for variants, bWhatever for boolean (e.g., bCheck instead of just Chect), lngWhatever for Longs etc. |
|
#4
|
|||
|
|||
|
Guessed
I apologize for the strong flavour of bad formatting practices. I'm trying to improve, but it is not always easy, since this is not my profession or main activity. I do apply styles, but on documents which I receive from others, and to do so automatically I often have to 'clean' what I find (any sort of things). Many thanks for suggesting the use of wdUndefined and especially for your patience, past and present. Gmaxey Thanks a lot for providing an example and for all your explanations. I will try to conform to your practices, while I envy your mastery of the subject. |
|
#5
|
|||
|
|||
|
I use this code to level out font size based on the first word of the paragraph. Is it possible to change the code so that uniformity is based on the majority of words in the paragraph? Thanks!
Code:
Sub FontSize()
Dim oPar As Paragraph
Dim iType As Integer
For iType = 1 To 2
For Each oPar In ActiveDocument.StoryRanges(iType).Paragraphs
If oPar.Range.Font.Size = wdUndefined Then
varSz = oPar.Range.Characters.First.Font.Size
oPar.Range.Font.Size = varSz
End If
Next oPar
Next iType
End Sub
|
|
#6
|
|||
|
|||
|
Okay, this could take awhile and it assumes a) There are no single words with mixed font size and 2) the largest font applied will not exceed 99.
Also this is just a swag without much thought: Code:
Sub FontSizeMajor()
Dim oPar As Paragraph
Dim iType As Integer
Dim oWord As Range
Dim arrSizes(99)
Dim lngMax As Long, lngIndex As Long
For iType = 1 To 2
For Each oPar In ActiveDocument.StoryRanges(iType).Paragraphs
If oPar.Range.Font.Size = wdUndefined Then
For Each oWord In oPar.Range.Words
arrSizes(oWord.Font.Size) = arrSizes(oWord.Font.Size) + 1
Next oWord
lngMax = GetMaxNumber(arrSizes)
For lngIndex = 0 To 99
If arrSizes(lngIndex) = lngMax Then
oPar.Range.Font.Size = lngIndex
Exit For
End If
Next lngIndex
End If
Erase arrSizes
Next oPar
Next iType
lbl_Exit:
Exit Sub
End Sub
Public Function GetMaxNumber(ByRef strValues()) As Double
Dim i As Long
For i = LBound(strValues) To UBound(strValues)
If IsNumeric(strValues(i)) Then
If CDbl(strValues(i)) > GetMaxNumber Then GetMaxNumber = CDbl(strValues(i))
End If
Next i
End Function
|
|
#7
|
|||
|
|||
|
Many thanks, Gmaxey! It works like charm. Incredible!
|
|
#8
|
|||
|
|||
|
You are welcome. The third assumption would be that no two font sizes has an equal number of occurrences. E.g., if a 20 word paragraph, has 8 words 8 point, 4 words 12 point and 8 words 16 point. The result would be all words formatted as point 8.
|
|
#9
|
|||
|
|||
|
I see. But I don't think that would be a problem with the documents I get. Thanks again!
|
|
#10
|
||||
|
||||
|
RobiNew
Sigh, here we are again going to great lengths to make documents that are still inconsistently formatted and more complex than they should be. Wouldn't it be better to reset the font attributes of the content so that the font size of all content matches that of the applied paragraph style? Like press Ctrl-A, then Ctrl-Space Then if that introduces problems because styles weren't used correctly, apply the RIGHT style to those paragraphs or apply Character styles where content should vary inside a paragraph. Change the style definitions to ripple changes through the document once the styles are applied correctly. Life would be so much easier if you bent a little and used styles for the purpose they were designed for.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
#11
|
|||
|
|||
|
Hi, Guessed! I really appreciate your suggestions and the philosophy behind them, but unfortunately I cannot apply them to the texts I get. If I use Ctrl-A and then Ctrl-Space, how do I know (for example) which words were italicized within the paragraph? Do believe me: I am always in favor of simple solutions, but automation has its own exigencies.
|
|
#12
|
|||
|
|||
|
Flag them as italic or bold or whatever then apply the appropriate character style and remove the flags.
Code:
Sub ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim oRng As Range
Dim oPar As Paragraph
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Bold = True
While .Execute
oRng.Text = "<b>" & oRng.Text & "</b>"
oRng.Collapse wdCollapseEnd
Wend
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Italic = True
While .Execute
oRng.Text = "<i>" & oRng.Text & "</i>"
oRng.Collapse wdCollapseEnd
Wend
End With
ActiveDocument.Range.Font.Reset
For Each oPar In ActiveDocument.Paragraphs
oPar.Style = "Body Text"
Next
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "(\<i\>\<b\>)(*)(\</b\>\</i\>)"
.Replacement.Text = "\2"
.Replacement.Style = "Emphasis"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "(\<i\>)(*)(\</i\>)"
.Replacement.Text = "\2"
.Replacement.Style = "Subtle Emphasis"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "(\<b\>)(*)(\</b\>)"
.Replacement.Text = "\2"
.Replacement.Style = "Strong"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
lbl_Exit:
Exit Sub
End Sub
|
|
#13
|
|||
|
|||
|
Thank you very much, Gmaxey! That's a great idea for fonts, but what about paragraphs? A similar procedure should be simultanously applied to paragraphs in order to save the original formatting. But even then, how would I know (for example) if an indented paragraph was indented by applying a style or by Tabs or even by Spaces?
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
VBA - Word how to globally change the font and font size in footnotes
|
thomasoj | Word VBA | 3 | 01-15-2020 06:26 AM |
Merging two Word documents: 2nd document not maintaining original font type and font size
|
Swarup | Word | 31 | 08-28-2018 06:55 PM |
| Font size showing different (some superscripted??) but tools show its the same size? | mikkygee | PowerPoint | 4 | 12-14-2015 11:21 PM |
Looping Macro to Change Font, Font Size, and Give Heading 1
|
WH7262 | Word VBA | 1 | 08-26-2014 03:46 PM |
Paragraph (carriage) return font size
|
revrossreddick | Word | 2 | 12-28-2011 01:33 PM |