Deleting a Subset of a Line of Text

G

Gregg Anderson

In a Word 2007 document every line of text has the following format:
(bullet icon) Monday, text.
(bullet icon) Tuesday, more text...
(bullet icon) Friday, even more text.

I would like to create a macro such that if I hightlight a group of these
lines, the macro would selectively delete everything after the "DayOfWeek,"
rightward to the end of the line leaving...
(bullet icon) Monday,
(bullet icon) Tuesday,
etc...
A VB script would be OK too. But I'm a newbie to VB.
Thanks
 
P

Pesach Shelnitz

Hi Gregg,

Try this macro to see if it does what you want. Note that the macro assumes
that your bullets appear because your text is formatted as a list.

Sub DeleteAfterDay()
Dim i As Long
Dim pos As Long
Dim myRange As Range

With Selection.Range
For i = 1 To .ListParagraphs.Count
Set myRange = .ListParagraphs(i).Range
With myRange.Find
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
If .Execute("[FMSTWadehinorstu]{3,3}day,") Then
myRange.Collapse Direction:=wdCollapseEnd
pos = myRange.start
myRange.Expand wdParagraph
myRange.start = pos
myRange.MoveEnd wdCharacter, -1
myRange.Select
myRange.Delete
End If
End With
Next
End With
Set myRange = Nothing
End Sub
 
G

Gregg Anderson

Works perfectly! Thanks Pesach.
Can I now put a little icon in the Quick Access Toolbar that would access
your macro? It's not obvious to me how to do that.
--
Gregg A


Pesach Shelnitz said:
Hi Gregg,

Try this macro to see if it does what you want. Note that the macro assumes
that your bullets appear because your text is formatted as a list.

Sub DeleteAfterDay()
Dim i As Long
Dim pos As Long
Dim myRange As Range

With Selection.Range
For i = 1 To .ListParagraphs.Count
Set myRange = .ListParagraphs(i).Range
With myRange.Find
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
If .Execute("[FMSTWadehinorstu]{3,3}day,") Then
myRange.Collapse Direction:=wdCollapseEnd
pos = myRange.start
myRange.Expand wdParagraph
myRange.start = pos
myRange.MoveEnd wdCharacter, -1
myRange.Select
myRange.Delete
End If
End With
Next
End With
Set myRange = Nothing
End Sub

--
Hope this helps,
Pesach Shelnitz


Gregg Anderson said:
In a Word 2007 document every line of text has the following format:
(bullet icon) Monday, text.
(bullet icon) Tuesday, more text...
(bullet icon) Friday, even more text.

I would like to create a macro such that if I hightlight a group of these
lines, the macro would selectively delete everything after the "DayOfWeek,"
rightward to the end of the line leaving...
(bullet icon) Monday,
(bullet icon) Tuesday,
etc...
A VB script would be OK too. But I'm a newbie to VB.
Thanks
 
P

Pesach Shelnitz

Hi Gregg,

The basic procedure for adding a macro to the QAT is as follows.

1. Right-click the Quick Access Toolbar and then click Customize Quick
Access Toolbar.
2. In the Word Options dialog box, verify that Customize is selected.
3. In the Choose commands drop-down list, select Macros.
4. Select the macro by name in the list of macros.
5. Click the Add button.
6. If you want to select a different icon for the macro, click Modify.
7. Click OK.

--
Hope this helps,
Pesach Shelnitz


Gregg Anderson said:
Works perfectly! Thanks Pesach.
Can I now put a little icon in the Quick Access Toolbar that would access
your macro? It's not obvious to me how to do that.
--
Gregg A


Pesach Shelnitz said:
Hi Gregg,

Try this macro to see if it does what you want. Note that the macro assumes
that your bullets appear because your text is formatted as a list.

Sub DeleteAfterDay()
Dim i As Long
Dim pos As Long
Dim myRange As Range

With Selection.Range
For i = 1 To .ListParagraphs.Count
Set myRange = .ListParagraphs(i).Range
With myRange.Find
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
If .Execute("[FMSTWadehinorstu]{3,3}day,") Then
myRange.Collapse Direction:=wdCollapseEnd
pos = myRange.start
myRange.Expand wdParagraph
myRange.start = pos
myRange.MoveEnd wdCharacter, -1
myRange.Select
myRange.Delete
End If
End With
Next
End With
Set myRange = Nothing
End Sub

--
Hope this helps,
Pesach Shelnitz


Gregg Anderson said:
In a Word 2007 document every line of text has the following format:
(bullet icon) Monday, text.
(bullet icon) Tuesday, more text...
(bullet icon) Friday, even more text.

I would like to create a macro such that if I hightlight a group of these
lines, the macro would selectively delete everything after the "DayOfWeek,"
rightward to the end of the line leaving...
(bullet icon) Monday,
(bullet icon) Tuesday,
etc...
A VB script would be OK too. But I'm a newbie to VB.
Thanks
 
G

Gregg Anderson

Worked again!
Thank you.
--
Gregg A


Pesach Shelnitz said:
Hi Gregg,

The basic procedure for adding a macro to the QAT is as follows.

1. Right-click the Quick Access Toolbar and then click Customize Quick
Access Toolbar.
2. In the Word Options dialog box, verify that Customize is selected.
3. In the Choose commands drop-down list, select Macros.
4. Select the macro by name in the list of macros.
5. Click the Add button.
6. If you want to select a different icon for the macro, click Modify.
7. Click OK.

--
Hope this helps,
Pesach Shelnitz


Gregg Anderson said:
Works perfectly! Thanks Pesach.
Can I now put a little icon in the Quick Access Toolbar that would access
your macro? It's not obvious to me how to do that.
--
Gregg A


Pesach Shelnitz said:
Hi Gregg,

Try this macro to see if it does what you want. Note that the macro assumes
that your bullets appear because your text is formatted as a list.

Sub DeleteAfterDay()
Dim i As Long
Dim pos As Long
Dim myRange As Range

With Selection.Range
For i = 1 To .ListParagraphs.Count
Set myRange = .ListParagraphs(i).Range
With myRange.Find
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
If .Execute("[FMSTWadehinorstu]{3,3}day,") Then
myRange.Collapse Direction:=wdCollapseEnd
pos = myRange.start
myRange.Expand wdParagraph
myRange.start = pos
myRange.MoveEnd wdCharacter, -1
myRange.Select
myRange.Delete
End If
End With
Next
End With
Set myRange = Nothing
End Sub

--
Hope this helps,
Pesach Shelnitz


:

In a Word 2007 document every line of text has the following format:
(bullet icon) Monday, text.
(bullet icon) Tuesday, more text...
(bullet icon) Friday, even more text.

I would like to create a macro such that if I hightlight a group of these
lines, the macro would selectively delete everything after the "DayOfWeek,"
rightward to the end of the line leaving...
(bullet icon) Monday,
(bullet icon) Tuesday,
etc...
A VB script would be OK too. But I'm a newbie to VB.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top