PDA

View Full Version : Solved: Extracting Text from Selection and Copying into Table



b3m4s
02-01-2013, 03:15 PM
Hi Everyone,

I've done quite a bit of VBA on excel, and have recently started on Word, but my boss suggested that I come up with a way to automate a process that we do often, and I'm not sure if it is possible.

I work as a theatrical stage manager and I am trying to take a script, in word, and copy it into a table with three columns: character, type, and line. The thought was that we could select text, then on off-click (or activation of a macro) choose an option from a dropdown box (for type), copy the first word in the paragraph of the selection (for character), and the selected text all into a table. Not sure if all of that makes sense, or if this is possible, or if there is a modified way of doing it. I'm continuing my research, but if anyone has suggestions, it would be appreciated.

gmaxey
02-01-2013, 04:28 PM
You should be able to do that with a userform. Assumes you added a single row three column table to the docment. Put the cursor in the paragraph and call the macro.

UserForm needs a single listbox control:
Standard Module Code:

Sub CallMacro
UserForm1.Show
End Sub

Userform Code:
Private Sub ListBox1_Click()
Dim oTbl As Word.Table
Dim oRow As Row
Dim oRng As Word.Range
Set oTbl = ActiveDocument.Tables(1)
Set oRow = oTbl.Rows.Add
Set oRng = Selection.Paragraphs(1).Range
oRow.Cells(1).Range.Text = oRng.Words(1)
oRow.Cells(2).Range.Text = Me.ListBox1.ListIndex
oRng.MoveStart wdWord, 1
oRow.Cells(3).Range.Text = oRng.Text
Unload Me
End Sub
Private Sub UserForm_Initialize()
With Me.ListBox1
.AddItem "Type A"
.AddItem "Type B"
.AddItem "Type C"
End With
End Sub

b3m4s
02-02-2013, 07:16 PM
Prefect! Thanks Greg. Is there a way I can add the current page number at the cursor location as well?

gmaxey
02-02-2013, 08:15 PM
You and get page number using the range information property:

Debug.Print Selection.Paragraphs(1).Range.Information(wdActiveEndPageNumber)