Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find Styles thru Code (Word XP)

    OK, this one has me very frustrated.
    I'm writing a routine that needs to find a paragraph that has a particular style applied to it. My problem is that Word throws a run-time error 5834 ("Item with Specified Name Does Not Exist") whenever it gets to the line where I tell it what style to look for. The most frustrating thing is that the style definitely DOES exist. In fact, I wrote a boolean function to determine whether it does, and I run it right before trying to set the .Find.Style parameter. And I notice that Word throws that error whenever I use ANY custom style.

    Here's the hunk of code that throws the error, with the offending line in red. (In the code, CSRDoc is a variable that represents the document being searched):

    Set fRange = ActiveDocument.Content
    With fRange.Find
    .Text = "SYNOPSIS"
    If StyleExists(CSRDoc, "Synopsis Header") = True Then
    <font color=red> .Style = CSRDoc.Styles("Synopsis Header")</font color=red>
    Else
    .Style = CSRDoc.Styles("Heading 1")
    End If
    .Format = True
    .Execute
    End With
    If fRange.Find.Found = False Then GoTo NotFound

    Here's the "Style Exists" function I wrote to ensure that the style does, in fact, exist:

    Function StyleExists(WhatDocument As Document, StyleName As String) As Boolean
    Dim tStyle As Style

    StyleExists = False

    For Each tStyle In WhatDocument.Styles
    If tStyle.NameLocal = StyleName Then
    StyleExists = True
    Exit For
    End If
    Next

    End Function

    Does anyone know what gives? The help file isn't very specific about the .Style argument, and it doesn't throw an error if I were to use <font color=red>.Style = CSRDoc.Styles("Heading 1")</font color=red>, so I'm assuming that identifying an object in a collection in that fashion is OK.

    Thanks in advance for any help.
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Find Styles thru Code (Word XP)

    Why do you use ActiveDoc in the first line and CSRDoc in the rest? Are they the same?

    You could try

    .Style = "Synopsis Header"

  3. #3
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Find Styles thru Code (Word XP)

    I can't believe it! I hate when the answers are hiding in plain sight. It was not supposed to be ActiveDocument at all, and THAT'S what caused the error.

    Thanks, Hans. Now, I must go get my glasses checked...
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

Posting Permissions

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