Results 1 to 2 of 2
  1. #1
    Lounger
    Join Date
    Apr 2002
    Location
    Sydney, Australia
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Search using Range fails (Word 2002, VBA)

    I use the following code to check if the word 'enclosure' appears in the current section:

    If modSharedCode.Encl = True Then
    Selection.InsertParagraph
    Selection.InsertParagraph
    Selection.TypeText "Enclosure"
    Selection.InsertParagraph
    End If
    GoTo startsearch

    Function Encl() As Boolean
    Dim intSection As Integer
    Dim rngDoc As Range

    intSection = Selection.Information(wdActiveEndSectionNumber)
    Set rngDoc = ActiveDocument.Sections(intSection).Range


    rngDoc.Find.ClearFormatting
    rngDoc.Find.Replacement.ClearFormatting
    With rngDoc.Find
    .Text = "enclos"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    End With
    If rngDoc.Find.Execute = True Then
    Encl = True
    Else
    Encl = False
    End If
    set rngDoc=Nothing
    Exit Function

    The next time my code performs a search using the standard Selection object, Word 2002 hangs and causes problems until I reboot the PC. I have experienced similar problems using the range object in other code. Does anyone know why? Is this a bug?

    Thanks

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

    Re: Search using Range fails (Word 2002, VBA)

    I copied the Encl function into a module, and performed some tests. Running Encl didn't cause problems with subsequent searches with the Selection object. It didn't matter whether either of the searches found something or not. So I suspect there must be something else the matter with your code.

    I don't know if this is still relevant, but if so, you might post a demo document with the code (replace sensitive information by dummy text).

    For the record, I am using Word 2002 SP-2 on Windows XP Home SP-1.

Posting Permissions

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