none
Opening a New Office 2007 Document using Visual Studio 2010 RRS feed

  • Question

  •  

    I've been attempting to open a Word 2007 document using Visual Studio 2010.  I'm not sure where I'm going wrong, this is my code so far:

    Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim oWord As Word.Application Dim oDoc As Word.Document 'Start Word and open the document template. oWord = CreateObject("Word.Application") oWord.Visible = True oDoc = oWord.Documents.Add oWord = CreateObject("Word.Application") oWord.Visible = True oWord.WindowState = Word.WdWindowState.wdWindowStateMaximize 'this opens document. Use document file name in the open statement oWord.Documents.Open(txtFileName)

    End Sub

     

     

    End Class

    The problem appears to be with the "Dim oWord As Word.Application" line.  Visual Studio is not recognizing "Word.Application".

    Thank you.

     

     

     


    James Hutchinson


    • Moved by Aaron.Parker Saturday, January 31, 2015 3:28 AM Not related to App-V
    • Edited by jhutch03 Sunday, February 1, 2015 11:17 PM deleted code that was commented-out.
    Monday, January 26, 2015 3:14 AM

Answers

  • Hello James,

    You need to add a reference to Microsoft Word. You can find all the required steps described in the How to automate Word from Visual Basic .NET to create a new document article.

    Also I have noticed the following code:

            oWord = CreateObject("Word.Application")
            oWord.Visible = True
            oDoc = oWord.Documents.Add
            oWord = CreateObject("Word.Application")
    
    There is no need to create a new instance of the Word application. I'd recommend removing the second CreateObject statement in the code.

    Monday, February 2, 2015 1:23 PM
  • Hi James Huntchinson,

    The suggestion provied by Eugene is correct. If you used the late binding to automate the Office application, we don't need to add the reference for the Office PIAs.

    If you want to add the reference, based on the code it used the custom name space. Did the code below work for you after you add the reference for Microsoft 12.0 Object Library?

    Imports Word = Microsoft.Office.Interop.Word

    Regards & Fei


    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.

    Thursday, February 12, 2015 8:16 AM
    Moderator

All replies

  • Is Microsoft Application Virtualisation related to this question?


    Please remember to click "Mark as Answer" or "Vote as Helpful" on the post that answers your question (or click "Unmark as Answer" if a marked post does not actually answer your question). This can be beneficial to other community members reading the thread.


    This forum post is my own opinion and does not necessarily reflect the opinion or view of my employer, Microsoft, its employees, or other MVPs.

    Twitter: @stealthpuppy | Blog: stealthpuppy.com | The Definitive Guide to Delivering Microsoft Office with App-V

    Monday, January 26, 2015 6:33 AM
  • Has anyone out there figured out how to open a new Word 2007 document using Visual Studio 2010?  This can't be that difficult, but I sure can't figure out.

    Thank you,

    Jim


    James Hutchinson

    Sunday, February 1, 2015 11:16 PM
  • Hello James,

    You need to add a reference to Microsoft Word. You can find all the required steps described in the How to automate Word from Visual Basic .NET to create a new document article.

    Also I have noticed the following code:

            oWord = CreateObject("Word.Application")
            oWord.Visible = True
            oDoc = oWord.Documents.Add
            oWord = CreateObject("Word.Application")
    
    There is no need to create a new instance of the Word application. I'd recommend removing the second CreateObject statement in the code.

    Monday, February 2, 2015 1:23 PM
  • Eugene,

    I tried referencing two objects in Visual Studio 2010 that I thought my work --

    Microsoft Word 12.0 Object Library

    Microsoft.Interop.Office.Word

    Your reference article above is for Visual Basic .NET.  The instructions described in the article don't work the same in Visual Basic 2010.


    James Hutchinson

    Sunday, February 8, 2015 4:22 AM
  • Hi James Huntchinson,

    The suggestion provied by Eugene is correct. If you used the late binding to automate the Office application, we don't need to add the reference for the Office PIAs.

    If you want to add the reference, based on the code it used the custom name space. Did the code below work for you after you add the reference for Microsoft 12.0 Object Library?

    Imports Word = Microsoft.Office.Interop.Word

    Regards & Fei


    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.

    Thursday, February 12, 2015 8:16 AM
    Moderator
  • Eugene,

    My code does not work after I add a reference to Microsoft 12.0 Object Library.   My code doesn't recognize what "Word.Application" and "Word.Document" are in my two Dim statements above.  A blue line keeps appearing under both of these lines of code.

    I don't know what this sentence means that you typed above. --

    "If you used the late binding to automate the Office application, we don't need to add the reference for the Office PIAs."  I'm sorry, you're going to have to simplify this for me.  What's late binding and what is a PIA?

    As I mentioned previously, the .NET instructions above do not work the same on Visual Studio 2010.  I'm afraid someone is going to have to give me step-by-step instructions for Visual Studio 2010 or I'm not going to be able to figure this out.

    Jim


    James Hutchinson

    Saturday, February 21, 2015 10:29 PM
  • Hi Jim,

    >> My code doesn't recognize what "Word.Application" and "Word.Document" are in my two Dim statements above.  A blue line keeps appearing under both of these lines of code.<<

    Because we need to add the Word name space for the code and the following figure may be helpful:

    Also if you have issues about devloping with VB syntax, I suggest that you reopen a new thread in Visual Basic forum.

    Regards & Fei


    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.

    Tuesday, March 3, 2015 7:12 AM
    Moderator
  • Fei,

    I've tried the "Imports Word = Microsoft.Office.Interop.Word" line previously.  I just tried it again using your code above and I still get the blue syntax error line appearing under "Word.Application".

    Maybe I'm not referencing the Microsoft Word 12.0 Object Library correctly, that's the only thing I can think of.

    These are the steps I'm performing to try to reference the Microsoft Word 12.0 Object Library:

    1) Click on Project from the main menu and select "Add Reference..."

    2) Click on the COM tab and select "Microsoft Word 12.0 Object Library"

    3) Click the OK button.

    All I can figure is that these steps are not correct and I'm not referencing the Microsoft Word 12.0 Object Library correctly.


    James Hutchinson

    Monday, March 9, 2015 2:39 AM
  • Hi James Hutchinson,

    Thanks for the detailed explain for this issue.

    Based on the steps above, you are correct to add the Word interop assembly for reference.

    When you move the cursor over the bule line, there should be some description about the error, would you mind sharing the figure with us to understand the issue?

    Also if it is convenient you can upload a demo project to help us to under this issue exactly. You can upload it via OneDrive and please don't contain any sensitive data in the project.

    Regards & Fei


    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.


    Monday, March 9, 2015 9:06 AM
    Moderator
  • Hi James,

    If you create a new project and add the references. Check if there is any error?

    Regards,

    Monday, March 9, 2015 9:38 AM
  • Yang,

    I tried your suggestion (creating a new project) and I was able to succesfully run the program.  Thank you! 

    I'm not sure where I went wrong on my other project because I used the exact same code and the exact same reference (Microsoft Word 12.0 Object Library).  Oh well, at least I'm finally making progress.

    However, I'm still running into one problem.  When the Microsoft Word document appears, every menu option is grayed-out and I can't type anything.  A big blue window appears below the menu options, where someone would normally type text in a Word document.

    In addition, I receive the following error message in Visual Studio (in the Immediate Window):

    A first chance exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll

    Any ideas on what may be causing this?

    Thanks in advance,

    Jim


    James Hutchinson

    Saturday, March 14, 2015 7:31 PM
  • Actually, the error message that I described in my previous post is no longer appearing (the System.MissingMemberException).  I'm not sure why it appeared the first time I ran the application.

    So is there other code that I must put into this program to enable the menu items in the Word document and so that people can type into the Word document?

    Thanks in advance,

    Jim


    James Hutchinson

    Saturday, March 14, 2015 7:53 PM
  • If I'm using Word 2007 on my computer, will this cause the problem that I described in my previous two posts?  As I stated previously, the Word document that automatically opens up will not allow me to type anything and the menu items are grayed-out.

    Thanks,

    Jim


    James Hutchinson

    Sunday, March 22, 2015 2:17 AM