import Excel data into Word tabel
-
- 5StarLounger
- Posts: 656
- Joined: 18 Feb 2010, 14:26
import Excel data into Word tabel
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. Thanks.
Robie
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. Thanks.
Robie
You do not have the required permissions to view the files attached to this post.
-
- Administrator
- Posts: 12619
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: import Excel data into Word tabel
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
-
- 5StarLounger
- Posts: 656
- Joined: 18 Feb 2010, 14:26
Re: import Excel data into Word tabel
Sorry, I should have made is clear my opening post.StuartR wrote:Simple copy and paste usually works well enough for me. Copy the data in Excel and then paste into Word...
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.
-
- Administrator
- Posts: 78589
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: import Excel data into Word tabel
You'll have to specify precisely what you want to import.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 656
- Joined: 18 Feb 2010, 14:26
Re: import Excel data into Word tabel
Thanks Hans.HansV wrote:You'll have to specify precisely what you want to import.
Quite simply import columns 1-7 for the rows specified (the number of rows would be different for each spreadsheet). That's it really.
-
- Administrator
- Posts: 78589
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: import Excel data into Word tabel
What exactly do you mean by "the rows specified"?
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 656
- Joined: 18 Feb 2010, 14:26
Re: import Excel data into Word tabel
The number of rows in the spreadsheet.HansV wrote:What exactly do you mean by "the rows specified"?
-
- Administrator
- Posts: 78589
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: import Excel data into Word tabel
Which worksheet do you want to use? The active sheet in the active workbook? Or ...?
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 656
- Joined: 18 Feb 2010, 14:26
Re: import Excel data into Word tabel
The active worksheet please Hans. Thanks.HansV wrote:Which worksheet do you want to use? The active sheet in the active workbook? Or ...?
-
- Administrator
- Posts: 78589
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: import Excel data into Word tabel
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
Hans
-
- 5StarLounger
- Posts: 656
- Joined: 18 Feb 2010, 14:26
Re: import Excel data into Word tabel
That's perfect Hans. Thank you so much. I will try it out later today but again thank you so much for your help.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
-
- 5StarLounger
- Posts: 656
- Joined: 18 Feb 2010, 14:26
Re: import Excel data into Word tabel
Hi HansHansV 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
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?
-
- Administrator
- Posts: 78589
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: import Excel data into Word tabel
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
Hans
-
- 5StarLounger
- Posts: 656
- Joined: 18 Feb 2010, 14:26
Re: import Excel data into Word tabel
Thanks Hans. That is exactly what it was. They are happy now.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).
Thank you so much.