简体   繁体   中英

VBA - Convert Excel Table to Text in Word

My objective is to convert an Excel table to text

I have copied the range in Excel and then comes the following code...

' =============== ' CONVERT IN WORD ' ===============

Dim wApp As Word.Application
Dim wDoc As Word.Document

Set wApp = CreateObject("word.application")
wApp.Visible = True

Set wDoc = wApp.Documents.Add

With wDoc.Content
    .PasteExcelTable False, False, False
    .Tables(1).Select
    .Rows.ConvertToText Separator:=wdSeparateByDefaultListSeparator, NestedTables:=True
End With

This last line of code fails throwing run time error 5907... There is no table at this location.


This is a macro that was written in Word itself and runs without error after I manually paste into Word but I want to use the above solution as I am also running code on this table in Excel before bringing it into Word and trying to use one routine versus two.

With Selection
    .WholeStory
    .PasteExcelTable False, False, False
    .Tables(1).Range.Select
    .Rows.ConvertToText Separator:=wdSeparateByDefaultListSeparator, _
        NestedTables:=True
End With

Thanks for your help... grinnZ

With wDoc.Content
    .PasteExcelTable False, False, False
    .Tables(1).Rows.ConvertToText _
                Separator:=wdSeparateByDefaultListSeparator, _
                NestedTables:=True
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM