Inserting Excel file data to a word document using VB.NET
Hi Friends,
I have tried inserting an excel file to a word document.But the following code is inserting the junk information. So can you please help me out?
Dim m_WordApp As New Word.Application
Dim m_WordDoc As Word.Document
m_WordDoc = m_WordApp.Documents.Open(CObj("C:\Documents and Settings\santu\My Documents\New Microsoft Word Document.doc"), CObj(False), CObj(False), CObj(False))
Dim fullpath As String = "C:\Documents and Settings\santu\My Documents\Certification Details.xls"
m_WordDoc.Application.Selection.EndKey(WdUnits.wdS tory)
m_WordDoc.Application.Selection.InsertBreak(WdBrea kType.wdPageBreak)
m_WordDoc.Application.Selection.InsertFile(fullpat h)
m_WordDoc.Application.Selection.InsertParagraphAft er()
Even i have tried using this also...
m_WordDoc.Range.InsertParagraphAfter()
m_WordDoc.Range.InsertFile(fullpath, , 0, , )
So can you please help me out?
Re: Inserting Excel file data to a word document using VB.NET
What are you actually trying to do? Insert the entire actual file into your word document?
Re: Inserting Excel file data to a word document using VB.NET
Yes,
I want to insert all the data of the excel file to a word document. When I am using the same for inserting a word doc to another word doc it is working fine. But with excel i am getting this problem.
Thanks.
Re: Inserting Excel file data to a word document using VB.NET
Ok, is your code in Excel or in Word? Where is your originating/source for automating?
Re: Inserting Excel file data to a word document using VB.NET
My code needs to insert excel sheet information to a word document which i have some content already. I want all the cell content of the excel sheet to be copied.
I am writing the code for word.
Re: Inserting Excel file data to a word document using VB.NET
Ok and also since you are using .net you dont want to do this inside of Word in the documents VBA code?
Re: Inserting Excel file data to a word document using VB.NET
Yes, I dont want to do this in Word but using VB.NET.....
When I use m_WordDoc.Shapes.AddOLEObject(, fullpath) it is inserting correctly but in the top of the document. So when I use this statement multiple times, it is just overlapping all the content. (One over the other).
Re: Inserting Excel file data to a word document using VB.NET
Can please some one answer to my post.... This is urgent for me
Many Thanks.
Re: Inserting Excel file data to a word document using VB.NET
Use the Range field of the AddOLEObject object:
AddOLEObject Method
Creates an OLE object. Returns the InlineShape object that represents the new OLE object.
expression.AddOLEObject(ClassType, FileName, LinkToFile, DisplayAsIcon, IconFileName, IconIndex, IconLabel, Range)
expression Required. An expression that returns a InlineShapes object.
ClassType Optional Variant. The name of the application used to activate the specified OLE object.
FileName Optional Variant. The file from which the object is to be created. If this argument is omitted, the current folder is used. You must specify either the ClassType or FileName argument for the object, but not both.
LinkToFile Optional Variant. True to link the OLE object to the file from which it was created. False to make the OLE object an independent copy of the file. If you specified a value for ClassType, the LinkToFile argument must be False. The default value is False.
DisplayAsIcon Optional Variant. True to display the OLE object as an icon. The default value is False.
IconFileName Optional Variant. The file that contains the icon to be displayed.
IconIndex Optional Variant. The index number of the icon within IconFileName. The order of icons in the specified file corresponds to the order in which the icons appear in the Change Icon dialog box (Insert menu, Object dialog box) when the Display as icon check box is selected. The first icon in the file has the index number 0 (zero). If an icon with the given index number doesn't exist in IconFileName, the icon with the index number 1 (the second icon in the file) is used. The default value is 0 (zero).
IconLabel Optional Variant. A label (caption) to be displayed beneath the icon.
Range Optional Variant. The range where the OLE object will be placed in the text. The OLE object replaces the range, unless the range is collapsed. If this argument is omitted, the object is placed automatically.
Here's an example:
ActiveDocument.InlineShapes.AddOLEObject _
ClassType:="Excel.Sheet", DisplayAsIcon:=False, _
Range:=ActiveDocument.Paragraphs(2).Range
Use the Paragraphs collection to select the location you want the chart...
Re: Inserting Excel file data to a word document using VB.NET
Hi Quadlet,
thanks for your explanation. I have one more doubt in this. I am generating a very large document (may extend upto 800 pages or more than that). So in that case we may not provide the paragraph number.
after opening the Word document where i have printed excel content, it is showing me a message " The object was created in Excel. this application is not available to open this object". Do I need to include the Excel dll?
Thanks.
Re: Inserting Excel file data to a word document using VB.NET
Hi Quadlet,
I got the thing correctly. Many Thanks for your help.