#1
|
|||
|
|||
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. |
#2
|
||||
|
||||
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 |
#3
|
|||
|
|||
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. |
#4
|
||||
|
||||
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 |
#5
|
|||
|
|||
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... |
|
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 |
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 |