import Excel data into Word tabel

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

import Excel data into Word tabel

Post by Robie »

Hi

I need to create a Word macro which can import Excel data into Word. I created something using MailMerge before (which was bit more complicated than this one) but in this case it is a simple import.
Basically, take the Excel data and copy it to Word table. I don't know if this is possible but thought I would just ask.

The Excel file is basically 7 columns by n rows. Each column has a heading.
52.png
Thanks.
Robie
You do not have the required permissions to view the files attached to this post.

User avatar
StuartR
Administrator
Posts: 12619
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: import Excel data into Word tabel

Post by StuartR »

Simple copy and paste usually works well enough for me. Copy the data in Excel and then paste into Word...
You do not have the required permissions to view the files attached to this post.
StuartR


Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: import Excel data into Word tabel

Post by Robie »

StuartR wrote:Simple copy and paste usually works well enough for me. Copy the data in Excel and then paste into Word...
Sorry, I should have made is clear my opening post. :sad:

I need to create a VBA in Word to import from a selected Excel spreadsheet. Then there are other things I have to do with the data which are still being discussed.
All my users want is to click a button, specify a file and get the data into Word. They are that simple.

User avatar
HansV
Administrator
Posts: 78589
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: import Excel data into Word tabel

Post by HansV »

You'll have to specify precisely what you want to import.
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: import Excel data into Word tabel

Post by Robie »

HansV wrote:You'll have to specify precisely what you want to import.
Thanks Hans.

Quite simply import columns 1-7 for the rows specified (the number of rows would be different for each spreadsheet). That's it really.

User avatar
HansV
Administrator
Posts: 78589
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: import Excel data into Word tabel

Post by HansV »

What exactly do you mean by "the rows specified"?
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: import Excel data into Word tabel

Post by Robie »

HansV wrote:What exactly do you mean by "the rows specified"?
The number of rows in the spreadsheet.

User avatar
HansV
Administrator
Posts: 78589
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: import Excel data into Word tabel

Post by HansV »

Which worksheet do you want to use? The active sheet in the active workbook? Or ...?
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: import Excel data into Word tabel

Post by Robie »

HansV wrote:Which worksheet do you want to use? The active sheet in the active workbook? Or ...?
The active worksheet please Hans. Thanks.

User avatar
HansV
Administrator
Posts: 78589
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: import Excel data into Word tabel

Post by HansV »

Here you go:

Code: Select all

Sub ImportFromXL()
    Dim app As Object
    Dim wsh As Object
    Dim rng As Object
    Dim m As Long
    On Error GoTo ErrHandler
    Set app = GetObject(Class:="Excel.Application")
    Set wsh = app.Activesheet
    m = wsh.Range("A:G").Find(What:="*", SearchOrder:=1, SearchDirection:=2).Row
    wsh.Range("A1:G" & m).Copy
    Selection.Paste
ExitHandler:
    Set rng = Nothing
    Set wsh = Nothing
    Set app = Nothing
    Exit Sub
ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
End Sub
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: import Excel data into Word tabel

Post by Robie »

HansV wrote:Here you go:

Code: Select all

Sub ImportFromXL()
    Dim app As Object
    Dim wsh As Object
    Dim rng As Object
    Dim m As Long
    On Error GoTo ErrHandler
    Set app = GetObject(Class:="Excel.Application")
    Set wsh = app.Activesheet
    m = wsh.Range("A:G").Find(What:="*", SearchOrder:=1, SearchDirection:=2).Row
    wsh.Range("A1:G" & m).Copy
    Selection.Paste
ExitHandler:
    Set rng = Nothing
    Set wsh = Nothing
    Set app = Nothing
    Exit Sub
ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
End Sub
That's perfect Hans. Thank you so much. I will try it out later today but again thank you so much for your help. :cheers:

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: import Excel data into Word tabel

Post by Robie »

HansV wrote:Here you go:

Code: Select all

Sub ImportFromXL()
    Dim app As Object
    Dim wsh As Object
    Dim rng As Object
    Dim m As Long
    On Error GoTo ErrHandler
    Set app = GetObject(Class:="Excel.Application")
    Set wsh = app.Activesheet
    m = wsh.Range("A:G").Find(What:="*", SearchOrder:=1, SearchDirection:=2).Row
    wsh.Range("A1:G" & m).Copy
    Selection.Paste
ExitHandler:
    Set rng = Nothing
    Set wsh = Nothing
    Set app = Nothing
    Exit Sub
ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
End Sub
Hi Hans

I went with your code and it works great. Thanks. :)

But, there is one user (there always is one) who gets an error 'ActiveX can't create object' error. There is no error number. Before, I do further investigation, it there something obvious at which I should look?

User avatar
HansV
Administrator
Posts: 78589
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: import Excel data into Word tabel

Post by HansV »

The most probable explanation is that the user hasn't started Excel when the macro is called. The macro will only work correctly if Excel has been started, a workbook has been opened or created, and a worksheet is active (not a chart sheet).
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: import Excel data into Word tabel

Post by Robie »

HansV wrote:The most probable explanation is that the user hasn't started Excel when the macro is called. The macro will only work correctly if Excel has been started, a workbook has been opened or created, and a worksheet is active (not a chart sheet).
Thanks Hans. That is exactly what it was. They are happy now.

Thank you so much. :cheers: :clapping: