MS Office Forum / Word / Programming / August 2006
VBA Involving Paragraphs
|
|
Thread rating:  |
KenWilson1000@gmail.com - 17 Aug 2006 15:18 GMT Hi. I wonder if the following is possible in Word, using VBA.
I have approx. 20 different paragraphs of text on the same Word doc entitled Core.doc. Can I identify each of these paragraphs with a distinct name, such as Paragraph1, Paragraph3, etc. and then compose a type of listbox from which to select several of these paragraphs, and have my "combination" of chosen paragraphs then copied into a second, open Word doc?
Example one: if I selected Paragraphs 9, 3, 20, 1, from the listbox, and then pressed a button, these 4 paragraphs would be copied into the other open Word doc, in that order.
If this is possible, could you please share with me the proper VBA code to accomplish this. Thank you very, very much for your help.
Ken
Jezebel - 17 Aug 2006 15:35 GMT Newtext = doc1.Paragraphs(9).Range & doc1.Paragraphs(3).Range ...
> Hi. I wonder if the following is possible in Word, using VBA. > [quoted text clipped - 13 lines] > > Ken KenWilson1000@gmail.com - 17 Aug 2006 17:49 GMT Jezebel, I appreciate your reply, but I need some more help, if you would. How would I create a listbox with the 20 Paragraph choices on a userform, and more importantly, using your code, how would I "insert" or "place" the NewText you mentioned into the other open Word doc? (I'm so much more familiar with Excel VBA). Thank you for your help.
Ken
Doug Robbins - Word MVP - 17 Aug 2006 18:13 GMT It is easy enough to create a list box that would contain the numbers of the paragraphs and then insert the respective paragraphs into a document, but getting them inserted in any order other than the order they are listed in the listbox will be an issue.
See the article "How to find out which Items are selected in a Multi-Select ListBox" at:
http://www.word.mvps.org/FAQs/Userforms/GetMultiSelectValues.htm
I would store the paragraphs in the cells of the second column of a two column table in your core document and populate the cells in the first column with the numbers.
This routine which loads a listbox with client details stored in a table in a separate document (which makes it easy to maintain with additions, deletions etc.), that document being saved as Clients.Doc for the following code, shows you how to load the information into the listbox.
On the UserForm, have a list box (ListBox1) and a Command Button (CommandButton1) and use the following code in the UserForm_Initialize() and the CommandButton1_Click() routines
Private Sub UserForm_Initialize() Dim sourcedoc As Document, i As Integer, j As Integer, myitem As Range, m As Long, n As Long ' Modify the path in the following line so that it matches where you saved Clients.doc Application.ScreenUpdating = False ' Open the file containing the client details Set sourcedoc = Documents.Open(FileName:="e:\worddocs\Clients.doc") ' Get the number or clients = number of rows in the table of client details less one i = sourcedoc.Tables(1).Rows.Count - 1 ' Get the number of columns in the table of client details j = sourcedoc.Tables(1).Columns.Count ' Set the number of columns in the Listbox to match ' the number of columns in the table of client details ListBox1.ColumnCount = j ' Define an array to be loaded with the client data Dim MyArray() As Variant 'Load client data into MyArray ReDim MyArray(i, j) For n = 0 To j - 1 For m = 0 To i - 1 Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range myitem.End = myitem.End - 1 MyArray(m, n) = myitem.Text Next m Next n ' Load data into ListBox1 ListBox1.List() = MyArray ' Close the file containing the client details sourcedoc.Close SaveChanges:=wdDoNotSaveChanges End Sub
Private Sub CommandButton1_Click() Dim i As Integer, Addressee As String Addressee = "" For i = 1 To ListBox1.ColumnCount ListBox1.BoundColumn = i Addressee = Addressee & ListBox1.Value & vbCr Next i ActiveDocument.Bookmarks("Addressee").Range.InsertAfter Addressee UserForm2.Hide End Sub
The Initialize statement will populate the listbox with the data from the table and then when a client is selected in from the list and the command button is clicked, the information for that client will be inserted into a bookmark in the document. You may want to vary the manner in which it is inserted to suit our exact requirements, but hopefully this will get you started.
To make it easy for you, the code has been written so that it will deal with any number of clients and any number of details about each client. It assumes that the first row of the table containing the client details is a header row.
 Signature Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my services on a paid consulting basis.
Doug Robbins - Word MVP
> Jezebel, I appreciate your reply, but I need some more help, if > you would. How would I create a listbox with the 20 Paragraph [quoted text clipped - 4 lines] > > Ken Greg Maxey - 18 Aug 2006 00:26 GMT Ken,
As Doug points out, it would be hard to resolve the order you picked the paragraph numbers using a multi-select listbox.
Here is an idea you might try. Create the userform with a listbox, label (set the caption to nothing) and a command button.
Private Sub UserForm_Initialize() 'Populate the Listbox with paragraph numbers and reset. Dim myArray() Me.ListBox1.List = Split("Reset,1,2,3,4,5,6,7,8", ",") 'up to 20 End Sub
Private Sub ListBox1_Change() 'Build the caption value based on the order you select the paragraphs. 'If you mess up, then use the reset to start over. If Me.ListBox1.Value = "Reset" Then Me.Label1.Caption = "" Else If Len(Me.Label1.Caption) = 0 Then Me.Label1.Caption = Me.Label1.Caption & Me.ListBox1.Value Else Me.Label1.Caption = Me.Label1.Caption & "-" & Me.ListBox1.Value End If End If End Sub
When you are satisfied with the order displayed in the label caption then execute
Private Sub CommandButton1_Click() Dim oDoc As Word.Document Dim myText As String Dim myArray Dim i As Long myArray = Split(Me.Label1.Caption, "-") Set oDoc = Documents.Open("C:\Source.doc") For i = 0 To UBound(myArray) myText = myText & oDoc.Paragraphs(myArray(i)).Range.Text Next oDoc.Close Selection.InsertAfter myText Unload Me End Sub
Private Sub UserForm_Initialize() Dim myArray() Me.ListBox1.List = Split("Reset,1,2,3,4,5,6,7,8", ",") 'up to 20 End Sub
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
> Jezebel, I appreciate your reply, but I need some more help, if > you would. How would I create a listbox with the 20 Paragraph [quoted text clipped - 4 lines] > > Ken KenWilson1000@gmail.com - 18 Aug 2006 12:21 GMT I truly thank each of you for jumping in quickly and helping with your code and ideas. Great generosity here.
Ken
|
|
|