Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-09-2020, 02:47 AM
alex100 alex100 is offline Finding text without moving the cursor down Windows 7 64bit Finding text without moving the cursor down Office 2016
Advanced Beginner
Finding text without moving the cursor down
 
Join Date: May 2020
Posts: 58
alex100 is on a distinguished road
Default Finding text without moving the cursor down

I am using the following code in a larger Word macro that I use...

Code:
With Selection.Find
    .ClearFormatting
    .Text = "http" & "*" & vbCr & "-----"
    .MatchWildcards = True
    .MatchWholeWord = True
    Do While .Execute
        Selection.MoveStart Unit:=wdCharacter, Count:=0
        Selection.MoveEnd Unit:=wdCharacter, Count:=-6
    Loop
End With
url = Selection.Text
It finds and saves (in a variable) the first URL address that is written in this particular format:

http:// www.address.com
-----

The problem is that the code moves down the page (depending on where an URL address is found), and the current page view goes out of focus.



How can I avoid this? What I need is to keep the same functionality of the code, but achive this in a seamless way, without moving the cursor at all.

Using 'Application.ScreenUpdating = False' does seem to help, but the page still loses its initial position when the code is run. I also tried saving the vertical scroll position and restore it later (using 'ActiveWindow.ActivePane.VerticalPercentScrolled') . That helped too, but did not solve the problem entirely.

Alex
Reply With Quote
  #2  
Old 10-09-2020, 05:47 AM
gmaxey gmaxey is online now Finding text without moving the cursor down Windows 10 Finding text without moving the cursor down Office 2016
Word MVP 2003-2009
 
Join Date: May 2010
Location: Marble, NC
Posts: 1,106
gmaxey has a spectacular aura aboutgmaxey has a spectacular aura aboutgmaxey has a spectacular aura about
Default

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim URL As String
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    Selection.ClearFormatting
    .Text = "http" & "*" & vbCr & "-----"
    .MatchWildcards = True
    .MatchWholeWord = True
    Do While .Execute
      oRng.End = oRng.End - 6
      URL = oRng.Text
      oRng.Collapse wdCollapseEnd
    Loop
  End With
  MsgBox URL
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 10-09-2020, 08:09 AM
alex100 alex100 is offline Finding text without moving the cursor down Windows 7 64bit Finding text without moving the cursor down Office 2016
Advanced Beginner
Finding text without moving the cursor down
 
Join Date: May 2020
Posts: 58
alex100 is on a distinguished road
Default

Thank you very much, this is just what I needed!

Alex
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cursor moving text in blocks, black blocks appear Gippslander Word 2 11-06-2018 11:31 PM
Finding text without moving the cursor down How do I lock an editable text box header and stop it moving with the rest of the main body text? thegaffa Word 6 09-28-2018 09:21 AM
finding 'struck-through' text joebloggs Word 1 08-16-2017 10:27 PM
Selecting and moving text boxes identified by specific text. Chayes Word VBA 8 02-22-2016 07:01 AM
Finding text without moving the cursor down Finding the nth Blank Value in a Row of Text ljg713 Excel 7 01-12-2016 07:49 AM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 06:37 AM.


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