WebBrowser Some considerations for opening Word documents

Source: Internet
Author: User

 WebBrowser Some considerations for opening Word documentsCategory: C # Word2010-03-31 21:26 5640 people read reviews (3) favorite reports WebBrowser document browser Tools Objectsql Serverwebbrowser Some considerations for opening Word documents September 27, 2009 Sunday 17:37

Busy for a few days, and finally will WebBrowser in the Word toolbar to take care of

In the question Bank management system, it is necessary to display the test questions and edit them. Therefore, each problem is stored as a Word file, and saved in SQL Server, when browsing the question library, with the WebBrowser control display test is the best method, there is an intuitive word interface, but also for the preservation of the question to provide a convenient.
It was initially displayed using the built-in Web Browser Controls in vs 2005. The display is normal, but quickly brings two questions:
1. The default is to have the Reviewing toolbar when it is opened, and how to have the Standard toolbar when you just open it.
2. There is no way to find the corresponding Document object to control the saving of Word files.
I've searched a lot of them on the Internet. To use the COM object Axwebbrowser control, including this article on MSDN: "How to use the WebBrowser control to open Office documents in Visual C # 2005 or Visual C #. NET" (http: SUPPORT.MICROSOFT.COM/KB/304662/ZH-CN)

First step: Add the Axwebbrowser control to the toolbox.
On the toolbox, right-click, use the selection, find the "Microsfot Web Browser" entry in the COM component, OK.

The second step is to add the Microsfot Web Browser control to the form, which is named Wbdocview

Third, when you need to use the WebBrowser control to load a Word document, use the following statement:
Wbdocview.nagivate (Yourfilename);

First problem encountered: When you open the first file, when you open the same file again, you cannot open the file again because it is still in the open state. When the form closes, it does not actively close the file, and it waits until the entire program exits to release the resource.
Workaround: In the form's FormClosing event, use the following statement to close the file:
Wbdocview.navigate ("About:blank");

To resolve the second issue, when you open a Word file, the Standard toolbar is displayed. What is described in MSDN 304662 is:
Object refmissing = System.Reflection.Missing.Value;
odocument = null;
Axwebbrowser1.navigate (strFileName, ref refmissing, ref refmissing, ref refmissing, ref refmissing);
When I do this the same way, there is always an "attempt to revoke an unregistered drag-and-drop target (exception from hresult:0x80040100 (dragdrop_e_notregistered))" error. A long time cannot be solved.
Later, the following code is written in the NavigateComplete2 event of the WebBrowser control:
Axwebbrowser1.navigate (strFileName, ref refmissing, ref refmissing, ref refmissing, ref refmissing);
Sure enough, the Reviewing toolbar no longer appears, instead there are three toolbars, common, font, and drawing. Temporarily regardless of it, later further study.

Fix the third question: How do I save a Word file when it's appropriate?
Similarly, in the NavigateComplete2 event, write the following code to get the Document object.
Object o = e.pdisp;
odocument = O.gettype (). InvokeMember ("Document", System.Reflection.BindingFlags.GetProperty, NULL, O, NULL);
The odocument here is a private field defined in the Form class: Object odocument = null;
Next, in the Click event of the Save button, use the following code: (You need to introduce the Office class library at this point)
Object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.DocumentClass doc = odocument as Microsoft.Office.Interop.Word.DocumentClass;
Doc. Save ();
At this point, the WebBrowser control on the form will be in suspended animation, then reload the file:
Wbdocview.navigate (Wbdocview.locationurl);
Although the loading process will have a noticeable pause and flicker, but temporarily regardless of it.

Forwarded from: http://blog.csdn.net/cqbsbjianxy/article/details/5438893

WebBrowser Some considerations for opening Word documents

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.