Copying the result of a macro to the Clipboard

J

John Doue

Thanks to the kind of Helmut and Greg, I finally got the following macro

Sub MotsTect()
Dim lCnt As Long
Dim wCnt As Long
Dim rDcm As Range
Dim Mots As Long
wCnt = ActiveDocument.ComputeStatistics(Statistic:=wdStatisticWords,
IncludeFootnotesAndEndnotes:=True)
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "'"
.Replacement.Text = "'"
While .Execute
lCnt = lCnt + 1
Wend
rDcm.Start = ActiveDocument.Range.Start
.Execute Replace:=wdReplaceAll
End With
MsgBox "There are " & wCnt & " words in the document and " & lCnt & "
replacements. For a total count of " & lCnt + wCnt
End Sub

to do what I wanted. Now, I would like to improve on it and write a
variant that would copy the resulting content of the message box to the
clipboard as the same time it displays it.

I have no idea how to do that and some further help would be greatly
appreciated.

Best regards
 
G

Greg Maxey

John,

Try:

Sub MotsTect()
Dim lCnt As Long
Dim wCnt As Long
Dim rDcm As Range
Dim Mots As Long
Dim myData As DataObject
Dim strClip As String
'In the VBA editor, go to Tools, References, and set a reference
'to the "Microsoft Forms 2.0 Object Library"
wCnt = ActiveDocument.ComputeStatistics(Statistic:=wdStatisticWords, _
IncludeFootnotesAndEndnotes:=True)
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "'"
.Replacement.Text = "'"
While .Execute
lCnt = lCnt + 1
Wend
rDcm.Start = ActiveDocument.Range.Start
.Execute Replace:=wdReplaceAll
End With
strClip = "There are " & wCnt & " words in the document and " _
& lCnt & " replacements. For a total count of " & lCnt + wCnt
MsgBox strClip
Set myData = New DataObject
myData.SetText strClip
myData.PutInClipboard
End Sub
 
J

John Doue

Greg said:
John,

Try:

Sub MotsTect()
Dim lCnt As Long
Dim wCnt As Long
Dim rDcm As Range
Dim Mots As Long
Dim myData As DataObject
Dim strClip As String
'In the VBA editor, go to Tools, References, and set a reference
'to the "Microsoft Forms 2.0 Object Library"
wCnt = ActiveDocument.ComputeStatistics(Statistic:=wdStatisticWords, _
IncludeFootnotesAndEndnotes:=True)
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "'"
.Replacement.Text = "'"
While .Execute
lCnt = lCnt + 1
Wend
rDcm.Start = ActiveDocument.Range.Start
.Execute Replace:=wdReplaceAll
End With
strClip = "There are " & wCnt & " words in the document and " _
& lCnt & " replacements. For a total count of " & lCnt + wCnt
MsgBox strClip
Set myData = New DataObject
myData.SetText strClip
myData.PutInClipboard
End Sub
I am very grateful to you, this works perfectly for me.

Best regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top