Deleting text - how to do, and prompting user before deleting?

  • Thread starter StargateFanFromWork
  • Start date
S

StargateFanFromWork

I recorded keystrokes and I got this as a result. Is there a way to do the
same as below but with the 2 changes listed at the bottom?
**************************************************************************
Sub DeleteText()
' Deletes all the text on the page.
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub
**************************************************************************

1) Prompt the user is s/he want to delete the text, and cancel returns user
to doct.?
2) Deletes the text without using "Selection".

Thanks! :eek:D
 
G

Greg Maxey

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("\Page").Range
If MsgBox("Are you sure you want to delete this page of text", _
vbQuestion + vbYesNo, "Delete Text This Page") = vbYes Then
oRng.Delete
End If

You might want to look at your macro again for changing case.
End Sub
 
S

StargateFan

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("\Page").Range
If MsgBox("Are you sure you want to delete this page of text", _
vbQuestion + vbYesNo, "Delete Text This Page") = vbYes Then
oRng.Delete
End If

You might want to look at your macro again for changing case.
End Sub

Hi, thanks for the code! Much appreciated.

What do you mean re your reference to the change case macro? Does
this script affect it? I didn't see how, btw (which doesn't mean
anything. I might have missed something <g>.)



Re this script here, the only thing I ran into is a problem I've found
before. Deleting text programatically doesn't allow one to get it
back via ^z. I've never found a solution in XL2K for this, if memory
serves. Is this a problem in Word, too?
 
S

StargateFan

Hi, thanks for the code! Much appreciated.

What do you mean re your reference to the change case macro? Does
this script affect it? I didn't see how, btw (which doesn't mean
anything. I might have missed something <g>.)

O K A Y .... I see now. I just tried pasting code into the page from
a text file (as this is a doct. to clean up text saved from the
internet), and it all went to uppercase. NO GOOD, you're right. What
is causing this??

Also, p.s., but this only deletes one page of text at a time. Is
there a way to get it to delete _all_ the text at once??

Thanks. I thought this was going to be easier. Guess not! :eek:D
 
H

Helmut Weber

Hi StargateFan,

Windows has a unique clipboard, a buffer,
unlike Unix, which has n, if I recall correctly.
Whereby n is any number as far as there are resources.

You may set up as many buffers as you like
and store the contents with or without formatting.

You may get back the w-th deletion of your x-th
session of the y-th day, if you like.

Not practible.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
S

StargateFan

Hi StargateFan,

Windows has a unique clipboard, a buffer,
unlike Unix, which has n, if I recall correctly.
Whereby n is any number as far as there are resources.

You may set up as many buffers as you like
and store the contents with or without formatting.

You may get back the w-th deletion of your x-th
session of the y-th day, if you like.

Not practible.

But why does a deletion done manually work, whereas a deletion done
programatically, not? Do you happen to know?
 
H

Helmut Weber

Hi StargateFan
But why does a deletion done manually work, whereas a deletion done
programatically, not? Do you happen to know?

I don't know.

because someone at MSFT decided otherwise.

Sorry

..
--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg Maxey

What I meant was the you got two answers to your question on that
topic. While Jonathan's obviously solved your immediate problem, and
I am not trying to find fault in it in any way, you might not find it
to be as perfect if your IP was in a storyrange other than the main
text when you ran the code.
 
S

StargateFan

What I meant was the you got two answers to your question on that
topic. While Jonathan's obviously solved your immediate problem, and
I am not trying to find fault in it in any way, you might not find it
to be as perfect if your IP was in a storyrange other than the main
text when you ran the code.

Ah. Then there's something going on here that I don't understand.
Excuse me, but what does IP mean? And if there is something other
than this "storyrange", is it something that can be done easily? Will
the user than be able to retrieve deleted text via ^z, or not?

Please bear with me, these are new terms and concepts for me. Thanks.
 
G

Greg Maxey

IP means the insertion point or cursor. You wanted to know how you could do
way with using selection to change case. My statement "unless you know
where the cursor is" or unless you know where the IP is you would need to
use something like this:

Sub Test()
ActiveDocument.StoryRanges(Selection.StoryType).Case = wdUpperCase
End Sub

A word document consists of several stories. You have the 3 header stories,
3 footer stories, comments story, main text story, etc.

If you had text selected in the footer and ran the code that Jonathan
provided your footer text would remain as is and all the text in the main
text story would be changed to upper case.

(Selection.StoryType) tell Word which storyrange to act on.
 
S

StargateFan

IP means the insertion point or cursor. You wanted to know how you could do
way with using selection to change case. My statement "unless you know
where the cursor is" or unless you know where the IP is you would need to
use something like this:

Sub Test()
ActiveDocument.StoryRanges(Selection.StoryType).Case = wdUpperCase
End Sub

A word document consists of several stories. You have the 3 header stories,
3 footer stories, comments story, main text story, etc.

If you had text selected in the footer and ran the code that Jonathan
provided your footer text would remain as is and all the text in the main
text story would be changed to upper case.

(Selection.StoryType) tell Word which storyrange to act on.

Ah, so that's what "story" means. Thanks. I'll look into this. I'm
assuming that if one writes the macro to include/exclude all these,
that the problem becomes moot re ^z undelete, yes?

Thanks.
 
G

Greg Maxey

Yes you can include all storyranges like this:

Public Sub ScratchMacro()
Dim rngStory As Word.Range
Dim lngJunk As Long
'Fix the skipped blank Header/Footer problem
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
rngStory.Case = wdUpperCase
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End Sub

re ^z undelete, yes?

The code above doesn't delete anything. Not sure I understand this
question.
 
S

StargateFan

Yes you can include all storyranges like this:

Public Sub ScratchMacro()
Dim rngStory As Word.Range
Dim lngJunk As Long
'Fix the skipped blank Header/Footer problem
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
'Iterate through all story types in the current document
For Each rngStory In ActiveDocument.StoryRanges
'Iterate through all linked stories
Do
rngStory.Case = wdUpperCase
'Get next linked story (if any)
Set rngStory = rngStory.NextStoryRange
Loop Until rngStory Is Nothing
Next
End Sub

re ^z undelete, yes?

The code above doesn't delete anything. Not sure I understand this
question.

Oh, well, the thing is that the script for deleting confirmed to me
something that I'd run across in Excel and seems to be the same in
Word.
2
When you delete text programatically, the undelete (^z) won't bring it
back if you change your mind/make a mistake in deleting, etc. Yet
when you delete manually, ^z does. So the problem is that I need to
be able to delete programatically yet can't because I can't undelete.
This actually happened after posting these msgs in this thread - I was
testing the delete code on something and forgot to make a backup and
when after testing couldn't get the text back, had to spend 20 mins
typing it all back in. I know better, of course, it's just that when
you're in the middle of finetuning a doct format, there's so much to
remember you sometimes forget to do vital things like backing up
continuously! <g>. Hence my goal of making a commandbar that by its
very nature, will really help me to remember to do things as the
prompt for them is right there in the buttons and then it's all set up
to do any of the tasks without fiddling. Win-win situation.

Anyway, I really need to be able to delete via a button on the
floating commandbar. And which is why I was hoping that there was a
way to be able to retrieve something via undelete (^z) even though we
just erased it via a macro on it.

I'm not sure that the above covers that, though. I know you advise
that it doesn't delete, but is there a way to ensure that this can be
done even after a macro deletes something for us? Perhaps by
including some of the code above somehow into a delete script?

Thanks. :eek:D
 
R

Russ

re ^z undelete

Type in 'undo' , without the quotes in Word VBA Help to obtain information
on undoing actions.

If you just have one line in your macro to delete the second paragraph:
ActiveDocument.Paragraphs(2).Range.Delete
And run it; you normally can get it back with the Undo/Redo button in the
Word editor or ^z.

But undo has a limited amount of memory to remember things to undo. If your
delete was five steps back, you would also have to undo the preceding four
steps. If you click on the down arrow attached to the undo button, a drop
down list of undo actions will be listed, if there are more than one.

Also the undo memory can be cleared programmatically or can be reset by
other actions. (read the help information)
The VBA editor also has an Undo/Redo button.
 

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