PDA

View Full Version : Solved: Extract results from MsgBox



Bernadette
08-20-2012, 06:12 AM
I copy a paragraph or some text and then strikethrough. When I paste the text I would like it to indicate the paragraph number it was copied from. This code gives me the paragraph number but is there a way to extract it as text?

MsgBox Selection.Paragraphs(1).Range.ListFormat.ListString

Thank you!!

Example:

[1] (This paragraph has strikethrough)You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab. Most controls offer a choice of using the look from the current theme or using a format that you specify directly.
[2] To change the overall look of your document, choose new Theme elements on the Page Layout tab. To change the looks available in the Quick Style gallery, use the Change Current Quick Style Set command. Both the Themes gallery and the Quick Styles gallery provide reset commands so that you can always restore the look of your document to the original contained in your current template.
[3] (moved from para. 1) You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab. You can also format text directly by using the other controls on the Home tab. Most controls offer a choice of using the look from the current theme or using a format that you specify directly.

macropod
08-20-2012, 06:34 AM
Perhaps:
Dim StrSrc As String
With Selection
.Start = .Paragraphs(1).Range.Start
.Collapse wdCollapseStart
StrSrc = "(moved from para. " & _
.Paragraphs(1).Range.ListFormat.ListString & ")"
.InsertBefore StrSrc
.Font.Color = wdColorBrightGreen
.Font.Bold = True
End With
before you move the text.

Bernadette
08-21-2012, 04:44 PM
This looks very promising except that I want it to capture the existing paragraph number not the one it is moved to. Thank you so much for your input. I will keep working on it and post the solution if and when I figure it out.

macropod
08-22-2012, 12:55 AM
Hi Bernadette,

This looks very promising except that I want it to capture the existing paragraph number not the one it is moved to.
Umm, that's exactly what it does ...

fumei
08-22-2012, 09:17 PM
Bernadette, you need to execute it
before you move the text.

Bernadette
08-23-2012, 02:39 PM
I need it to copy the existing paragraph then do a strikethrough on the existing paragraph. Then move the insertion point somewhere else in the document and paste it with the note (moved from para ?) and the text in red.

Thanks again for your help.

macropod
08-23-2012, 02:51 PM
Hi Bernadette,

That's quite a different proposition from what you first asked. The code I posted does what you asked for. It's a bit much to expect a macro to handle all you're now asking for, as there is no 'event' to trap the moving of text from one location to another. You really should be using 'track changes' for that; the only limitation being that the text added by my macro (if you decide to use it) will likewise be struck through/coloured by the process.

Frosty
08-23-2012, 02:54 PM
A bit ugly... but try running this routine.

After this, you would need to paste it somewhere. It currently operates on the entire paragraph, rather than the selection. Is that desireable?

Sub DemoCopyAndStrikeThrough()
Dim strSrc As String
Dim rngNew As Range

With Selection
.Start = .Paragraphs(1).Range.Start
.Collapse wdCollapseStart
strSrc = "(moved from para. " & _
.Paragraphs(1).Range.ListFormat.ListString & ")"
.InsertBefore strSrc
.Font.Color = wdColorBrightGreen
.Font.Bold = True
.Paragraphs(1).Range.Copy
Set rngNew = .Paragraphs(1).Range
rngNew.End = rngNew.Start + Len(strSrc)
rngNew.Delete
.Paragraphs(1).Range.Font.StrikeThrough = True
End With
End Sub

Frosty
08-23-2012, 02:57 PM
Whoops, I see you need a bit more criteria for the pasted in text. That's do-able too, but you'd still need to manually paste somewhere after the process of 1) modifying the existing paragraph, 2) copying that existing paragraph, 3) undoing those modifications, 4) formatting the existing paragraph as strike through.

You can't code the 5th step of that process ("paste somewhere else")...

As Paul as indicated-- this functionality really exists in some combination of track changes and the native redlining function (which are slightly different).

What are you actually trying to accomplish? A better way of displaying "moves" in redlining?

Frosty
08-23-2012, 03:04 PM
Sub DemoCopyFirstParagraphWithListSourceInfo()
Dim strSrc As String
Dim rngNew As Range

'operates on the first paragraph in the selection *ONLY*
'get a working range
Set rngNew = Selection.Paragraphs(1).Range
rngNew.Collapse wdCollapseStart

With Selection.Paragraphs(1).Range
'color the whole paragraph
.Font.ColorIndex = wdRed

'get some additional info and put it at the beginning with a different format
With rngNew
strSrc = "(moved from para. " & .ListFormat.ListString & ")"
.Text = strSrc
.Font.Color = wdColorBrightGreen
.Font.Bold = True
End With

'copy our modifications
.Copy

'now reset formatting, apply strikethrough
.Font.ColorIndex = wdAuto
.Font.StrikeThrough = True
'and delete the new text
rngNew.Delete
End With
End Sub


Still have to paste it somewhere manually. Only works on a single paragraph.
EDIT: slightly better comments to follow

macropod
08-23-2012, 04:26 PM
Hi Frosty,

I think the code could be reduced to:
Dim StrSrc As String, Rng As Range
With Selection
Set Rng = .Range
.Font.Color = wdColorRed
.Start = .Words.First.Start
.End = .Words.Last.End
.Collapse wdCollapseStart
StrSrc = "(moved from para. " & _
.Paragraphs(1).Range.ListFormat.ListString & ")"
.InsertBefore StrSrc
.Font.Color = wdColorBrightGreen
.Font.Bold = True
Rng.Start = .Start
Rng.Copy
.Text = vbNullString
Rng.Font.StrikeThrough = True
Rng.Font.ColorIndex = wdAuto
End With
Of course, that still leaves Bernadette with the decision of where to paste the content that's now on the clipboard.

Frosty
08-23-2012, 04:36 PM
Apart from the flavor differences, I like that your code works directly off the selection. But there's also the problem that when you paste it doesn't have any paragraph formatting (i.e., the numbering) if your selection is a couple of sentences in the middle of a paragraph. So if you're not pasting it into an already numbered paragraph, you're left with text which isn't numbered. And if you subsequently apply numbering via a style, you may lose the formatting you actually wanted (red text and green/bold text).

But there are a host of things I might address if I were to make this live to multiple end-users.

Apart from the requirement of having to choose where to paste, I think the design questions that need to be asked are...
1. Operate only on a paragraph which is numbered (.ListString is blank when the paragraph isn't numbered)? What about bulleted paragraphs?
2. Operate on the Selection (the way Paul's does) or operate on an entire paragraph or operate on multiple paragraphs?

Neither your code nor my code really addresses the bigger questions of the appropriate use of such functionality.

But I think the OP will understand, at this point, that what she wants (something that happens when she performs a PASTE) is not possible. But that we can do a lot in the moment just before text is CUT, which I think is the bigger lesson.

macropod
08-23-2012, 04:43 PM
Hi Frosty,

I deliberately chose to work with whatever's selected (whole word minimums) precisely because the user may not want to replicate the paragraph formatting. But, if a whole paragraph is wanted, select it.

I too have serious doubts, which I've already expressed, about the way Bernadette is trying to manage this.

Frosty
08-23-2012, 04:57 PM
Sorry if I wasn't clear-- but I do like your approach. But it can break down if you have multiple partial paragraphs selected (select the middle of one numbered paragraph into the middle of another).

Although... mine didn't add so many operations to the Undo List *grin*

Of course, that's a selection garbage-in-garbage-out problem. But it's something to consider.

I still think we sometimes provide vba solutions to what a poster thinks they want, rather than what they really need (i.e., in this case, perhaps a better understanding of track changes).

macropod
08-23-2012, 05:20 PM
Sorry if I wasn't clear-- but I do like your approach.
Thanks. Actually, I did get that understanding.

But it can break down if you have multiple partial paragraphs selected (select the middle of one numbered paragraph into the middle of another).
True. As you said, "there are a host of things" that might need to be addressed.

I still think we sometimes provide vba solutions to what a poster thinks they want, rather than what they really need (i.e., in this case, perhaps a better understanding of track changes).
Yes, it's always a risk. All too often posters define their problems in terms of a presupposed macro solution. Then there's the doozy's, like one I responded to in another forum, which goes along the lines of "I want a macro to unmerge all the merged cells in a table". Really easy to say, next to impossible to implement, especially when some cells may have been split and others may merely have widths that differ from others in the same column.

Frosty
08-23-2012, 05:23 PM
"Unmerge all the merged cells?"

Haha. Next to impossible? I'd say that's an optimistic analysis. Grin.

macropod
08-23-2012, 05:26 PM
Nah, just split anything that even has a whiff of being a candidate, then leave the user to clean up the gazillion cell mess ...

Bernadette
08-24-2012, 06:19 PM
I will to process all of this information. Thanks so much!!! I realize that I wasn't clear in what I needed. I know I need two separate macros - one to copy and strike and the other to paste. Automatic Track Changes is not an option unfortunately.

macropod
08-24-2012, 07:59 PM
Hi Bernadette,

I don't believe you will need two macros. The code does all that can be done and leaves the paragraph to be moved sitting on Word's clipboard. All you need to do is to select the destination and choose 'Paste'.

fumei
08-24-2012, 11:24 PM
Nah, just split anything that even has a whiff of being a candidate

Seems to me that this would cause quite a stink...

Bernadette
08-25-2012, 08:58 AM
This is exactly what I needed it to do! Amazing! Thanks so much.

Bernadette
08-27-2012, 06:09 AM
This is the code I used:

Sub CopyAndStrikeThrough()
'
Dim StrSrc As String, Rng As Range
With Selection
Set Rng = .Range
.Font.Color = wdColorRed
.Start = .Words.First.Start
.End = .Words.Last.End
.Collapse wdCollapseStart
StrSrc = "(moved from para. " & _
.Paragraphs(1).Range.ListFormat.ListString & ") "
.InsertBefore StrSrc
.Font.Color = 5287936 'Dark Green
.Font.Bold = True
Rng.Start = .Start
Rng.Copy
.Text = vbNullString
Rng.Font.StrikeThrough = True
Rng.Font.ColorIndex = wdAuto
End With

End Sub

Thanks again!!!