Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-11-2019, 06:25 PM
Genericname1111 Genericname1111 is offline Macro to navigate to specific point in line (Word 97) Windows 7 64bit Macro to navigate to specific point in line (Word 97) Office 97-2003
Novice
Macro to navigate to specific point in line (Word 97)
 
Join Date: Aug 2019
Posts: 17
Genericname1111 is on a distinguished road
Default Macro to navigate to specific point in line (Word 97)

Hi,
I'm using word 97 with a Q and A format. I want to create various macros that involve knowing where the actual text begins (at the left tab), which is after the Q or A of each line.
The Q and A lines are different, so using macros that use "home" and then move right don't work consistently, as they need to move a different amount of characters/words for Q vs A lines. It looks like this, where the text begins at the left tab of the margin:
Q(tab)number(tab)Text


A(tab) (tab) Text

Is it possible to navigate to the beginning of the text consistently with a macro? Hopefully I've explained this properly.
Thanks.
Reply With Quote
  #2  
Old 09-11-2019, 08:31 PM
gmayor's Avatar
gmayor gmayor is offline Macro to navigate to specific point in line (Word 97) Windows 10 Macro to navigate to specific point in line (Word 97) Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

Assuming by 'line' you mean 'paragraph' then

Code:
Sub Macro1()
Dim oPara As Paragraph
Dim oRng As Range
Dim intPos As Integer
    For Each oPara In ActiveDocument.Paragraphs
        Set oRng = oPara.Range
        oRng.End = oRng.End - 1 'omit the paragraph break from the range
        If UBound(Split(oRng.Text, Chr(9))) = 2 Then 'ensure there are two tab characters
            intPos = InStrRev(oRng.Text, Chr(9)) 'Find the second tab character position
            oRng.Start = oRng.Start + intPos 'move the start of the range to that position
            MsgBox Trim(oRng.Text)    'this is the text in the paragraph. Trim to remove spurious spaces
            oRng.Collapse 1    'this is the start of the text portion of the paragrapj
        End If
    Next oPara
    Set oPara = Nothing
    Set oRng = Nothing
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 09-12-2019, 12:20 AM
Genericname1111 Genericname1111 is offline Macro to navigate to specific point in line (Word 97) Windows 7 64bit Macro to navigate to specific point in line (Word 97) Office 97-2003
Novice
Macro to navigate to specific point in line (Word 97)
 
Join Date: Aug 2019
Posts: 17
Genericname1111 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Assuming by 'line' you mean 'paragraph' then

Code:
Sub Macro1()
Dim oPara As Paragraph
Dim oRng As Range
Dim intPos As Integer
    For Each oPara In ActiveDocument.Paragraphs
        Set oRng = oPara.Range
        oRng.End = oRng.End - 1 'omit the paragraph break from the range
        If UBound(Split(oRng.Text, Chr(9))) = 2 Then 'ensure there are two tab characters
            intPos = InStrRev(oRng.Text, Chr(9)) 'Find the second tab character position
            oRng.Start = oRng.Start + intPos 'move the start of the range to that position
            MsgBox Trim(oRng.Text)    'this is the text in the paragraph. Trim to remove spurious spaces
            oRng.Collapse 1    'this is the start of the text portion of the paragrapj
        End If
    Next oPara
    Set oPara = Nothing
    Set oRng = Nothing
End Sub
Hi, thanks for posting that. I'm getting "sub or function not defined" when I run it. I just copied it into a new macro to test it, is there something more I should have done?
Reply With Quote
  #4  
Old 09-12-2019, 12:23 AM
gmayor's Avatar
gmayor gmayor is offline Macro to navigate to specific point in line (Word 97) Windows 10 Macro to navigate to specific point in line (Word 97) Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

It is a macro in its own right. You cannot copy it 'into' a macro.
__________________
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 09-12-2019, 12:31 AM
Genericname1111 Genericname1111 is offline Macro to navigate to specific point in line (Word 97) Windows 7 64bit Macro to navigate to specific point in line (Word 97) Office 97-2003
Novice
Macro to navigate to specific point in line (Word 97)
 
Join Date: Aug 2019
Posts: 17
Genericname1111 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
It is a macro in its own right. You cannot copy it 'into' a macro.
Sorry, I explained that badly - I did make it as a new macro.
Reply With Quote
  #6  
Old 09-12-2019, 02:53 AM
gmayor's Avatar
gmayor gmayor is offline Macro to navigate to specific point in line (Word 97) Windows 10 Macro to navigate to specific point in line (Word 97) Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

Which line reports the error?
__________________
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
  #7  
Old 09-12-2019, 11:12 PM
Genericname1111 Genericname1111 is offline Macro to navigate to specific point in line (Word 97) Windows 7 64bit Macro to navigate to specific point in line (Word 97) Office 97-2003
Novice
Macro to navigate to specific point in line (Word 97)
 
Join Date: Aug 2019
Posts: 17
Genericname1111 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Which line reports the error?
It highlights "Split" in the "If UBound(Split(oRng.Text, Chr(9)))" line.
Reply With Quote
  #8  
Old 09-13-2019, 02:02 AM
gmayor's Avatar
gmayor gmayor is offline Macro to navigate to specific point in line (Word 97) Windows 10 Macro to navigate to specific point in line (Word 97) Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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 line in question to
Code:
If InStr(1, oRng.Text, Chr(9)) > 0 Then
It has been a long time since I programmed for Word 97.
__________________
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
  #9  
Old 09-15-2019, 04:46 PM
Genericname1111 Genericname1111 is offline Macro to navigate to specific point in line (Word 97) Windows 7 64bit Macro to navigate to specific point in line (Word 97) Office 97-2003
Novice
Macro to navigate to specific point in line (Word 97)
 
Join Date: Aug 2019
Posts: 17
Genericname1111 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Change the line in question to
Code:
If InStr(1, oRng.Text, Chr(9)) > 0 Then
It has been a long time since I programmed for Word 97.
Thank you, unfortunately it's now highlighting "InStrev" and reporting the same error (and yeah, it's annoying that my work uses word 97).
Reply With Quote
  #10  
Old 09-15-2019, 08:13 PM
gmayor's Avatar
gmayor gmayor is offline Macro to navigate to specific point in line (Word 97) Windows 10 Macro to navigate to specific point in line (Word 97) Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

Is this code being run on a laptop or a PC that is normally hibernated rather than switched off? If so reboot the PC and try the code again. If not try the following which approaches the task from another direction
Code:
Sub Macro1()
Dim oPara As Paragraph
Dim oRng As Range
    For Each oPara In ActiveDocument.Paragraphs
        Set oRng = oPara.Range
        oRng.End = oRng.End - 1    'omit the paragraph break from the range
        If InStr(1, oRng.Text, Chr(9)) > 0 Then
            oRng.Collapse 0
            oRng.MoveStartUntil Chr(9), wdBackward    'move the start of the range to that position
            MsgBox Trim(oRng.Text)    'this is the text in the paragraph. Trim to remove spurious spaces
            oRng.Collapse 1    'this is the start of the text portion of the paragraph
        End If
    Next oPara
    Set oPara = Nothing
    Set oRng = Nothing
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
  #11  
Old 09-15-2019, 11:55 PM
Genericname1111 Genericname1111 is offline Macro to navigate to specific point in line (Word 97) Windows 7 64bit Macro to navigate to specific point in line (Word 97) Office 97-2003
Novice
Macro to navigate to specific point in line (Word 97)
 
Join Date: Aug 2019
Posts: 17
Genericname1111 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Is this code being run on a laptop or a PC that is normally hibernated rather than switched off? If so reboot the PC and try the code again. If not try the following which approaches the task from another direction
Code:
Sub Macro1()
Dim oPara As Paragraph
Dim oRng As Range
    For Each oPara In ActiveDocument.Paragraphs
        Set oRng = oPara.Range
        oRng.End = oRng.End - 1    'omit the paragraph break from the range
        If InStr(1, oRng.Text, Chr(9)) > 0 Then
            oRng.Collapse 0
            oRng.MoveStartUntil Chr(9), wdBackward    'move the start of the range to that position
            MsgBox Trim(oRng.Text)    'this is the text in the paragraph. Trim to remove spurious spaces
            oRng.Collapse 1    'this is the start of the text portion of the paragraph
        End If
    Next oPara
    Set oPara = Nothing
    Set oRng = Nothing
End Sub
Thanks for that. After running that, it brings up the text from each line in dialogue boxes, but I think I explained what I wanted badly in my opening post, sorry. Basically I'm wanting to know how to use a macro to navigate to the start of the paragraph so that when running various macros that change/delete/add text I can alter body text without changing the Q and A text (since the Q and A lines have different amounts of text before the paragraph starts, so I can't use the home key).
Reply With Quote
  #12  
Old 09-16-2019, 04:38 AM
gmayor's Avatar
gmayor gmayor is offline Macro to navigate to specific point in line (Word 97) Windows 10 Macro to navigate to specific point in line (Word 97) Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

The macro processes each paragraph in turn. It displays the part after the final tab in that paragraph and sets the range to the start of the text. which appears to be what you asked for -
Quote:
knowing where the actual text begins (at the left tab), which is after the Q or A of each line
If you want the range to include that text then remove the line
Code:
oRng.Collapse 1
__________________
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
  #13  
Old 10-06-2019, 03:13 PM
Genericname1111 Genericname1111 is offline Macro to navigate to specific point in line (Word 97) Windows 7 64bit Macro to navigate to specific point in line (Word 97) Office 97-2003
Novice
Macro to navigate to specific point in line (Word 97)
 
Join Date: Aug 2019
Posts: 17
Genericname1111 is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
The macro processes each paragraph in turn. It displays the part after the final tab in that paragraph and sets the range to the start of the text. which appears to be what you asked for - If you want the range to include that text then remove the line
Code:
oRng.Collapse 1
Thank you for your help. So there's a few uses I have in mind for knowing where the paragraph starts, one example would be consistently navigating to the start of the paragraph on each line, which I haven't been able to figure out to do since the amount of characters at the start differs depending on whether it's an answer or question line (question lines have more characters before the paragraph). Would the macro you've suggested enable me to do something like that if I included another macro to run it first? Sorry if the answer is obvious - I am not very used to using macros.
Reply With Quote
  #14  
Old 10-06-2019, 08:54 PM
gmayor's Avatar
gmayor gmayor is offline Macro to navigate to specific point in line (Word 97) Windows 10 Macro to navigate to specific point in line (Word 97) Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
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

The macro example finds text within a paragraph by adjusting the range accordingly. The paragraph range is set by
Code:
Set oRng = oPara.Range
which is the complete paragraph including the paragraph break which ends it.

If you want to locate the start of that paragraph then collapse the range to its start e.g
Code:
oRng.Collapse 1
You would then need to do something with that range before the macro moves on to the next paragraph.
__________________
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
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to navigate to specific point in line (Word 97) Excel Macro finding a specific word ducky831 Excel Programming 3 09-17-2015 01:36 PM
Macro to navigate to specific point in line (Word 97) Cannot scroll past specific point in doc sunriver Word 3 06-24-2015 07:00 PM
Macro to navigate to specific point in line (Word 97) macro to add brackets to each line and add single quotes to each word in the line bracketandquotes Word VBA 17 02-16-2015 03:51 PM
Macro to navigate to specific point in line (Word 97) Deleting A blank Line that has a specific heading style , word 2010 & 2013 SteveWcg Word 5 01-08-2014 10:37 PM
Macro to navigate to specific point in line (Word 97) Point to a specific cell ericerler Mail Merge 1 08-11-2011 05:31 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:26 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