Results 1 to 11 of 11

Thread: Inserting Excel file data to a word document using VB.NET

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2007
    Posts
    22

    Angry 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?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2007
    Posts
    22

    Angry 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.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2007
    Posts
    22

    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.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2007
    Posts
    22

    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).

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2007
    Posts
    22

    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.

  9. #9
    New Member
    Join Date
    Jul 2007
    Posts
    1

    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...

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jul 2007
    Posts
    22

    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.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2007
    Posts
    22

    Re: Inserting Excel file data to a word document using VB.NET

    Hi Quadlet,
    I got the thing correctly. Many Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width