View Single Post
 
Old 04-25-2018, 12:57 PM
slaycock slaycock is offline Windows 7 64bit Office 2016
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

Most of what you have written is a good attempt but there are a number of simplifications that can be made and a couple of misunderstandings that need to be corrected.


The definition and assignment


Dim mycomment As Object

Set objDoc = ActiveDocument

are not necessary as you only use active document once in your macro. So you could use Activedocument directly (as ActiveDocument.storyranges(wdmaintextstory). If you wanted this macro to be usable by multiple documents it would be better to have a parameter of word.range which represents the range you want to check.

e.g.

Sub CheckFonts(this_range as word.range)

If you then wanted to add flexibility you could make the parameter optional so that if no parameter was provided the active document is used. The default action can be specified as the parameter to assign to the optional argument if no such parameter is supplied but unfortunately you can't specify the active document as the default range.

Sub CheckFonts(optional this_range as word.range=nothing)

The with statement is useful for providing a shortcut to save retyping qualifier text multiple times. Thuis it makes more sense to use the full reference in the for loop and to dereference objSingleWord within the loop

The method .addcomment is an Excel method so won't work in Word. You can check on the definition of keywords by clicking on the keyword and then pressing F1. This will bring up the MS help page for the keyword. This would have helped you see that you'd got an Excel rather than Word method.

The correct syntax is '<range>.comments.add range:=<a_range>, Text:=<comment text>

Unfortunately, in word, uou cannot change the visibility of individual comments. You can only have all or none displayed. The visibility of comments is a property of the View object which is in turn a property of the ActiveWindow object. To hide all the comments you are adding use

this_range.application.activewindow.view.showcomme nts=false

For the name of the font this is obtained using .font.name



Code:
Sub test1()
    
    CheckFonts 
    
End Sub

sub test2()

    CheckFonts ActiveDocument.StoryRanges(wdTextMainStory)

End Sub

Sub test3()                                               

dim my_section                                          as Word.Section

    for each my_section in activedocument.sections
        checkfonts my_section.range
        ' do something for each section?
    next

end sub

Sub CheckFonts(Optional this_range As Word.Range = Nothing)

Dim objSingleWord                                     As Word.Range
Dim my_comment                                      As Word.Comment


    If this_range Is Nothing Then
        Set this_range = ActiveDocument.StoryRanges(wdMainTextStory)
    End If

    this_range.Application.ActiveWindow.view.ShowComments = False
    For Each objSingleWord In this_range.Words
        With objSingleWord
            If .Font.Size <> 10 Then
                .Comments.Add Range:=objSingleWord, Text:="Warning: Font size is " & .Font.Size
                ' or, if you wish to do other activities with the added comment use
                ' Set my_comment = .Comments.Add(Range:=objSingleWord, Text:="here a comment")
            End If
            If .Font.name <> "Arial" Then
                .Comments.Add Range:=objSingleWord, Text:="Warning: Font name is " & .Font.name
            End If
        End With
    Next
End Sub
Reply With Quote