473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using vbnet to link excel objects to word document

Hi -
I'm able to open excel workbooks and word documents, but I can't seem to
copy excel charts, named ranges, etc. to a word document. Anyone know of
good reference material in this area? What little documentation I've been
able to find focuses on using only one office app at a time.

Thanks for your help
Nov 20 '05 #1
4 3228
> I'm able to open excel workbooks and word documents, but I can't seem to
copy excel charts, named ranges, etc. to a word document.


Can I see the code you are using to copy the excel object to the clipboard
please?

It is very difficult to diagnose without this.
Nov 20 '05 #2
Lisa wrote:
I'm able to open excel workbooks and word documents, but I can't seem to
copy excel charts, named ranges, etc. to a word document. Anyone know of
good reference material in this area? What little documentation I've been
able to find focuses on using only one office app at a time.


Hi Lisa,

this is the minimal code to insert an Excel-Sheet into a Word-Document:

'set a reference to the Word xx.0 Object Library
Imports wd = Microsoft.Office.Interop.Word
Imports xl = Microsoft.Office.Interop.Excel

'Start Word
Dim wdApp As New wd.ApplicationClass
'Add an empty document
Dim wdDoc As wd.Document = wdApp.Documents.Add()
'Make Work visible
wdApp.Visible = True
'ClassType of an Excel-Sheet. Can of course also be a diagram,
'a chart, a sound-file, whatever supports OLE
Dim ClassType As Object = "Excel.Sheet.8"
'Location of the file
Dim FileName As Object = "C:\test.xls"
'Embed, not link
Dim LinkToFile As Object = False
'Add OleObject
wdDoc.InlineShapes.AddOLEObject(ClassType, FileName, LinkToFile)

[Reference: VBA]
http://msdn.microsoft.com/library/de...dOLEObject.asp

To edit embedded objects again you can use the following code:

'let's say our Excel-Sheet is the first Object in the document
Dim ole as wd.OLEFormat = doc.InlineShapes(1).OLEFormat
Dim progID as String = ole.ProgID
'it won't work without activating the Ole-Object first!!!
ole.Activate()
'just for security reasons. One can leave this away
If progID = "Excel.Sheet.8" Then
'cast the Ole-Object to an Excel.Workkook-Object
Dim wbk as xl.Workbook = CType(ole.Object, xl.Workbook)
'get a reference to the first sheet
Dim sht as xl.Worksheet = CType(wbk.Worksheets[1], xl.Worksheet)
'get Cell "A1"
xl.Range rng = CType(sht.get_Range("A1", "A1"), xl.Range)
'show the value of "A1"
MessageBox.Show(rng.Value2.ToString())
End If

Cheers

Arne Janning
Nov 20 '05 #3
Dim EApp As New Excel.Application()
Dim wa As New Word.Application()
Dim source As String
Dim target As String

Private Sub GenerateTheReport()
Dim doc As New Word.Document()
Dim r As Excel.Range
Dim ws As New Excel.Worksheet()

EApp.Workbooks.Open(source)
EApp.AskToUpdateLinks = False
doc = wa.Documents.Add

Try
ws = CType(EApp.Worksheets("Blue info"), Excel.Worksheet)
'"Table_Blue" is a named range in 'Blue info' worksheet
'='Blue info'!$A$2:$B$12
r = CType(ws.Range("Table_Blue"), Excel.Range)
doc.Content.PasteSpecial(, r) <=========== Error here
"Type mismatch"
doc.SaveAs(target)
Catch
MsgBox(Err.Description)
Finally
wa.Documents.Close()
EApp.Application.Quit()
End Try
End Sub

I get an exception when trying to PasteSpecial the range r. I know the
reference is valid. Any thoughts?
"scorpion53061" <sc************@nospamhereeveryahoo.com> wrote in message
news:OA****************@TK2MSFTNGP09.phx.gbl...
I'm able to open excel workbooks and word documents, but I can't seem to
copy excel charts, named ranges, etc. to a word document.


Can I see the code you are using to copy the excel object to the clipboard
please?

It is very difficult to diagnose without this.

Nov 20 '05 #4
Lisa,

This will take some time for me to port the necessary vba code to .NET to
acomplish this.

Please check Arne's response and see if it works for you. If not, email me
at ad***@nospamkjmsolutions.com (remove no spam) and let me know this issue
is not resolved and I will write you up a project to deal with this issue.

"Lisa" <sp****@herplace.com> wrote in message
news:x4********************@eatel.net...
Dim EApp As New Excel.Application()
Dim wa As New Word.Application()
Dim source As String
Dim target As String

Private Sub GenerateTheReport()
Dim doc As New Word.Document()
Dim r As Excel.Range
Dim ws As New Excel.Worksheet()

EApp.Workbooks.Open(source)
EApp.AskToUpdateLinks = False
doc = wa.Documents.Add

Try
ws = CType(EApp.Worksheets("Blue info"), Excel.Worksheet)
'"Table_Blue" is a named range in 'Blue info' worksheet
'='Blue info'!$A$2:$B$12
r = CType(ws.Range("Table_Blue"), Excel.Range)
doc.Content.PasteSpecial(, r) <=========== Error here
"Type mismatch"
doc.SaveAs(target)
Catch
MsgBox(Err.Description)
Finally
wa.Documents.Close()
EApp.Application.Quit()
End Try
End Sub

I get an exception when trying to PasteSpecial the range r. I know the
reference is valid. Any thoughts?
"scorpion53061" <sc************@nospamhereeveryahoo.com> wrote in message
news:OA****************@TK2MSFTNGP09.phx.gbl...
I'm able to open excel workbooks and word documents, but I can't seem to copy excel charts, named ranges, etc. to a word document.


Can I see the code you are using to copy the excel object to the clipboard please?

It is very difficult to diagnose without this.


Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Pawel | last post by:
Hello All I tried to embed Excel sheet into HTML page by following command: <iframe src="Tmp.XLS" width="100%" height="500"></iframe> and it works, showing Excel sheet inside the page, but...
1
by: Francois Gagnon | last post by:
Hi, I have the following scenario. THe side is built using 2 frames. The top frame is a menu where the bottom one is the content. The top frame allows th user to select what document he wants...
8
by: W. de Jonge | last post by:
Who can help me? I want to create a link(href) which opens an .doc or an ..xls directly in MS Word or MS Excell and not in IE so that I don't have to save the document first en open it from...
4
by: Chris | last post by:
Hi, everything works apart from the last line :-(( rng.Value2.ToString() An exception is thrown : "Old format or invalid type library" It gets compiled though (so he recognizes the property...
1
by: musicloverlch | last post by:
I have a problem. I have an Access 2003 report that contains a number of Word and Excel objects with fancy formatting and spreadsheets, etc. I have about 20 records that I am having the database...
1
by: Cloud | last post by:
Hi all, I have a parent page(parent.aspx) which will open different new windows depending on the user input. for example, the new windows are opened using the following code :...
1
by: webgirl | last post by:
Hi everyone, I have a weird problem with some Word/Excel automation code that I run from Access (not sure if I should therefore post this in the Access forum..? Thought I'd try here first) ...
2
by: =?Utf-8?B?UFM=?= | last post by:
Hi, Is there a way to compare PDF, Word Document, Excel using C# like WinDiff utility that compares text file and display the result with the modified text in both the document? -- Thanks,...
2
by: Iain Wilson | last post by:
Hi Mike I have changed the code using Server.MapPath but to no avail. Prints the Adobe PDF document to the default local printer (I am running this localhost). I Have not run it on the server...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.