Selecting variable amount of text in Word 2007 macro/VBA

M

Mark

I have recently started using VBA to make my macros work better in Word. I
had been using wp51 for DOS (not kidding, this has worked well for me and I
knew how to program it to do most everything I needed) for making custom
reports but now that I have upgraded to a Windows 7, I can no longer install
this old program.

When my macro runs, I need to be able to select (then delete) a variable
amount of text. In the example below [with the cursor just to the right of 3
which I marked with an X)], I would like to be able to delete #3 through #5
then have the cursor end up just to the left of 1) under suggestions.
However, I would like this macro to do the same for starting at any other
number (4 or 5 for example) without any user input.

In wp51, that was easy as you click on select, then go to the end point of
the selection (I would just put a character like # and use the find
function), then delete. With Word, of course, you can only select a fixed
amount of text in a macro.

Is there some simple VBA code that I put in my macro to perform this task
that does not require any user input? Also, I would like to do this for the
lower section if I don't use all the 5 numbered suggestion lines using the
same macro.

3) X (insertion point here)

4)

5)

SUGGESTIONS:

1)

2)

3)

4)

5)

Thanks,

Mark
 
D

Doug Robbins - Word MVP

Dim myRange as Range
Set myRange = Selection.Range
myRange.End = ActiveDocument.Range.End
myRange.End = myRange.Start + Instr(myRange, "#")
myRange.Delete

Would delete everything from the point where the selection is located until
the first point where # appears.

--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.
 
J

Jay Freedman

Well, this statement is just wrong:
With Word, of course, you can only select a fixed
amount of text in a macro.

There are several ways to select variable amounts of text. Which one
to use depends on what the document looks like before you run the
macro and what you want it to look like afterward. It can be very easy
if the starting point is absolutely uniform and known, or it can be
quite difficult if the document is the typical helter-skelter made by
unsophisticated users.

For example, *if* the starting document looks like what you showed,
and *if* you put a # character at the end of the material you want
deleted, and then go back and put the cursor at the beginning, then
either of these two macros will do what you want. (The count in the
..MoveRight statement might need adjustment if there is no space after
the parentheses.)

Sub DeleteToHash1()
With Selection
.Extend Character:="#"
.Delete
.MoveDown Unit:=wdParagraph, Count:=2
.MoveRight Unit:=wdCharacter, Count:=3
End With
End Sub

Sub DeleteToHash2()
With Selection
.MoveEndUntil Cset:="#"
.MoveEnd Unit:=wdCharacter, Count:=1
.Delete
.MoveDown Unit:=wdParagraph, Count:=2
.MoveRight Unit:=wdCharacter, Count:=3
End With
End Sub

There are other solutions involving the .Find object.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.


I have recently started using VBA to make my macros work better in Word. I
had been using wp51 for DOS (not kidding, this has worked well for me and I
knew how to program it to do most everything I needed) for making custom
reports but now that I have upgraded to a Windows 7, I can no longer install
this old program.

When my macro runs, I need to be able to select (then delete) a variable
amount of text. In the example below [with the cursor just to the right of 3
which I marked with an X)], I would like to be able to delete #3 through #5
then have the cursor end up just to the left of 1) under suggestions.
However, I would like this macro to do the same for starting at any other
number (4 or 5 for example) without any user input.

In wp51, that was easy as you click on select, then go to the end point of
the selection (I would just put a character like # and use the find
function), then delete. With Word, of course, you can only select a fixed
amount of text in a macro.

Is there some simple VBA code that I put in my macro to perform this task
that does not require any user input? Also, I would like to do this for the
lower section if I don't use all the 5 numbered suggestion lines using the
same macro.

3) X (insertion point here)

4)

5)

SUGGESTIONS:

1)

2)

3)

4)

5)

Thanks,

Mark
 
M

macropod

Hi Mark,

If there is a pattern to the blocks of text you're wanting to Find and Replace/Delete, you could use a Find/Replace operation with
wildcards. See 'Find and replace text or formatting' in Word's help file for more details.
 
M

Mark

Doug,

Your macro works exactly as I was hoping. It does exactly what I needed for
my reports.
 
M

Mark

Jay,

Your 2 macros also do what I asked for (as did Doug's macro). I did have to
play around a little with the the count for the for the paragraph and
character in the last 2 lines of each code (I have a tab after the number).

What I meant by saying that there was no easy way to select a variable
amount of text, was that I could not do this using the macro recorder in Word
just using Word commands. I assumed that there was an easy way to do this
with VBA programming but I am too much of a novice to figure it out.

Thanks for your help,

Mark
 
M

Mark

Macropod,

I did try to use find and replace using format but it would select stuff
above my selection and text that I had already typed. It may be my lack of
expertise in formatting but I could not make that work.

Thanks,

Mark
 
J

Jay Freedman

Mark,

The macro recorder in Word is very limited compared to what you can do
by writing VBA from scratch. In this case, though, you can get the
equivalent of my first macro by recording the use of the Extend
command (F8), which is less well-known than it should be.

After inserting the # character and positioning the cursor, turn on
the recorder and enter these keystrokes:

F8
#
Del
Ctrl+down arrow (twice)
Ctrl+right arrow (three times)

The macro I get from this is:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 2/15/2010 by Jay Freedman
'
Selection.Extend
Selection.Extend Character:="#"
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=2
Selection.MoveRight Unit:=wdWord, Count:=3
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

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