none
Embedding Word in an ASP.NET page. RRS feed

  • Question

  • Hi everyone,

    I looking for a way to embed Word onto an ASP.NET page. I want the user to be able to edit the Word doc directly from within my ASP.NET page.

    So my page would look something like the following screen shot...

    http://img84.imageshack.us/img84/2382/wordb.png

    Also, I'd like to be able to have a hook on the Save button, so that when the user clicks on Save, I would then be able to execute some code of my own.

    Thanks!

     

    Saturday, November 27, 2010 1:55 PM

All replies

  • Hello Fatbob73,

    Thanks for posting. To embed Word in a page, you could refer to this article: An Easy Way to Embed Word in a Web Page. It introduces a way based upon conversion of the Word document into an MHT file. Hope this would help you.

    Here is a point we need to pay attention to. Actually, it is not recommended to use Excel Automation in server-side. It has issues with security risks among other things. As said in this KB article, I will quote a statement here.

    In this context, the term "server-side" also applies to code that is running on a Windows workstation, if the code is running from a Windows workstation other than the interactive station of the user who is logged on. For example, code that is started by Task Scheduler under the SYSTEM account runs in the same environment as "server-side" ASP code or as DCOM code. Therefore, many of the issues that this article describes may occur. For more information about Windows workstations and about COM, see the "More Information" section and the "References" section.

    The first link I posted above is not using Word automation. If we are using Word automation in a no "server-side", the SaveAs event in Excel Automation could be used to execute some code. However, in this case, it is an ASP.NET application, and this event is not recommended to be used.

    However, here are some alternative ways in that KB article, such openxml SDK. Unfortunately, I am not very familiar with this topic in OpenXML SDK. For the alternative ways, here is also a thread which could give you some useful information:

    http://social.msdn.microsoft.com/Forums/en-US/exceldev/thread/54847ba0-0d5a-45e0-9098-597fe045642d.

    If you have any concern on this post, just feel free to follow up. Have a nice day.

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, November 29, 2010 3:50 AM
  • Thanks for the reply.

    I'm not too worried about security issues due to the fact that this will be on an intranet and not accessible but outside our network.

    How about adding Word onto a C# desktop application? Is that do able?

    Monday, November 29, 2010 7:43 PM
  • Hello again Fatbob73,

    Do you mean desktop application is a Windows From application? If yes, please see this KB article about how to use the WebBrowser control to open Office documents: http://support.microsoft.com/kb/304662.

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, November 30, 2010 8:20 AM
  • Hi Bessie,

     

    Yes I mean a Windows Form Application. I had a look at that link you provided, but the code on that link does not seem to work.

    I get errors with the following line of code...

    this.axWebBrowser1.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser1_NavigateComplete2);
    
    also when I try the following...
    this.axWebBrowser1_NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser1_NavigateComplete2);
    Also I'm not sure why when I return to the form design screen after adding the code provided on that link I get the following error...
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.GetMemberTargetObject(XmlElementData xmlElementData, String& member)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.CreateAssignStatement(XmlElementData xmlElement)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.XmlElementData.get_CodeDomElement()
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.EndElement(String prefix, String name, String urn)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.Parse(XmlReader reader)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.XML.CodeDomXmlProcessor.ParseXml(String xmlStream, CodeStatementCollection statementCollection, String fileName, String methodName)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomParser.OnMethodPopulateStatements(Object sender, EventArgs e)
    at System.CodeDom.CodeMemberMethod.get_Statements()
    at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
    at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
    at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
    at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host)
    I should have mentioned that I'm using VS2008. Also I need to be able to edit the doc file not simply just show a doc file. That is why I'm looking for a way to embed Word.
    Wednesday, December 1, 2010 1:51 PM
  • Hello again Fatbob73,

    Yes, that article was created targeted at Visual Studio 2005. We also could use this article to use WebBrowser to open Office document in Visual Studio 2008. In VS 2008, we could directly use WebBrowser control. Please refer to this article: http://www.codeproject.com/KB/office/Embedding_Excel.aspx.

    To open the document inside the WebBrowser control, please see this article: http://support.microsoft.com/kb/927009/.

    Also here are some resources which also maybe help you more or less.

    How can show a Excel sheet in Windows Form:
    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/f0101d8e-e8c4-4979-9acb-b18854aab482.

    How to display office doc. of version 2007 inside WebBrowser:
    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/4744f6d9-6cd6-4543-a8c4-2bb696bf36d0.

    Also we could edit the document and save it. Please test it again. Just feel free to follow up if you have any concern on it. Have a nice day.

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, December 2, 2010 6:32 AM
  • Hello again Fatbob73,

    I am writing to check the status of the issue on your side. Could you please let me know if the suggestion works for you or not? If you have any questions or concerns, please feel free to let me know, or unmark it back. Thanks.

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, December 6, 2010 7:00 AM
  • Hi Bessie,

    Well the status is that I more or less abandoned the idea of doing my application via the web (meaning an ASP.NET) page, and I'm working on a Windows Form application. I had a look at the link you provide above which uses the web browser appoarch, but I'm leaning towards using a Win32 API method as outlined here...

    http://www.codeproject.com/kb/office/WordInDotnet.aspx

    The issue I'm having now (and hopefully you can help me with), is how can I disable the hotkeys like Ctrl S and Ctrl O? I've hidden all of the toolbars and menu bar, but I also want to be able to not allow the user to press Ctrl S to save a document. Also besides disabling hotkey commands, can we intercept those commands so that when the user does hit Ctrl O, instead of the open dialog box appearing, I can run my own code?

    Tuesday, December 7, 2010 12:01 AM
  • Hello again Fatbob73,

    What version of Office you are working with? If it is Office 2010 or 2007, we could use <command> element in Ribbon XML in a VSTO add-in or Shared add-in. In this ribbon xml we could customize "Open" and "Save" button on Office menu. For more information, you could refer to Cindy's reply in this thread:

    trapping file open event in Word 2007:
    http://social.msdn.microsoft.com/Forums/pl-PL/vsto/thread/efbd4bb0-10c2-4a52-a411-c7e212009286.

    If you need any more further question on Ribbon xml, just feel free to let me know. Thanks.

    Best regards,
    Bessie Zhao - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, December 7, 2010 5:57 AM
  • Hi Bessie,

    We're working with Office 2003, so I'm looking for a solution that will work with 2003. We are planning on upgrading to Office 2007 (or possibly 2010), but that is in the future. I'd like my application to support 2007 and 2010, but it must work with 2003.

    Tuesday, December 7, 2010 2:22 PM
  • Hi again Bessie,

    I had a look at the link you provided, and I must I'm very confused. Besides disabling the hotkeys like Ctrl S and Ctrl O in Office 2003 (and 2007), I was looking at how I can remove the Ribbon in Office 2007, and I'm very lost. I'm able to remove the menu bars and toolbars inmy  C# application for Office 2003, but obviously when I run my app on a PC that has Office 2007, the Ribbon is displayed. Can I remove the Ribbon and if so how? I'm confused about this talk of using an XML file or VSTO add-in? Do you mean I need to create a Word 2007 Add-in project in VS2008? The application I wrote is a simple C# Windows Form Application.

    To remove the Office 2003 toolbars all I'm doing is the following...

    int counter = wd.ActiveWindow.Application.CommandBars.Count;
    
    for(int i = 1; i <= counter;i++)
    {
    	try
    	{
    		wd.ActiveWindow.Application.CommandBars[i].Enabled = false;
    	}
    }
    

    What must I do for Office 2007 and 2010 to remove the Ribbon?

    Thanks!

    Tuesday, December 7, 2010 5:10 PM
  • The UI has been changed to Ribbon in Word 2007 and 2010. I think you have also notice about this point on your side. Commandbars collection could not be used to customize ribbon. In Word 2007 and 2010, we could use Ribbon XML or Ribbon designer to customize ribbon. To make the ribbon with built-in tabs invisible, we could use startFromScratch property. Also there is also a old way with Macro. For more information, please see this thread:

    Hide Fluent Ribbon:
    http://social.msdn.microsoft.com/forums/en-us/vsto/thread/3A26E6AB-181E-4566-804B-D1267C5EEDCE.

    Since Ribbon is the new feature in Word 2007 and 2010, we could not use it in Word 2003 add-in. So here I recommend you create an add-in just for Word 2003. For this point, please refer to Ji Zhou and Cindy's replies in this thread about compatibility:

    VSTO development, compatibility:
    http://social.msdn.microsoft.com/Forums/en/vsto/thread/890cb60c-11f6-4fea-8a44-e520341f3331.

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, December 8, 2010 3:45 AM
  • Hi Bessie,

    First off, thanks for all the help you've been providing me with.

    I'll have a look at those two links you provided me, but I'm still a bit confused about making an Office add-in. How will this add-in work with my C# Windows Form application? I need to read up on Office add-ins.

    However I still need some help on suppressing and/or intercepting the hotkeys (example... Ctrl-S and Ctrl-O) in Office 2003. Any ideas on how I can do this in my C# application?

    Thanks!

    Wednesday, December 8, 2010 2:00 PM
  • VSTO add-in could not work with C# windows Form application. However, we could call code in application-level add-ins from other office solutions. It is a little complicated. You could refer to this thread: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/47eca4ff-b9f6-4d12-b1f4-6aa854d731ed. As far as I see, it is not what you want.

    For the beginner of VSTO, you could create your first application-level or document-level vsto project:
    Walkthrough: Creating Your First Application-Level Add-in for Word.
    Walkthrough: Creating Your First Document-Level Customization For Word.

    To suppress these two hotkeys, based on my experience, there is not a way for this goal exposed by Word 2003 object model. However, when pressing Ctrl+S, it will trigger DocumentBeforeSave event. Then in this event handler, by setting Cancel to true, it will not save this document. However, by this way, it will disable any saving operation not just hotkey ctrl+S. I am not sure if you could accept this way.

    Best regards,
    Bessie

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, December 9, 2010 5:30 AM
  • To suppress these two hotkeys, based on my experience, there is not a way for this goal exposed by Word 2003 object model. However, when pressing Ctrl+S, it will trigger DocumentBeforeSave event. Then in this event handler, by setting Cancel to true, it will not save this document. However, by this way, it will disable any saving operation not just hotkey ctrl+S. I am not sure if you could accept this way.

    That's great! I think I can work with this solution. However I'm not sure where in my project I need to added the DocumentBeforeSave event. As I mention in a previous post, I'm working with a solution provided by someone on Code Project, and that user wrote a control to display Word documents using Win32 APIs, so I'm not sure if I should add the event to that control or in my application.

    As for Office 2007 and 2010, are you saying that I have to create an Office 2007 add-in is VS2008, and this add-in must be installed on all of my user's PCs and then use this add-in from my C# Windows Form application?

    Thursday, December 9, 2010 5:42 AM
  • I believe I've found out how to trap the DocumentBeforeSave event, but even after setting Cancel to true, I still get the save as dialog box. Even when I'm not getting the save as dialog box, pressing Ctrl-S is still saving the document.

    Here is a snippet of my code...

    if(wd == null) wd = new Word.ApplicationClass();
    try
    {
    	wd.CommandBars.AdaptiveMenus = false;
    	wd.DocumentBeforeClose += new Word.ApplicationEvents2_DocumentBeforeCloseEventHandler(OnClose);
    	wd.NewDocument += new Word.ApplicationEvents2_NewDocumentEventHandler(OnNewDoc);
    	wd.DocumentOpen+= new Word.ApplicationEvents2_DocumentOpenEventHandler(OnOpenDoc);
    	wd.ApplicationEvents2_Event_Quit += new Word.ApplicationEvents2_QuitEventHandler(OnQuit);
    
    	wd.DocumentBeforeSave += new Word.ApplicationEvents2_DocumentBeforeSaveEventHandler(OnSave);
    }
    catch{}
    
    
    and here is my OnSave method...
    
    
    
    private void OnSave(Word.Document Doc, ref bool SaveAsUI, ref bool Cancel)
    {
    	MessageBox.Show("OnSave");
    
    	Cancel = true;
    }
    
    
    When I press Ctrl-S, I get the message box so I know I'm trapping the event and I'm getting to my method, but despite setting Cancel to true, the doc is still being saved.
    
    Thursday, December 9, 2010 7:12 PM
  • Hi again,

    I believe I found out my setting Cancel = true was not working. I did some searching on the internet and it turns out that Cancel is an In parameter only, so not matter what you set Cancel to be, it is being ignored. That would explain why even after setting cancel = true, the save was not being aborted.

    Here is the link that I found that offers a work around which involves modifying interop.word.dll.

    http://bytes.com/topic/c-sharp/answers/224072-word-documentbeforeclose-event-unable-cancel-event

    Is this the only solution? Do we have to resort to modifying the dll? If that is so, that I'm ok with that since I tried it and it does work.

    Also, now that the suppressing the save is more or less solved, is there a way to suppress Ctrl-O? There doesn't seem to be a DocumentBeforeOpen method? But somehow there must be a way to trap the Ctrl-O?

    Any thoughts on both the solution I found for trapping the Ctrl-S and a solution for trapping Ctrl-O?

    Thanks!

    Monday, December 13, 2010 2:28 AM
  • Hello Fatbob,

    Sorry for delay. For your reply, I will check it on my side. Absolutely, I will follow up if I have any useful information shared with you. Have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 14, 2010 10:09 AM