From: Mark on 14 Feb 2010 18:43 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
From: Doug Robbins - Word MVP on 14 Feb 2010 22:37 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. "Mark" <Mark(a)discussions.microsoft.com> wrote in message news:6DC50E34-200E-43F5-AB6F-4955AF3BEC88(a)microsoft.com... >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 > >
From: Jay Freedman on 14 Feb 2010 23:04 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 FAQ: http://word.mvps.org Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit. On Sun, 14 Feb 2010 15:43:01 -0800, Mark <Mark(a)discussions.microsoft.com> wrote: >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 >
From: macropod on 14 Feb 2010 23:16 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. -- Cheers macropod [Microsoft MVP - Word] "Mark" <Mark(a)discussions.microsoft.com> wrote in message news:6DC50E34-200E-43F5-AB6F-4955AF3BEC88(a)microsoft.com... >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 > >
From: Mark on 15 Feb 2010 02:06 Doug, Your macro works exactly as I was hoping. It does exactly what I needed for my reports.
|
Next
|
Last
Pages: 1 2 Prev: Pass control name from Access to Word and save text in the con Next: Macros get disconnected |