Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-06-2023, 01:07 AM
RobiNew RobiNew is offline Check uniform font size in each paragraph Windows 10 Check uniform font size in each paragraph Office 2016
Competent Performer
Check uniform font size in each paragraph
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default Check uniform font size in each paragraph


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
Reply With Quote
  #2  
Old 12-06-2023, 04:24 AM
Guessed's Avatar
Guessed Guessed is offline Check uniform font size in each paragraph Windows 10 Check uniform font size in each paragraph Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 12-06-2023, 05:00 AM
gmaxey gmaxey is offline Check uniform font size in each paragraph Windows 10 Check uniform font size in each paragraph Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 12-06-2023, 11:00 AM
RobiNew RobiNew is offline Check uniform font size in each paragraph Windows 10 Check uniform font size in each paragraph Office 2016
Competent Performer
Check uniform font size in each paragraph
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

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.
Reply With Quote
  #5  
Old 12-07-2023, 07:53 AM
RobiNew RobiNew is offline Check uniform font size in each paragraph Windows 10 Check uniform font size in each paragraph Office 2016
Competent Performer
Check uniform font size in each paragraph
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

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
Reply With Quote
  #6  
Old 12-07-2023, 08:28 AM
gmaxey gmaxey is offline Check uniform font size in each paragraph Windows 10 Check uniform font size in each paragraph Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 12-07-2023, 09:36 AM
RobiNew RobiNew is offline Check uniform font size in each paragraph Windows 10 Check uniform font size in each paragraph Office 2016
Competent Performer
Check uniform font size in each paragraph
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

Many thanks, Gmaxey! It works like charm. Incredible!
Reply With Quote
  #8  
Old 12-07-2023, 10:38 AM
gmaxey gmaxey is offline Check uniform font size in each paragraph Windows 10 Check uniform font size in each paragraph Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #9  
Old 12-07-2023, 10:42 AM
RobiNew RobiNew is offline Check uniform font size in each paragraph Windows 10 Check uniform font size in each paragraph Office 2016
Competent Performer
Check uniform font size in each paragraph
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

I see. But I don't think that would be a problem with the documents I get. Thanks again!
Reply With Quote
  #10  
Old 12-07-2023, 09:34 PM
Guessed's Avatar
Guessed Guessed is offline Check uniform font size in each paragraph Windows 10 Check uniform font size in each paragraph Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #11  
Old 12-08-2023, 12:51 AM
RobiNew RobiNew is offline Check uniform font size in each paragraph Windows 10 Check uniform font size in each paragraph Office 2016
Competent Performer
Check uniform font size in each paragraph
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

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.
Reply With Quote
  #12  
Old 12-08-2023, 06:41 AM
gmaxey gmaxey is offline Check uniform font size in each paragraph Windows 10 Check uniform font size in each paragraph Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #13  
Old 12-08-2023, 07:45 AM
RobiNew RobiNew is offline Check uniform font size in each paragraph Windows 10 Check uniform font size in each paragraph Office 2016
Competent Performer
Check uniform font size in each paragraph
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Default

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?
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Check uniform font size in each paragraph VBA - Word how to globally change the font and font size in footnotes thomasoj Word VBA 3 01-15-2020 06:26 AM
Check uniform font size in each paragraph 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
Check uniform font size in each paragraph Looping Macro to Change Font, Font Size, and Give Heading 1 WH7262 Word VBA 1 08-26-2014 03:46 PM
Check uniform font size in each paragraph Paragraph (carriage) return font size revrossreddick Word 2 12-28-2011 01:33 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:23 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft