Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-19-2023, 07:01 PM
Chayes Chayes is offline Highlighting just the address Windows 7 32bit Highlighting just the address Office 2003
Advanced Beginner
Highlighting just the address
 
Join Date: May 2012
Posts: 79
Chayes is on a distinguished road
Default Highlighting just the address

I need some advice on solving a puzzle.



I have two types of document after processing. Those with the recipient address at the top of the page , and those where the recipient address is at the bottom.

In both documents these addresses are always top or bottom of the page in the same place , but do vary in size in terms of the amount of lines they occupy , and lines of other information above and below them.

For each , I need a reliable way to select and copy just the address in each. I've tried various ways but not found anything to cope with the variant size of the address.

I'm hoping that someone can suggest some VBA to select the address whether it be at the top or the bottom of the document in a reliable way. Perhaps it will need separate macros for each type.

Perhaps selecting between the beginning of the address to the next return on the page where it is at the top , or from the end of the address to the first return above where it is at the bottom.

I'll attach samples of each. Grateful for any advice.


Attached Files
File Type: doc Sample4.doc (22.5 KB, 2 views)
File Type: doc Sample5.doc (22.5 KB, 2 views)
Reply With Quote
  #2  
Old 05-19-2023, 11:00 PM
gmayor's Avatar
gmayor gmayor is offline Highlighting just the address Windows 10 Highlighting just the address Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
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 of
Default

You can do it with one macro - provided your samples are an accurate reflection of the documents.
Code:
Option Explicit

Sub SelectAddress()
'Graham Mayor - https://www.gmayor.com - Last updated - 20 May 2023
Dim oPara As Paragraph
Dim oRng As Range
Dim lPara As Long
Dim bTop As Boolean
    Set oRng = ActiveDocument.Range
    oRng.Collapse 1
    For lPara = 1 To ActiveDocument.Paragraphs.Count
        Set oPara = ActiveDocument.Paragraphs(lPara)
        If Len(oPara.Range) > 4 Then
            oRng.End = oPara.Range.End
        Else
            Exit For
        End If
    Next lPara
    If oRng.Paragraphs.Count > 2 Then
        bTop = True
        oRng.Select
        GoTo lbl_Exit
    End If
    
    If Not bTop = True Then
        Set oRng = ActiveDocument.Range
        oRng.Collapse 0
        For lPara = ActiveDocument.Paragraphs.Count To 1 Step -1
            Set oPara = ActiveDocument.Paragraphs(lPara)
            If Len(oPara.Range) > 4 Then
                oRng.Start = oPara.Range.Start
            Else
                Exit For
            End If
        Next lPara
        If oRng.Paragraphs.Count > 2 Then
            oRng.Select
        End If
    End If
    
lbl_Exit:
    Set oRng = Nothing
    Set oPara = Nothing
    Exit Sub
End Sub
__________________
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 05-20-2023, 06:58 AM
Chayes Chayes is offline Highlighting just the address Windows 7 32bit Highlighting just the address Office 2003
Advanced Beginner
Highlighting just the address
 
Join Date: May 2012
Posts: 79
Chayes is on a distinguished road
Default

Hi gmayor - many thanks! This is working very well and selecting the address successfully. Much appreciated.


BTW As a side issue : Do you know a way of scrolling to the top of a page in Word without losing focus on any text selected on the page? For example , if an address were selected in the middle of the page , and the top of the page was not showing - how to re-align the page so the top is showing , but keep the address selected? I can do this via various key-presses but all lose the selection. Using the mouse wheel works of course , but I wonder if some VBA could do it programmatically.



Reply With Quote
  #4  
Old 05-20-2023, 07:29 AM
gmayor's Avatar
gmayor gmayor is offline Highlighting just the address Windows 10 Highlighting just the address Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,106
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 of
Default

Change the two instances of oRng.Select to oRng.Copy and the cursor will remain where it was when the macro was run and the address will be copied to the clipboard, which seems to be the aim.
After the second instance enter
Code:
Else
    MsgBox "Address not found"
__________________
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
  #5  
Old 05-20-2023, 08:21 AM
Chayes Chayes is offline Highlighting just the address Windows 7 32bit Highlighting just the address Office 2003
Advanced Beginner
Highlighting just the address
 
Join Date: May 2012
Posts: 79
Chayes is on a distinguished road
Default

Very good - thanks. This works well in the context of the earlier macro and resolves it there.


I was wondering more in wider general terms of how this might be achieved via Word VBA. How to scroll to the top of the page without selected text being unselected...
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
outlook app of android changes outgoing email address to a gmail address bruegel Outlook 0 01-05-2023 08:13 AM
Highlighting just the address MAIL Merge - Site Address / Invoice address mgsjarvis Mail Merge 3 09-25-2018 02:43 PM
Differentiate between my address and my address in a Distribution List fogyreef Outlook 1 01-14-2016 12:45 AM
Add link to email address that hides the actual address or makes it inaccessible to online bots richiebabes Word 1 09-03-2014 03:22 PM
HELP! Outlook 2003 Address Books - multiple account address lists ukmonkeynuts Outlook 0 06-01-2011 06:18 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:17 AM.


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