Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-02-2014, 02:19 AM
NobodysPerfect NobodysPerfect is offline Search in Range Only Problem Windows 8 Search in Range Only Problem Office 2010 32bit
Competent Performer
Search in Range Only Problem
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default Search in Range Only Problem

Hi to all,



I’m looking for a) an explanation and b) for some workaround for the following phenomenon :

Given: A paragraph completely (=incl. paragraph marker) formatted in bold, next paragraph Font.Bold = False
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.
Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.

Selection: First three words of this paragraph.
VBA code:
Code:
Sub RangeFindProblem()
Dim oRng As Range
    Set oRng = Selection.Range
    With oRng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Font.Bold = True
        .Text = ""
        .Replacement.Text = "<start>^&<end>"
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .Execute Replace:=wdReplaceAll
    End With
End Sub


Result: Although the range has been set to the first three words only, the complete paragraph (=incl. paragraph marker) has been searched and 'tagged':
<start>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.
<end>Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.

The start of the oRng will always be set correctly according to the selection, but the end will not. The end always seems be set to the very end of the document: all bold passages will be 'tagged'.

I’m a bit confused . I’ve been searching in ranges for many years now, but never noticed that behavior before.

So, first question: Any explanations?
Second question: What can I do to solve this problem?

Thanks for any ideas
NP
Reply With Quote
  #2  
Old 10-02-2014, 02:43 AM
gmayor's Avatar
gmayor gmayor is offline Search in Range Only Problem Windows 7 64bit Search in Range Only Problem Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I have noticed this phenomenon and I cannot readily explain it, but you can work around it:

Code:
Dim oRng As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
        .Font.Bold = True
        .Text = Selection.Text
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        Do While .Execute
            If oRng.Start = Selection.Start Then
                oRng.Text = "<start>" & oRng.Text & "<end>"
                Exit Do
            End If
        Loop
    End With
or even shorter
Code:
Dim oRng As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
        Do While .Execute(FindText:=Selection.Text)
            If oRng.Start = Selection.Start Then
                oRng.Text = "<start>" & oRng.Text & "<end>"
                Exit Do
            End If
        Loop
    End With
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 10-02-2014, 06:55 AM
gmaxey gmaxey is offline Search in Range Only Problem Windows 7 32bit Search in Range Only Problem Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

The behavior is discussed here:
http://gregmaxey.com/word_tip_pages/..._property.html

Basically:
1) we can't find a "thing" when that thing exactly matches the search range, and 2) when that thing does exactly match the search range, the dynamically redefined range is a "runaway" range.

I've modified the PracticalIV code on the page to address your specific issue:

Code:
Sub PracticalIV()
Dim oRng As Word.Range
  'Insert a temporary range modifier.
  Selection.Range.InsertAfter "~"
  Selection.MoveEnd wdCharacter, 1
  Set oRng = Selection.Range
  'Add unique (unanticipated formatting to the modifier).
  oRng.Characters.Last.Font.Bold = False
  With oRng.Find
    .Font.Bold = True
    .Replacement.Text = "<start>^&<end>"
    .Execute Replace:=wdReplaceAll
  End With
  'Delete the modifier.
  oRng.Characters.Last.Delete
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 10-02-2014, 06:58 AM
NobodysPerfect NobodysPerfect is offline Search in Range Only Problem Windows 8 Search in Range Only Problem Office 2010 32bit
Competent Performer
Search in Range Only Problem
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default

Hi Grahman,

Thanks for your code, it - of course - works.

You wrote you
Quote:
noticed this phenomenon and I cannot readily explain it.
Does that mean there is a resonable explanation, but it is too complicated/long for the forum, or is it more like a guess?

If there is an explanation, a link would be enough. I'd really like to unerstand what happens.

BTW: do you know if this phenomenon only occurs with .FONT properties?


Thanks again
NP
Reply With Quote
  #5  
Old 10-02-2014, 07:04 AM
NobodysPerfect NobodysPerfect is offline Search in Range Only Problem Windows 8 Search in Range Only Problem Office 2010 32bit
Competent Performer
Search in Range Only Problem
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default

Ooops,

your sent another post during my typing ...

Basically I know your 'How To Search Site', but as it's the first time (for me) a oRng.Find procedure failed, I'm still a bit puzzled. It seems to be illogical somehow - for me.

But I'll work it out.

Cheers
NP
Reply With Quote
  #6  
Old 10-02-2014, 07:05 AM
gmaxey gmaxey is offline Search in Range Only Problem Windows 7 32bit Search in Range Only Problem Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

NP,

No the problem is universal. Again it is a result of trying to find something in a range when the range is that something. Here is another demo:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
  ActiveDocument.Range.Delete
  ActiveDocument.Range.Text = "Test"
  Set oRng = ActiveDocument.Words(1)
  Debug.Print oRng
  With oRng.Find
    .Text = "Test"
    If .Execute Then
      MsgBox "Found"
    Else
      MsgBox "Not found.  Why? Because the thing we are looking for exactly matches the search range." & _
             " We can't find ""Test"" IN the search range because ""Test"" IS the search range."
    End If
  End With
  'Now redefine the range.
  Set oRng = ActiveDocument.Range '(includes Words(1) and the end of document mark.
  Debug.Print oRng
  With oRng.Find
    .Text = "Test"
    If .Execute Then
      MsgBox "Found."
    End If
  End With
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 10-02-2014, 07:07 AM
gmaxey gmaxey is offline Search in Range Only Problem Windows 7 32bit Search in Range Only Problem Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

I think you are confusing gmayor with gmaxey.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 10-02-2014, 07:35 AM
NobodysPerfect NobodysPerfect is offline Search in Range Only Problem Windows 8 Search in Range Only Problem Office 2010 32bit
Competent Performer
Search in Range Only Problem
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default

Well, normally I wouldn't, but as I said: I'm a bit confused

NP
Reply With Quote
  #9  
Old 10-02-2014, 09:51 PM
gmayor's Avatar
gmayor gmayor is offline Search in Range Only Problem Windows 7 64bit Search in Range Only Problem Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

There is of course the additional issue that if you are processing the selection, you wouldn't need to use the Range.Find function because you have already found it in order to select it.

Code:
Dim oRng As Range
Set oRng = Selection.Range
oRng.Text = "<start>" & oRng.Text & "<end>"
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #10  
Old 10-05-2014, 04:43 AM
NobodysPerfect NobodysPerfect is offline Search in Range Only Problem Windows 8 Search in Range Only Problem Office 2010 32bit
Competent Performer
Search in Range Only Problem
 
Join Date: Jan 2014
Location: Germany
Posts: 136
NobodysPerfect is on a distinguished road
Default

Hi,

I think, I really got it now - no longer confused. But it's not really intuitive ...

Thanks again for your help gmayor and gmaxey
NP
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Onenote 2013 search is not identifying search terms correctly Delta223 OneNote 0 08-12-2014 06:40 AM
Search for date and then apply mutliple search criteria in huge dataset maxtymo Excel 2 12-01-2013 04:52 AM
Looking for Windows Search app with ability to search by content gopher_everett Office 1 02-28-2013 09:23 PM
Search Function Problem MageWind Excel 2 06-29-2011 09:46 AM
Search in Range Only Problem Windows explorer search problem...help!! NewSysAdmin Windows 1 03-08-2011 05:33 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:09 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft