Delete paragraphs in active document?

S

Sandi Vogel

I have a collections letter that 1) thanks the recipient for verbal agreement
to payment plan in paragraph1 and then 2) askes the client to sign and return
a promissory note in paragrah 2.

I'd like to deleted paragraph 2, paragraph 10 (the enclosure line), and page
2 (the enclosure) if the author does not want the promissory note.

I've set up a form that has 3 buttons: Cancel, Letter Only, and
Letter+Promissory Note.

The Letter only button passes txt values to bookmarks, and I'd like it to
delete text as shown above. It's not ActiveDocument.Paragrah.2.Delete; it's
not anything I've tried so far. Please help, VBA masters!

If I'm going about this entirely wrong, I want to know. If I just need to
read a key webpage, I'll take a link. I've googled endlessly and can't find
it.

Thanks in advance!
Sandi
 
S

Sandi Vogel

Thanks for your reply. But, I'm not trying to delete empty paragraphs. I'm
trying to delete a paragraph that says

"Those arrangements will be fine however; we will need to revisit this
matter in six months. Due to your balance being over BALAGE old; with your
last payment of $LASTPAYMENT received on LASTPAYDATE, we are unable to extend
this account much longer. For that reason we will also need you to sign a
promissory note (enclosed) which will not accrue interest unless you default
on our agreement. Please sign that and mail it back to my attention within a
week."

I tried this, based on the article you referenced:

Dim MyRange As Range
Set MyRange = ActiveDocument.Paragraphs(8)
MyRange.Delete

No complile errors, but the paragraph remans.

and this:
Dim MyRange As Range
Set MyRange = ActiveDocument.Paragraphs(8)
If MyRange.Text = "Those arrangements*" Then MyRange.Delete

Again, no compile eorrs, but the paragraph remains.

I feel like I'm close....any other thoughts? Can I even use a wildcard
character in VBA?
 
J

Jean-Guy Marcil

Sandi Vogel was telling us:
Sandi Vogel nous racontait que :
I have a collections letter that 1) thanks the recipient for verbal
agreement to payment plan in paragraph1 and then 2) askes the client
to sign and return a promissory note in paragrah 2.

I'd like to deleted paragraph 2, paragraph 10 (the enclosure line),
and page 2 (the enclosure) if the author does not want the promissory
note.

I've set up a form that has 3 buttons: Cancel, Letter Only, and
Letter+Promissory Note.

The Letter only button passes txt values to bookmarks, and I'd like
it to delete text as shown above. It's not
ActiveDocument.Paragrah.2.Delete; it's not anything I've tried so
far. Please help, VBA masters!

If I'm going about this entirely wrong, I want to know. If I just
need to read a key webpage, I'll take a link. I've googled endlessly
and can't find it.

Thanks in advance!
Sandi

Instead of
ActiveDocument.Paragrah.2.Delete
have you tried
ActiveDocument.Paragraphs(2).Range.Delete
???

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg Maxey

Sandi,

Are you positive the text you want to delete is and always will be in
paragraph 8?

Another suggestion (and this was just scratched together and could
likely be improved upon) is to find the text and then delete the
paragraph the text is in.

I assume that your page two is in section 2 of the document. It
should be, otherwise deleting the earlier text might pull some of the
page 2 text onto page 1.

Try something like this:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Encl: (1) Promissory Note"
.Execute
If .Found Then
oRng.Paragraphs(1).Range.Delete
End If
End With
With oRng.Find
.Text = "Those arrangements will be"
.Execute
If .Found Then
oRng.Paragraphs(1).Range.Delete
End If
End With
ActiveDocument.Sections(2).Range.Delete
With oRng.Find
.Text = "^b"
.Replacement.Text = ""
.Execute Replace:=wdReplaceOne
End With
End Sub
 
S

Sandi Vogel

Thanks for your time! Here's what I have, after your suggestion (it's still
not working. I'm including the code before and after because I don't
understand what "range" is, and it's probably what I'm doing wrong):

Set oRng = ActiveDocument.Bookmarks("bkBillingAtty").Range
oRng.Text = Me.cboBillingAtty.Text
ActiveDocument.Bookmarks.Add Name:="bkBillingAtty", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkBillingAtty"

Dim MyRange As Range
Set MyRange = ActiveDocument.Paragraphs(8).Range
ActiveDocument.Paragraphs(8).Range.Delete

Me.Hide
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
Selection.GoTo What:=wdGoToBookmark, Name:="bkStartHere"
End Sub
 
G

Greg Maxey

Personnally I see little resemlance with what you have to what you
started with. Again I will ask are you sure that you and your Word
application are in agreement on just what "paragraph 8" is? Do you
have any empty paragraphs? Are there any line breaks that you are
counting as paragraphs?

If this line is executing without and error, then the real paragraph 8
is being deleted.

ActiveDocument.Paragraphs(8).Range.Delete

If it wasn't then you would get an error "The requested member of the
collection does not exist."
 
S

Sandi Vogel

Ah, that looks like it should work and makes sense to me. But, it's not
working.

(I'm positive the text will always but in paragraph 8; the user opens the
template and a user form opens. She fills out the form, clicks OK btn
(labeled "Letter only").

The OK subroutine already starts off with a

Dim oRng As Word.Range for the bookmarks, as follows:

Private Sub btnOK_Click()
On Error Resume Next

Dim oRng As Word.Range

Set oRng = ActiveDocument.Bookmarks("bkName").Range
oRng.Text = Me.txtName
Document.Bookmarks.Add Name:="bkName", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkName"

etc.; so I'm getting "Expected End Sub".

Is there a way to "end Dim" and then restart it to use your suggestion of
Fine & Delete? A sub-sub routine for the same button?
 
G

Greg Maxey

Sandi,

As I mentioned in my post following your reply to JGM. If paragraph 8
exists then this line will either delete it or throw and error:

ActiveDocument.Paragraphs(8).Range.Delete

Your On Error Resume Next is likely masking that error.


Before we spin wheels further, can you create a new document based on
your template, open the VB editor and execute this code:

Sub Test()
ActiveDocument.Paragraphs(8).Range.Delete
End Sub

Paragraph 8 whatever it is if it exists will be deleted or the
compiler will throw and error.

You don't need to "end Dim" and I have never heard of it. You can
declare another range

Dim oRng2 as Word.Range
Set oRng2 = ActiveDocument.Range
With oRng.Find
.....

or after you do your deeds with the bookmarks then just redefine oRng

Set oRng = ActiveDocument.Range

Based on your post in response to JGM, I just don't
 
S

Sandi Vogel

I apologize for the confusion. I neglected to "notify me of replies" when I
originally responded to Carim. Greg then responded, and I didn't see it
before I responded to Jean-Guy's later help with trying to delete a paragraph
number. I then replied to Greg, attempting to use his find & replace
suggestion - hence the difference in the code you are seeing. Let's keep
then focus on the paragraph deletion method, then; and I won't change the
code between postings :)

I really do appreciate everyone's time & expertise here; and want you to
know I do try to help myself first. Please bear with a beginner!

Does Word agree on what I think a paragraph number is? I view Word in
normal view. I understand a Paragraph mark vs. soft breaks. There are no
empty paragraphs, and my styles have appropriate Spacing After. Am I missing
something else? Greg used the term "line break"; while I use "soft break".
ARe these actually two different things?

I am counting paragraph marks like so (spacing after set via style; not a
hard return):

Date <P1>

ClientName<P2>
Address<P3>
CityStZip<P4>

Re:<P5>

Dear Salutation:<P6>

Thank you for agreeing...<P7>

Those arrangements will be fine<P8>

Here is the entire subroutine for the OK button. Thanks again for any help
you can give.

Private Sub btnOK_Click()
On Error Resume Next
Dim oRng As Word.Range

Set oRng = ActiveDocument.Bookmarks("bkName").Range
oRng.Text = Me.txtName
Document.Bookmarks.Add Name:="bkName", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkName"

Set oRng = ActiveDocument.Bookmarks("bkAdd1").Range
oRng.Text = Me.txtAdd1
ActiveDocument.Bookmarks.Add Name:="bkAdd1", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkAdd1"

Set oRng = ActiveDocument.Bookmarks("bkCityStZip").Range
oRng.Text = Me.txtCityStZip
ActiveDocument.Bookmarks.Add Name:="bkCityStZip", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkCityStZip"

Set oRng = ActiveDocument.Bookmarks("bkRe").Range
oRng.Text = Me.txtRe
ActiveDocument.Bookmarks.Add Name:="bkRe", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkRe"

Set oRng = ActiveDocument.Bookmarks("bkDear").Range
oRng.Text = Me.txtDear
ActiveDocument.Bookmarks.Add Name:="bkDear", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkDear"

Set oRng = ActiveDocument.Bookmarks("bkConversationDay").Range
oRng.Text = Me.txtConversationDay
ActiveDocument.Bookmarks.Add Name:="bkConversationDay", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkConversationDay"

Set oRng = ActiveDocument.Bookmarks("bkPastDueBal").Range
oRng.Text = Me.txtPastDueBal
ActiveDocument.Bookmarks.Add Name:="bkPastDueBal", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkPastDueBal"

Set oRng = ActiveDocument.Bookmarks("bkMonthlyPayment").Range
oRng.Text = Me.txtMonthlyPayment
ActiveDocument.Bookmarks.Add Name:="bkMonthlyPayment", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkMonthlyPayment"

Set oRng = ActiveDocument.Bookmarks("bkFirstPayDate").Range
oRng.Text = Me.txtFirstPayDate
ActiveDocument.Bookmarks.Add Name:="bkFirstPayDate", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkFirstPayDate"

Set oRng = ActiveDocument.Bookmarks("bkMonthlyDueDate").Range
oRng.Text = Me.txtMonthlyDueDate
ActiveDocument.Bookmarks.Add Name:="bkMonthlyDueDate", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkMonthlyDueDate"

Set oRng = ActiveDocument.Bookmarks("bkBillingAtty").Range
oRng.Text = Me.cboBillingAtty.Text
ActiveDocument.Bookmarks.Add Name:="bkBillingAtty", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkBillingAtty"

ActiveDocument.Paragraphs(8).Range.Delete

Me.Hide
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
Selection.GoTo What:=wdGoToBookmark, Name:="bkStartHere"
End Sub
 
G

Greg Maxey

Sandi,

I am just guessing, but from the looks of your example I would say that you
are using line breaks after ClientName and Addrees. Did you use Shift+Enter
to create this single space look?

If you did, then you really only have 6 paragraphs and 2 line breaks. The
code is running to completion without generating an error when it tries to
delete paragraph 8 (which doesn't exist) because you are bypassing the error
with your On Error Resume Next statement.

If I am correct, you can confirm this by stetting out (removing) the On
Error Resume Next statement or by toggling the display of non-printing
characters (CTRL+Shift+*) and seeing if you have litle left pointing arrow
marks after those two line instead of pilcrows.
 
S

Sandi Vogel

Greg Maxey said:
Sandi,

I am just guessing, but from the looks of your example I would say that you
are using line breaks after ClientName and Addrees. Did you use Shift+Enter
to create this single space look?

If you did, then you really only have 6 paragraphs and 2 line breaks. The
code is running to completion without generating an error when it tries to
delete paragraph 8 (which doesn't exist) because you are bypassing the error
with your On Error Resume Next statement.

If I am correct, you can confirm this by stetting out (removing) the On
Error Resume Next statement or by toggling the display of non-printing
characters (CTRL+Shift+*) and seeing if you have litle left pointing arrow
marks after those two line instead of pilcrows.


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
 
G

Greg Maxey

Sandi,

Did you have a more information or questions that you want to post? Nothing
appears on my end.
 
J

Jean-Guy Marcil

Sandi Vogel was telling us:
Sandi Vogel nous racontait que :
I apologize for the confusion. I neglected to "notify me of replies"
when I originally responded to Carim. Greg then responded, and I
didn't see it before I responded to Jean-Guy's later help with trying
to delete a paragraph number. I then replied to Greg, attempting to
use his find & replace suggestion - hence the difference in the code
you are seeing. Let's keep then focus on the paragraph deletion
method, then; and I won't change the code between postings :)

I really do appreciate everyone's time & expertise here; and want you
to know I do try to help myself first. Please bear with a beginner!

Does Word agree on what I think a paragraph number is? I view Word in
normal view. I understand a Paragraph mark vs. soft breaks. There
are no empty paragraphs, and my styles have appropriate Spacing
After. Am I missing something else? Greg used the term "line break";
while I use "soft break". ARe these actually two different things?

I am counting paragraph marks like so (spacing after set via style;
not a hard return):

Date <P1>

ClientName<P2>
Address<P3>
CityStZip<P4>

Re:<P5>

Dear Salutation:<P6>

Thank you for agreeing...<P7>

Those arrangements will be fine<P8>

Here is the entire subroutine for the OK button. Thanks again for
any help you can give.

Private Sub btnOK_Click()
On Error Resume Next
Dim oRng As Word.Range

Set oRng = ActiveDocument.Bookmarks("bkName").Range
oRng.Text = Me.txtName
Document.Bookmarks.Add Name:="bkName", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkName"

Set oRng = ActiveDocument.Bookmarks("bkAdd1").Range
oRng.Text = Me.txtAdd1
ActiveDocument.Bookmarks.Add Name:="bkAdd1", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkAdd1"

Set oRng = ActiveDocument.Bookmarks("bkCityStZip").Range
oRng.Text = Me.txtCityStZip
ActiveDocument.Bookmarks.Add Name:="bkCityStZip", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkCityStZip"

Set oRng = ActiveDocument.Bookmarks("bkRe").Range
oRng.Text = Me.txtRe
ActiveDocument.Bookmarks.Add Name:="bkRe", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkRe"

Set oRng = ActiveDocument.Bookmarks("bkDear").Range
oRng.Text = Me.txtDear
ActiveDocument.Bookmarks.Add Name:="bkDear", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkDear"

Set oRng = ActiveDocument.Bookmarks("bkConversationDay").Range
oRng.Text = Me.txtConversationDay
ActiveDocument.Bookmarks.Add Name:="bkConversationDay", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkConversationDay"

Set oRng = ActiveDocument.Bookmarks("bkPastDueBal").Range
oRng.Text = Me.txtPastDueBal
ActiveDocument.Bookmarks.Add Name:="bkPastDueBal", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkPastDueBal"

Set oRng = ActiveDocument.Bookmarks("bkMonthlyPayment").Range
oRng.Text = Me.txtMonthlyPayment
ActiveDocument.Bookmarks.Add Name:="bkMonthlyPayment", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkMonthlyPayment"

Set oRng = ActiveDocument.Bookmarks("bkFirstPayDate").Range
oRng.Text = Me.txtFirstPayDate
ActiveDocument.Bookmarks.Add Name:="bkFirstPayDate", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkFirstPayDate"

Set oRng = ActiveDocument.Bookmarks("bkMonthlyDueDate").Range
oRng.Text = Me.txtMonthlyDueDate
ActiveDocument.Bookmarks.Add Name:="bkMonthlyDueDate", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkMonthlyDueDate"

Set oRng = ActiveDocument.Bookmarks("bkBillingAtty").Range
oRng.Text = Me.cboBillingAtty.Text
ActiveDocument.Bookmarks.Add Name:="bkBillingAtty", Range:=oRng
Selection.GoTo What:=wdGoToBookmark, Name:="bkBillingAtty"

ActiveDocument.Paragraphs(8).Range.Delete

Me.Hide
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
Selection.GoTo What:=wdGoToBookmark, Name:="bkStartHere"
End Sub

While you work out your paragraph 8 problem, here are some comments on your
code:

Never, ever, use
On Error Resume Next
right at the beginning of the code. It is sloppy and can create all kinds of
problems. We normally use that for a specific line of code we know can
generate an error that is kind of expected and that is handled in the code.
Then, right after that line, we use:
On Error GoTo 0
to cancel the effect of "Resume Next".

Also, why are you continually selecting a bookmark, and then carrying on? It
seems to me that it serves no purpose, other than slowing down the code...:
Selection.GoTo What:=wdGoToBookmark, Name:="bkPastDueBal"
etc.

Finally, instead of repeating all that code for inserting the text and
recreating the bookmark, use a private sub to do all the work and write the
code only once. See my example below.

'_______________________________________
Private Sub btnOK_Click()

With Me
AddText "bkName", .txtName
AddText "bkAdd1", .txtAdd1
AddText "bkCityStZip", .txtCityStZip
AddText "bkRe", .txtRe
AddText "bkDear", .txtDear
AddText "bkConversationDay", .txtConversationDay
AddText "bkPastDueBal", .txtPastDueBal
AddText "bkMonthlyPayment", .txtMonthlyPayment
AddText "bkFirstPayDate", .txtFirstPayDate
AddText "bkMonthlyDueDate", .txtMonthlyDueDate
AddText "bkBillingAtty", .cboBillingAtty.Text
.Hide
End With

With ActiveDocument

.Paragraphs(8).Range.Delete
.PrintPreview
.ClosePrintPreview
End With

Selection.GoTo What:=wdGoToBookmark, Name:="bkStartHere"

End Sub
'_______________________________________

'_______________________________________
Private Sub AddText(strBookName As String, strText As String)

Dim rgeText As Range

Set rgeText = ActiveDocument.Bookmarks(strBookName).Range
rgeText.Text = strText
Document.Bookmarks.Add strBookName, rgeText

End Sub
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

Sandi Vogel

Not sure what happened to my reply last night. Here's the gist:

"Did you use Shift+Enter to create this single space look?"

No, the address block is three paragraphs with a Style that has Space After
set to 0.

Thanks for the vocablulary lessons: pilcrow! And finally the key combo for
the hide/show button.

I've removed Error Resume Next and will be pulliing out my hair for the rest
of the morning. Thanks.
 
S

Sandi Vogel

Thanks for the feedback. I'll work on it after the paragraph8 is resolved,
assuming I haven't gone insane by then. I have questions about all three of
the items you bring up (actually Greg has pointed out the cleaner "with..end
with" to me in the past but I couldn't make it work). If I can't work out
these issues on my own, I will probably post each issues as a separate thread
-- this thread is getting confusing!
 
G

Greg Maxey

Sandi,

Ok. I am really interested now. If you e-mail the document I would
like to look at it. You can use the Feedback link on my website
gregmaxey.mvps.org
 
S

Sandi Vogel

Very generous of you, thanks!

Greg Maxey said:
Sandi,

Ok. I am really interested now. If you e-mail the document I would
like to look at it. You can use the Feedback link on my website
gregmaxey.mvps.org
 
S

Sandi Vogel

Thanks for all your help & patience guys! This is resolved.

For the benefit of the discussion group: I was messing up the With
statement. Here's what finally worked:

With ActiveDocument
.Paragraphs(8).Range.Delete
.Paragraphs(12).Range.Delete
End With
 

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