none
Tried to run this code to learn how to use styles in word doc. Got several errors. Yes Macros OKed. RRS feed

  • Question

  • At

    https://msdn.microsoft.com/en-us/library/office/ff841014.aspx

    has problems noted below.

    Private Sub CommandButton1_Click()

    Dim docActive As Document

    Dim strMessage As String

    Dim styleLoop As Style

    Set docActive = ActiveDocument

    strMessage = "Styles in use:" & vbCr

    For Each styleLoop In docActive.Styles

     If styleLoop.InUse = True Then

     With docActive

     ' .Content.Find 'generates  “ Invalid use of property” if I remove this line then I get below.

      'FUN PART WHEN TYPE .CON  UP POPS  Content  

    MORE fun when type period after Contact up shows Find

      'Takes  line  .Content.Find until I click on Debug.

      '.Content  'Like this get compile error invalid use of property.

      .ClearFormatting  'err 438  object doesn’t support this prop or method

     .Text = ""

     .Style = styleLoop

     .Execute Format:=True

     If .Found = True Then

     strMessage = strMessage & styleLoop.NameLocal & vbCr

     End If

     End With

     End If

    Next styleLoop

     MsgBox strMessage

    End Sub

    I spent hrs on msdn reference sites nothing I found helped.

    Mark J


    Mark J

    Friday, September 9, 2016 3:58 PM

Answers

  • This works for me:

    Private Sub CommandButton1_Click()
        Dim docActive As Document
        Dim strMessage As String
        Dim styleLoop As Style
        Set docActive = ActiveDocument
        strMessage = "Styles in use:" & vbCr
        For Each styleLoop In docActive.Styles
            If styleLoop.InUse = True Then
                    With docActive.Content.Find
                    .ClearFormatting
                    .Text = ""
                    .Style = styleLoop
                    .Format = True
                    If .Execute Then
                        strMessage = strMessage & styleLoop.NameLocal & vbCr
                    End If
                End With
            End If
        Next styleLoop
        MsgBox strMessage
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, September 9, 2016 4:12 PM
  • The macro is intended to be run from Word. If you want to run it in VBScript, you'd have to either start Word ad open a document, or get a reference to the running instance of Word and to the active document in that instance. What is your purpose?

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by PuzzledByWord Wednesday, September 14, 2016 5:42 AM
    Wednesday, September 14, 2016 5:27 AM

All replies

  • This works for me:

    Private Sub CommandButton1_Click()
        Dim docActive As Document
        Dim strMessage As String
        Dim styleLoop As Style
        Set docActive = ActiveDocument
        strMessage = "Styles in use:" & vbCr
        For Each styleLoop In docActive.Styles
            If styleLoop.InUse = True Then
                    With docActive.Content.Find
                    .ClearFormatting
                    .Text = ""
                    .Style = styleLoop
                    .Format = True
                    If .Execute Then
                        strMessage = strMessage & styleLoop.NameLocal & vbCr
                    End If
                End With
            End If
        Next styleLoop
        MsgBox strMessage
    End Sub


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Friday, September 9, 2016 4:12 PM
  • Hans Vogelaar,

    Thanks. By look at your copy LOOKS just like my copy.

    So I will be going over my code extremely close to see if I can find where I did I typo.

    I hope I find a typo in my code.

    Do I need to make sure I clicked on a reference in VBA 7.1 to make this work like

    Scripting Runtime    or VBScript?    for this to work?

    Mark J


    Mark J

    Wednesday, September 14, 2016 4:51 AM
  • The macro is intended to be run from Word. If you want to run it in VBScript, you'd have to either start Word ad open a document, or get a reference to the running instance of Word and to the active document in that instance. What is your purpose?

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by PuzzledByWord Wednesday, September 14, 2016 5:42 AM
    Wednesday, September 14, 2016 5:27 AM
  • Hans Vogelaar,

    found the two lines in mine

     .Execute Format:=True

     If .Found = True Then

    in yours

     .Format=True

    If  .Execute Then

    That was the only difference, BUT    NO ERRORS  at compile and runtime.   Get values I wanted.

    when I added      styleLoop.Description

    puzzled about the "Priority" level   seemed various styles had a Priority ranging from 1 to 10.

    Thank again, some of the code I would have gone I don't want to use that command or keyword.

    Thanks for helping me learn to code better.

    Mark J


    Mark J

    Wednesday, September 14, 2016 5:35 AM