PDA

View Full Version : Solved: Getting the header number in selected paragraph



K1w1
09-19-2005, 02:51 AM
Just a nice simple problem for my first post.
I have these pieces of code I use to take paragraphs from a signed agreed document (cannot be changed) where I extract requirements to tables test document and once the test detail has been added the table is loaded into XL to manage the test process.
I would like to skip the intermediate word table.
The problem I have is when I select a paragraph
like
4.3.7 The customer number must be numeric.
" Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend"
or
" Selection.Next(wdParagraph, Count:=1).Select"
The only bit that appears in the select is "The customer number must be numeric."
However a selection.copy followed by the "Selection.PasteSpecial link:=False, dataType:=wdPasteText" results in the "4.3.7" appearing as text in the new document.
Whereabouts can I get the number in the macro?
Thanks
Steve.
(This has also been posted on the office experts forum.)

gmaxey
09-19-2005, 03:09 AM
I don't know if this will help, but something like the following will return a paragraph text range and listnumber string:


Sub Test()
Dim myRng As Range
Set myRng = ActiveDocument.Paragraphs(1).Range
MsgBox ActiveDocument.Paragraphs(1).Range.ListFormat.ListString _
& " " & myRng
End Sub

K1w1
09-19-2005, 03:17 AM
Thanks for that. I will give this a try when I get to work tomorrow.

I have to do this at home as the site where I do this does not provide internet access or any of the VB help files.

Will be about 10 hours from now.

gmaxey
09-19-2005, 10:54 AM
Ok, carry this with you. You can sort of use it as Copy and Paste pair. Note, when you paste the paragraph into a new document the numbering is simple text.


Option Explicit
Dim myRng As Range
Dim MyData As DataObject
Dim myString As String
Sub myCopy()
Dim i As Long
Set MyData = New DataObject
i = ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Paragraphs.Count
Set myRng = ActiveDocument.Paragraphs(i).Range
myString = ActiveDocument.Paragraphs(i).Range.ListFormat.ListString _
& " " & myRng
MyData.SetText myString
MyData.PutInClipboard
End Sub
Sub myPaste()
Set myRng = Selection.Range
MyData.GetFromClipboard
myString = MyData.GetText
myRng.Text = myString
End Sub

gmaxey
09-19-2005, 01:18 PM
Actually I don't suppose you even need the myPaste() bit. Just use Edit>Paste or CTRL+v

K1w1
09-19-2005, 02:56 PM
Thanks greg.
I failed to get your original suggestion to work however given the clues I adventured further and came up with

Set oSelect = Selection
sNumber = oSelect.Paragraphs(1).Range.ListFormat.ListString
Which worked just fine.

I can now take my paragraphs directly to XL without the intermediate "paste as text" step previously used.
Steve

gmaxey
09-19-2005, 04:11 PM
Steve. If that works for you then like you said "problem solved."

fumei
09-20-2005, 06:43 AM
A quick question. Exactly what library holds the reference to DataObject? Sometimes I can get it, but sometimes I do not.

TonyJollans
09-20-2005, 08:02 AM
A quick answer :)

Microsoft Forms 2.0 Object Library (C:\Windows\System32\FM20.DLL).

If you have a UserForm in your project it is automatically added to references I think, which would explain why you sometimes get it.

fumei
09-20-2005, 08:26 AM
Ah, thanks Tony. That wee thing has irked me for a while.

MOS MASTER
09-20-2005, 03:29 PM
If you have a UserForm in your project it is automatically added to references I think, which would explain why you sometimes get it.

Indeed Tony, :yes

I use it a lot to clear the clipboard but I ussualy just browse to the DLL to set the reference cause not all of my projects have a UserForm in it. (And I don't like a empty form doing nothing in my project) :whistle: