Skip to main content

Visual Studio LightSwitch - Exporting Data to Word using COM Sample

This code sample shows how to export data from a Visual Studio LightSwitch desktop application into a Word Document.  This download contains the sample application for the MSDN blog post: "How Do I: Export Data to a Word Mailing Labels Document".

VB.NET (2.2 MB)
5 Star
3,237 times
Add to favorites
E-mail Twitter Digg Facebook
Sign in to ask a question

  • C# example
    1 Posts | Last post November 03, 2011
    • Can you post the C# sample?  I can only see the sample on this site.
  • I am Getting the below error when running the Code.
    5 Posts | Last post August 20, 2011
    •     Dim mailingLabelDoc = wordObject.MailingLabel.CreateNewDocument(labelName)
      Exception from HRESULT: 0x800A16D3
      {System.Runtime.InteropServices.COMException (0x800A16D3): Exception from HRESULT: 0x800A16D3 ---> MS.Internal.ComAutomation.ComAutomationObjectException: One of the values passed to this method or property is out of range." & vbCrLf & "(Source=Microsoft Word)" & vbCrLf & "(HelpFile=C:\Program Files\Microsoft Office\Office12\1033\WDMAIN11.CHM#25323)" & vbCrLf & "   at MS.Internal.ComAutomation.ComAutomationNative.CheckInvokeHResult(UInt32 hr, String memberName, String exceptionSource, String exceptionDescription, String exceptionHelpFile, UInt32 exceptionHelpContext)" & vbCrLf & "   at MS.Internal.ComAutomation.ComAutomationNative.Invoke(Boolean tryInvoke, String memberName, ComAutomationInvokeType invokeType, ComAutomationInteropValue[] rgParams, IntPtr nativePeer, ComAutomationInteropValue& returnValue)" & vbCrLf & "   at MS.Internal.ComAutomation.ComAutomationObject.InvokeImpl(Boolean tryInvoke, String name, ComAutomationInvokeType invokeType, Object& returnValue, Object[] args)" & vbCrLf & "   at MS.Internal.ComAutomation.ComAutomationObject.Invoke(String name, ComAutomationInvokeType invokeType, Object[] args
    • I addressed some issues with the code files, so perhaps downloading the zip file again will address your problem.
      But it may also be that you do not have MSFT Word installed, which is necessary for this example (since the application exports data to Word).  Or it could be that you have an older version of Word which is also causing the issue.  I used Word 2010.
    • Great example Matt - thanks for creating it
      I had the same problem as Dwarapudi - I am using Word 2007
      I needed to use the labels from within word - once just to set them up - selected 8660 as the default label
      Then I specified the "8660" as the name of the label in the createnewdocument line
       Dim mailingLabelDoc = wordObject.MailingLabel.CreateNewDocument("8660")
      Then change saveas2 to saveas
      Otherwise you get a missing method exception presumably saveas2 is new in word 2010
      After that it works fine
      A small enhancement I made was to add the filter to the saveasfieldialog such as 
            dialog.Filter = "Docs|*.docx" to get the default file type
    • Same error of Dwarapudi..
      Changing "saveas2" to "saveas" it work but I got an error opening Doc file... No idea..
    • I have Office Word 2007. Changing the default file type do not resolve the problem.
  • Excel Format
    1 Posts | Last post June 28, 2011
    • Is it possible to edit this code for exporting to Excel template??
      Thank you