Lisa said:
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/default.asp?url=/library/en-us/vbawd10/html/womthAddOLEObject.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