none
Using Visual Studio 2015 -- Do I still need "Imports Word = Microsoft.Office.Interop.Word"?? RRS feed

  • Question

  • I recently started using Visual Studio 2015, which is irritating me because there appear to be some major differences from Visual Studio 2010.

    What is the equivalent of this line of code in Visual Studio 2015? --

    Imports Word = Microsoft.Office.Interop.Word

    I'm trying to port some code that I wrote for Visual Studio 2010 to 2015 and it's not working, I believe mainly because of the syntax error that I'm getting with the line of code above.

    I also need to know how to get these 2 lines of code to work using Visual Studio 2015 --

    Dim Doc As Word.Document

    Set Doc = Word.Application.Documents.Add(Template:="<name of document>")

    Thanks in advance,

    Jim


    James Hutchinson

    Monday, February 15, 2016 3:38 AM

Answers

  • Hello James,

    It doesn't matter what Visual Studio version is used. Most probably you just need to add a reference to the interop assembly where the Microsoft.Office.Interop.Word namespace is defined.

    You may find the How to automate Word from Visual Basic .NET to create a new document article helpful.

    Monday, February 15, 2016 4:41 PM
  • Hi Jim,

    >>Yes you need to add the reference to the Microsoft.Office.Interop.Word

    As the reply from Eugene, whatever version of Visualstudio  you used syntax will be the same. There is no change in the syntax.

    >>You have mention that you getting an error in following lines.

    Its beacause of syntax error. You can use lines below I have posted and tested working correctly on my side.

    Dim Doc As Word.Document 
    Doc = Application.Documents.Add(Template:="C:\Users\Administrator\Desktop\hello.docx")

    Regards

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by jhutch03 Tuesday, February 16, 2016 6:42 AM
    Tuesday, February 16, 2016 2:24 AM
  • Eugene,

    Thanks for the help.  I've got it now.  Here's what I did using Visual Studio 2015:

    1)  On the Project menu, select Add Reference

    2)  Under COM, select "Microsoft Word 14.0 Object Library"

    3)  Click the OK button.

    "Imports Word = Microsoft.Office.Interop.Word" no longer generates a syntax error.

    Thanks,

    Jim


    James Hutchinson

    • Marked as answer by jhutch03 Tuesday, February 16, 2016 6:44 AM
    Tuesday, February 16, 2016 5:12 AM
  • Edward,

    Thank you.  This code worked for me:

     Dim wordApp As New Word.Application
     wordApp.Visible = True
     Dim doc As Word.Document = wordApp.Documents.Add(Template:="C:\<file path & file name>")


    James Hutchinson

    • Marked as answer by jhutch03 Tuesday, February 16, 2016 6:44 AM
    Tuesday, February 16, 2016 6:42 AM

All replies

  • Hello James,

    It doesn't matter what Visual Studio version is used. Most probably you just need to add a reference to the interop assembly where the Microsoft.Office.Interop.Word namespace is defined.

    You may find the How to automate Word from Visual Basic .NET to create a new document article helpful.

    Monday, February 15, 2016 4:41 PM
  • Hi Jim,

    >>Yes you need to add the reference to the Microsoft.Office.Interop.Word

    As the reply from Eugene, whatever version of Visualstudio  you used syntax will be the same. There is no change in the syntax.

    >>You have mention that you getting an error in following lines.

    Its beacause of syntax error. You can use lines below I have posted and tested working correctly on my side.

    Dim Doc As Word.Document 
    Doc = Application.Documents.Add(Template:="C:\Users\Administrator\Desktop\hello.docx")

    Regards

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Marked as answer by jhutch03 Tuesday, February 16, 2016 6:42 AM
    Tuesday, February 16, 2016 2:24 AM
  • Eugene,

    Thanks for the help.  I've got it now.  Here's what I did using Visual Studio 2015:

    1)  On the Project menu, select Add Reference

    2)  Under COM, select "Microsoft Word 14.0 Object Library"

    3)  Click the OK button.

    "Imports Word = Microsoft.Office.Interop.Word" no longer generates a syntax error.

    Thanks,

    Jim


    James Hutchinson

    • Marked as answer by jhutch03 Tuesday, February 16, 2016 6:44 AM
    Tuesday, February 16, 2016 5:12 AM
  • Edward,

    Thank you.  This code worked for me:

     Dim wordApp As New Word.Application
     wordApp.Visible = True
     Dim doc As Word.Document = wordApp.Documents.Add(Template:="C:\<file path & file name>")


    James Hutchinson

    • Marked as answer by jhutch03 Tuesday, February 16, 2016 6:44 AM
    Tuesday, February 16, 2016 6:42 AM