PDA

View Full Version : Solved: Manual Track Changes Macro



Bernadette
09-18-2012, 10:55 AM
Hello again,

I recently posted a thread asking for help with a macro. The code worked beautifully but now there is a change in the requirement. When the text is pasted a green bold bracket needs to appear after the pasted text.

example:
[2] 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. (moved from para. [1]) You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks.) You can also format text directly by using the other controls on the Home tab.

Here is the code from the previous macro:

Sub CopyAndStrikeThrough()

Dim StrSrc As String, Rng As Range

With Selection
Set Rng = .Range
.Start = .Words.First.Start
.End = .Words.Last.End
.Collapse wdCollapseStart
StrSrc = "(moved from para. " & _
.Paragraphs(1).Range.ListFormat.ListString & ") "
.InsertBefore StrSrc 'inserts the specified text before selected text
.Font.Color = 5287936 'dark green colour
.Font.Bold = True
Rng.Start = .Start
Rng.Copy
.Text = vbNullString
Rng.Font.Strikethrough = True
Rng.Font.ColorIndex = wdAuto
End With

End Sub

Thank you so much for your help!
Bernie

Frosty
09-18-2012, 11:03 AM
you just need to manually change the StrSrc variable to include your brackets. Right now it says
StrSrc = "(moved from para. " & .Paragraphs(1).Range.ListFormat.ListString & ")"

It has a line break in there, for readability, but that doesn't matter to the code.

Since you know that turns into
(moved from para. 1)
where do you think you would put the brackets in that line of code?

I'm not trying to be difficult, but this is something you should be able to work out on your own. I want to help you learn this stuff.

Bernadette
09-18-2012, 03:24 PM
I spent two hours putting that bracket in different places but it never ended up at the end of the selected text. I haven't given up! I will keep trying. Thanks for your help and encouragement.

Frosty
09-18-2012, 03:54 PM
If you've spent two hours... then try this...
StrSrc = "(moved from para. [" & .Paragraphs(1).Range.ListFormat.ListString & "])"

If that's not the answer you're seeking, then I think you'll need to reframe the question.

Bernadette
09-18-2012, 04:38 PM
Hello Frosty,

I need the bracket to appear at the end of the text that is pasted elsewhere in the document.


[2] You can easily change the formatting of selected text in the documenttext by choosing a look for the selected text from the Quick Styles gallery onthe Home tab. (moved from para. [1] You can use these galleres to insert tables, headers,footers, lists, cover pages, and other document building blocks.) You can also format text directly by using the other controls on the Home tab.

Frosty
09-18-2012, 04:45 PM
Oh, well that's a bit different than you originally stated in your original post.

That's a bit trickier, since your original code simply deals with inserting text before your selection, and now you need to both insert text before and insert text after.

A lot of different ways to approach this, but since it's only one character... does this work?

Sub CopyAndStrikeThrough()

Dim StrSrc As String, Rng As Range

With Selection
Set Rng = .Range
.Start = .Words.First.Start
.End = .Words.Last.End
.Collapse wdCollapseStart
StrSrc = "(moved from para. [" & _
.Paragraphs(1).Range.ListFormat.ListString & "] "
.InsertBefore StrSrc 'inserts the specified text before selected text
.Font.Color = 5287936 'dark green colour
.Font.Bold = True
Rng.Start = .Start
Rng.InsertAfter ")"
With Rng.Characters.Last
.Font.Color = 5287936 'dark green colour
.Font.Bold = True
End With
Rng.Copy
Rng.Characters.Last.Delete
.Text = vbNullString
Rng.Font.StrikeThrough = True
Rng.Font.ColorIndex = wdAuto
End With

End Sub


Remember to use VBA code tags... it makes your posted code much easier to read...

Frosty
09-18-2012, 04:47 PM
Hmm, my code is not quite right, as I now see you actually did have a green close paren in your original post.

Is that what you want?
(moved from para [1]) yada yada yada text )

This creating a dangling close paren... but you can do with it whatever you want...

Bernadette
09-18-2012, 04:50 PM
That is exactly what I needed! THANK YOU!