Excel Macro Controlling Word - Run-time Error 509

lmasala

New Member
Joined
Jul 25, 2011
Messages
11
I've got text in a Word document that needs to be turned into a table and then the second column of the table pasted into Excel. I'm doing all of this through a macro in Excel.

I run into a problem in red section. "sel.WholeStory" does highlight all of the text in Word. But for some reason it doesn't seem to recognize this. At the next line I get this error: "Run-time error 509: The TextToTable command is not available because no text is selected." But it is all selected! I've tried deselecting and reselecting again and that does no good. I've tried adding references to the Word document, even though I think "WordBasic" should be sufficient. The code works in Word.

Any ideas?

Rich (BB code):
Sub SSConvertToTable()


Dim wd As Object
Set wd = CreateObject("Word.Application")
wd.Visible = True
Dim doc As Object
Set doc = wd.Documents.Open("H:\SS_January31.docx")
Dim sel As Object
Set sel = wd.Selection

   ' In Word - Format text for table
     'This part adds appropriate delimiters so the text can be 
      split into a two column table   
        
    ' In Word - Turn to table
    sel.WholeStory
    WordBasic.TextToTable ConvertFrom:=3, NumColumns:=2 
Thank you for any help.


(Word and Excel both 2007)
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I think it definitely has to be a problem with Text To Table and not with the selecting. If I change that last bit to:
Code:
sel.WholeStory
sel.Copy
Columns("A").Select
ActiveSheet.Paste
all of the text from Word copies over to Excel. Any ideas of how to fix my TextToTable?
 
Upvote 0
In case anyone else has this issue, the below code seems to fix whatever the problem is.

Code:
sel.WholeStory
sel.ConvertToTable
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

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