PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
MsOf03 [Word]Active Document Styles and IfThen Statements-VBForums
Results 1 to 4 of 4

Thread: [Word]Active Document Styles and IfThen Statements

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Posts
    5

    [Word]Active Document Styles and IfThen Statements

    So when the document contains the Style "Side Head", this code obviously works fine. But if the document doesn't contain it, it seems to ignore the Else If part of the statement and return an error that states:
    Run-time error '5941':
    The requested member of the collection does not exist.
    When I select Debug, it highlights the line "Selection.Find.Style = ActiveDocument.Styles("Side Head")
    So how can I write this so that it will actually use the If Then statement?
    Thanks.

    Code:
     Selection.Find.ClearFormatting
     Selection.Find.Style = ActiveDocument.Styles("Side Head")
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = True
            .MatchWholeWord = False
            .MatchByte = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = False
            .MatchFuzzy = False
        End With
            Selection.Find.Execute
        If Selection.Find.Found = True Then
        Selection.HomeKey Unit:=wdLine
        Selection.TypeText Text:="TEST"
        Else
        End If

  2. #2
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: [Word]Active Document Styles and IfThen Statements

    It is not working because the error is happening when trying to access a member that does not exist, so the IF-Then statement is never reached. If what you want to do is verify that the style exist, then loop through the collection and search for the name

    vb Code:
    1. Dim found As Boolean
    2.     Dim myStyle As Style
    3.  
    4.     found = False
    5.     For Each myStyle In ActiveDocument.Styles
    6.         If myStyle.NameLocal = "Side Head" Then found = True
    7.     Next myStyle
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2011
    Posts
    5

    Re: [Word]Active Document Styles and IfThen Statements

    I don't simply want to verify that the style exists, I want to find the style, then if it exists, I want to move to the front of it and insert the text "Test". Then do that until all the similar style has been found.
    If the style is not found in the document, then I simply want it to move on.

  4. #4
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: [Word]Active Document Styles and IfThen Statements

    THat code will thell you if the style exist, if it does you can do all that you wanted to do, but if it does not, then don't go into the .Find as your code will throw an error. Just do that with the found boolean variable or adjust the code as you wish.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width