![]() |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
Quote:
|
|
#4
|
||||
|
||||
|
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 |
|
#5
|
|||
|
|||
|
Sorry, I explained that badly - I did make it as a new macro.
|
|
#6
|
||||
|
||||
|
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 |
|
#7
|
|||
|
|||
|
It highlights "Split" in the "If UBound(Split(oRng.Text, Chr(9)))" line.
|
|
#8
|
||||
|
||||
|
Change the line in question to
Code:
If InStr(1, oRng.Text, Chr(9)) > 0 Then
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
#9
|
|||
|
|||
|
Thank you, unfortunately it's now highlighting "InStrev" and reporting the same error (and yeah, it's annoying that my work uses word 97).
|
|
#10
|
||||
|
||||
|
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 |
|
#11
|
|||
|
|||
|
Quote:
|
|
#12
|
||||
|
||||
|
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:
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 |
|
#13
|
|||
|
|||
|
Quote:
|
|
#14
|
||||
|
||||
|
The macro example finds text within a paragraph by adjusting the range accordingly. The paragraph range is set by
Code:
Set oRng = oPara.Range If you want to locate the start of that paragraph then collapse the range to its start e.g 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 |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Excel Macro finding a specific word
|
ducky831 | Excel Programming | 3 | 09-17-2015 01:36 PM |
Cannot scroll past specific point in doc
|
sunriver | Word | 3 | 06-24-2015 07:00 PM |
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 |
Deleting A blank Line that has a specific heading style , word 2010 & 2013
|
SteveWcg | Word | 5 | 01-08-2014 10:37 PM |
Point to a specific cell
|
ericerler | Mail Merge | 1 | 08-11-2011 05:31 AM |