Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating a user form that deletes specific parapgraphs

2 views
Skip to first unread message

everindrummer

unread,
Mar 14, 2010, 6:43:02 AM3/14/10
to
hi, hope you can help. Im trying to create a document for work, and ive been
confused how best to do it. I have a letter that contains 3 seperate
paragraphs. When opening the letter i want a user form to open, requiring the
user to make a selection of which paragraph they need to use (only one of the
paragraphs will be used on each letter). So, when the user makes the
seleciton and confirms, the other 2 paragraphs are removed. Im really
struggling, and am very basic at VBA.

any suggestions welcome.

Graham Mayor

unread,
Mar 14, 2010, 8:11:17 AM3/14/10
to
It would be simpler to allow the user to pick which paragraph to use then
enter that paragraph.

Use a list box to display sufficient information to choose which paragraph
Here using the Text Para1 to 3. Given default names for the userform, list
box and command button, the code associated with the userform could be
similar to the following. Put a docVariable field {DocVariable varPara} in
the document to display the selected paragraph text.. See also
http://www.gmayor.com/SelectFile.htm

Option Explicit
Private oVars As Variables
Private Sub CommandButton1_Click()
Set oVars = ActiveDocument.Variables
Select Case Me.ListBox1.Value
Case Is = "Para 1"
oVars("varPara").Value = "The text of Paragraph 1"
Case Is = "Para 2"
oVars("varPara").Value = "The text of Paragraph 2"
Case Is = "Para 3"
oVars("varPara").Value = "The text of Paragraph 3"
End Select
ActiveDocument.Fields.Update
Unload Me
End Sub

Private Sub UserForm_Initialize()
Me.ListBox1.AddItem "Para 1"
Me.ListBox1.AddItem "Para 2"
Me.ListBox1.AddItem "Para 3"
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


"everindrummer" <everin...@discussions.microsoft.com> wrote in message
news:7B32488B-B213-4886...@microsoft.com...

Fumei2 via OfficeKB.com

unread,
Mar 15, 2010, 4:48:23 PM3/15/10
to
"It would be simpler to allow the user to pick which paragraph to use then
enter that paragraph."

Hmmm. While I am not totally disagreeing, I would say this is debatable. I
think it is equally valid to use an reductive process, rather than additive
process.

If the there paragraphs are bookmarked, you can use option buttons in a frame
(thus only being able to choose one). Whatever option is TRUE (that bookmark
is retained), the others are removed.

Private Sub CommandButton1_Click()

If optPara1.Value = True Then
ActiveDocument.Bookmarks("para2").Range.Delete
ActiveDocument.Bookmarks("paar3").Range.Delete
GoTo MyExit:
End If
If optPara2.Value = True Then
ActiveDocument.Bookmarks("para1").Range.Delete
ActiveDocument.Bookmarks("para3").Range.Delete
GoTo MyExit
End If
If optPara3.Value = True Then
ActiveDocument.Bookmarks("para1").Range.Delete
ActiveDocument.Bookmarks("para2").Range.Delete
Exit Sub
End If
MyExit:
Unload Me
End Sub

The advantage of this is that if the userform is called when you invoke the
template (Document_New), you end up with the appropriate paragraph, PLUS you
have free rein to edit the original text in the template. You have no
restriction as to size, as the text content is not hard coded.

oVars("varPara").Value = "The text of Paragraph 1"

This puts actual text into the DOCVARIABLE. What if the paragraph is very
long? Using the reductive bookmark route means the actual content is not
relevant. If the content is changed, change it in the actual
document/template. No need to edit the code. Plus if the "chunk" is now a
couple of paragraphs (or even includes a graphic), it makes no difference as
long as it is within the range of the bookmarks.

Just a thought.

Gerry

>> hi, hope you can help. Im trying to create a document for work, and ive
>> been

>[quoted text clipped - 8 lines]
>>
>> any suggestions welcome.

--
Gerry

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-programming/201003/1

Graham Mayor

unread,
Mar 16, 2010, 3:31:31 AM3/16/10
to
Yes you could do it that way, but I guess approach is a matter of
preference. Mine is to use the macro to add text as required :)

The main problem with a reductive process as you describe, is that of
replacing the deleted paragraphs in the event of a wrong choice - though
this is not insurmountable.

There is a potential issue with docvariable fields, but they will work with
most practical paragraph lengths. If you want the paragraph to be user
editable without the need to edit the macro, you could read the paragraph
into the docvariable from from an external text file. Without exploring the
potential to do that in any depth the following would work.

Or you could use includetext fields. There were some alternative suffestions
in the linked page that could be adapted.

Option Explicit
Private oVars As Variables

Private oFSO As New FileSystemObject
Private oPara As Object
Private sText As String


Private Sub CommandButton1_Click()
Set oVars = ActiveDocument.Variables
Select Case Me.ListBox1.Value
Case Is = "Para 1"

Set oPara = oFSO.OpenTextFile("c:\Para1.TXT")


Case Is = "Para 2"

Set oPara = oFSO.OpenTextFile("c:\Para2.TXT")


Case Is = "Para 3"

Set oPara = oFSO.OpenTextFile("c:\Para3.TXT")
End Select
sText = ""
Do Until oPara.AtEndOfStream
sText = sText & oPara.ReadLine
Loop
oVars("varPara").Value = sText


ActiveDocument.Fields.Update
Unload Me
End Sub

Private Sub UserForm_Initialize()
Me.ListBox1.AddItem "Para 1"
Me.ListBox1.AddItem "Para 2"
Me.ListBox1.AddItem "Para 3"
End Sub

--

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

"Fumei2 via OfficeKB.com" <u53619@uwe> wrote in message
news:a50fb0fe38b6e@uwe...

Graham Mayor

unread,
Mar 16, 2010, 7:54:10 AM3/16/10
to

Just for the hell of it, I have been playing around with this approach and
it can easily be used to accommodate inserted texts with multiple paragraphs
to a total of several pages in length. Reading text files in this way is
much faster than reading from Word documents. In the following example the
texts (three of them here, but there could be more) are stored in documents
called Para(N).txt where (N) is a number reflecting the order of the List
Box items - thus Para1.txt to Para3.txt.

Option Explicit
Private oVars As Variables
Private oFSO As New FileSystemObject
Private oPara As Object
Private sText As String

Private sFname As String
Private iCount As Integer


Private Sub CommandButton1_Click()
Set oVars = ActiveDocument.Variables

iCount = Me.ListBox1.ListIndex + 1
sFname = "c:\Para" & iCount & ".txt"
Set oPara = oFSO.OpenTextFile(sFname)


sText = ""
Do Until oPara.AtEndOfStream

sText = sText & oPara.ReadLine & vbCr
Loop
sText = Left(sText, Len(sText) - 1)


oVars("varPara").Value = sText
ActiveDocument.Fields.Update
Unload Me
End Sub

Private Sub UserForm_Initialize()
With Me.ListBox1
.Clear
.AddItem "Para 1"
.AddItem "Para 2"
.AddItem "Para 3"
.ListIndex = 0
End With
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

"Graham Mayor" <gma...@REMOVETHISmvps.org> wrote in message
news:Ou2vxqNx...@TK2MSFTNGP04.phx.gbl...


> Yes you could do it that way, but I guess approach is a matter of
> preference. Mine is to use the macro to add text as required :)
>
> The main problem with a reductive process as you describe, is that of
> replacing the deleted paragraphs in the event of a wrong choice - though
> this is not insurmountable.
>
> There is a potential issue with docvariable fields, but they will work
> with most practical paragraph lengths. If you want the paragraph to be
> user editable without the need to edit the macro, you could read the

> paragraph into the docvariable from an external text file. Without

0 new messages