Macro for extracting selected text and comment from Word to Excel on Mac

aminchicago

New Member
Joined
Apr 6, 2024
Messages
3
Office Version
  1. 365
Platform
  1. MacOS
Hi! I am not a developer, but trying to use this video and its comments to install a macro (on a mac) that extracts selected text and its associated comment. The following macro works for comments, but does not include the selected text. My attempts to use other macros found on this site and elsewhere haven't worked at all. Would anyone be able to suggest a line of code that would include the selected text in the macro? I'd really appreciate it!

Sub CopyCommentsToExcel()
'Create in Word vba
Dim xlApp As Object
Dim xlWB As Object
Dim i As Integer
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err Then
Set xlApp = CreateObject("Excel.Application")
End If
On Error GoTo 0
xlApp.Application.Visible = True
Set xlWB = xlApp.Application.Workbooks.Add ' create a new workbook
With xlWB.Worksheets(1)
For i = 1 To ActiveDocument.Comments.Count
.Cells(i, 1).Formula = ActiveDocument.Comments(i).Initial
.Cells(i, 2).Value = ActiveDocument.Comments(i).Range.Text 'Comment Text

Next i
End With
Set xlWB = Nothing
Set xlApp = Nothing
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try inserting the following line of code after Next i
VBA Code:
If Selection.Type = wdSelectionNormal Then .Cells(i, 1).Value = Selection.Text
 
Upvote 0
I greatly appreciate your help, @Tetra201. I tried that and it produces an excel spreadsheet where column A is my initials, and column b is the comment, with no selected text or other content. Here's how I inserted the code - do you see any errors that might be preventing the third column of selected test from forming?

Sub CopyCommentsToExcel()
'Create in Word vba
Dim xlApp As Object
Dim xlWB As Object
Dim i As Integer
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If Err Then
Set xlApp = CreateObject("Excel.Application")
End If
On Error GoTo 0
xlApp.Application.Visible = True
Set xlWB = xlApp.Application.Workbooks.Add ' create a new workbook
With xlWB.Worksheets(1)
For i = 1 To ActiveDocument.Comments.Count
.Cells(i, 1).Formula = ActiveDocument.Comments(i).Initial
.Cells(i, 2).Value = ActiveDocument.Comments(i).Range.Text 'Comment Text

Next i
If Selection.Type = wdSelectionNormal Then .Cells(i, 1).Value = Selection.Text
End With
Set xlWB = Nothing
Set xlApp = Nothing
End Sub
 
Upvote 0
Yes, you inserted the line correctly. Before posting my suggestion, I have tested it to work in Word/Excel for Mac 2011 (14.7.7). It places the selected text from Word to column A in Excel after the last initial.

If you want the selected text from Word to appear in column C and if you have multiple selections, try the following line instead (also tested to work in Word/Excel for Mac 2011):
VBA Code:
If Selection.Type = wdSelectionNormal Then Selection.Copy: .Paste .Cells(1, 3)
 
Upvote 0
Hey @Tetra201, bless you for trying again. Unfortunately this didn't work. I continue to get a spreadsheet with Column A = my initials, Column B = comment text, and nothing in Column C. I am on Microsoft 365, so perhaps that's the difference. I've also tried creating an Apple Script, and I run into this same inability to extract the selected text. Thank you for trying!
 
Upvote 0

Forum statistics

Threads
1,215,200
Messages
6,123,612
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top