我已经用WebBrowser控件实现了在C#窗口程序内部打开Word文档,但是现在遇到三个问题,我想这三个问题其他朋友在做这样的操作的时候可能都遇到过,现在列举如下,希望能得到圆满的解决!1. 每次打开一个Word文档时,会弹出一个窗口,提示是打开还是保存(就和在网上下载东西的时候弹出的那个窗口一样),我希望不要弹出这个窗口,该怎么办?2. 打开Word文档以后,顶部的工具栏不是常用的工具栏,虽然点右键可以自己选择所需的工具栏,但这样不免有些麻烦,有没有办法,一打开就显示的是我自己需要的工具栏?(在代码中怎样实现?)3. 如果在一个程序里,有两个WebBrowser控件,当它们先后打开Word文档以后,先打开的那个Word文档的工具栏菜单就会全部失效(完全无法操作!),这个问题该如何解决???以上就是,关于用WebBrowser控件在窗口程序内部打开Word文档时的三大问题。希望大家齐努力,解决这些问题!
你可以导入Word控件来控制,上面的问题就不见了。
Word.CommandBars [P_MenuName].Enabled =true;
Word.CommandBars [P_MenuName].Visible =true;
Word.CommandBars [P_MenuName].Position =Office.MsoBarPosition.msoBarBottom;
Word.CommandBars [P_MenuName].Protection =Office.MsoBarProtection.msoBarNoCustomize;
常用的工具栏名有:
Standard
Formatting
Tables and Borders
Database
Drawing
Forms
Full Screen
Edit Picture
Visual Basic
Stop Recording
Mail Merge
Master Document
Microsoft
Header and Footer
Outlining
Print Preview
Word for Macintosh 5.0
Read Mail
Send Mail
Extended Formatting
AutoText
Web
WordArt
3-D Settings
Shadow Settings
Picture
Drawing Canvas
Organization Chart
Diagram
Reviewing
AutoSummarize
Exit Design Mode
Control Toolbox
Text Box
Outlook Read Mail
Outlook Send Mail
Function Key Display
Web Tools
Word Count
Japanese Greetings
Menu Bar
Refresh
Frames
E-mail
Reading Layout
Document Layout
Compare Side by Side
Ink Drawing and Writing
Ink Annotations
Ink Comment
Task Pane
符号栏
Drop Caps
Endnotes
Fields
Display Fields
Field Display List Numbers
Form Fields
Footnotes
Frames
Headings
Linked Headings
Script Anchor Popup
Lists
Inline Picture
Inline Canvas
Horizontal Line Popup
Tables
Table Cells
Table Headings
Table Lists
Table Pictures
Table Text
Whole Table
Linked Table
Text
Linked Text
Font Popup
Font Paragraph
Format Inspector Popup in Normal Mode
Format Inspector Popup in Compare Mode
Spelling
Grammar
Grammar (2)
Format consistency
Background Proofing Status Bar
Track Changes
Frame Properties
Hyperlink Context Menu
AutoSignature Popup
Field AutoText
Document Map
Shapes
Curve
Curve Node
Curve Segment
Floating Picture
Canvas Popup
OLE Object
ActiveX Control
WordArt Context Menu
Rotate Mode
Comment
Organization Chart Popup
Diagram
Connector
Track Changes Indicator
Chinese Translation
Address Block Popup
Greeting Line Popup
Inline ActiveX Control
XML Structure Node Popup
XML Error Options
Ink Comment
AutoShapes
Callouts
Flowchart
Block Arrows
Stars & Banners
Lines
Basic Shapes
Connectors
Fill Color
Insert Shape
Line Color
Drawing and Writing Pens
Annotation Pens
Drawing and Writing Pens
Annotation Pens
Align or Distribute
Rotate or Flip
Order
Nudge
Borders
Font Color
Shading Color
Cell Alignment
Text Wrapping
Clipboard
Envelope
System
Online Meeting
Document Recovery
我的QQ:53150147,我的邮箱:[email protected]
麻烦告知您的联系方式,因为我最近在开发一个程序,一定要在窗体中内嵌显示和编辑多个Word和PowerPoint,头都搞大了,遇到好多问题!
希望您能帮帮忙,万分感谢!!!
============================================================VS.Net 2003中MS还没有提供对Word的包装,需要自己导入。
在Office目录下有一个文件MSWORD.OLB,你把它作为COM组件引用添加进项目。
项目->添加引用->COM组件->浏览->C:\Program Files\Microsoft Office\OFFICE11\MSWORD.OLB
然后:
Microsoft.Office.Interop.Word.Application
这就是了。
但是用WebBrowser控件存在我开贴提到的三个大问题,严重影响使用,如果不解决,那这种方式就没什么实际意义了!
QQ:307331145
如果在同一个窗体上有两个 WebBrowser 控件,且两个控件都加载有 Word 文档,而您使用了前面某一种技术显示了工具栏,那么只有一组工具栏会是活动的,且只有该工具栏能正常工作。另外一个则被禁用,无法使用。参见:
http://support.microsoft.com/default.aspx?scid=kb;zh-cn;304662
我明白你的意思。导入的Word控件不是可视化空间。你可以创建起来Word.Application,并且操纵它。
就像VBA里面那样做。
你所说的把这个程序放到WinForm中,通过调用Win32API,SetParent就可以搞定。
创建多个、打开多个文档,都不是问题。
现在在公司上班,没有样例代码。晚上我把代码贴上来——如果问题还没有解决的话。
是这样的,我们公司有个软件产品中的一个模块需要用Word和PowerPoint向客户展示一些报表,上一个版本的就是操纵外部Word和PowerPoint。(当多个文档时,这种方式我个人也觉得确实不是很好)
现在老板喊我把它改成可以在窗口内部显示多个Word和PowerPoint文档,而且还要对这些文档进行如同在Office中一样的操作,唉!这几天搞得头都大了!Tooler(小杨):你的控件我找到了,但是只能对Word文档进行操作,我用它打开PowerPoint文档时,它还是调用的Word程序。你有类似的PowerPoint控件吗?
添加一个对Microsoft Word的开发资源的引用,以获取访问Word的入口。
位置:在Office目录中,文件MSWORD9.OLB(Office2000)。2.1.1. 如何创建
通过Word.ApplicationClass。
Word.ApplicationClass _WA;
_WA = new Word.ApplicationClass();
_WA.Visible = true;2.1.2. 如何把它放置到一个Panel上去
这需要调用Win32的API SetParent来实现。
先列出我们可能要用到的API:
public class User32 { [DllImport("User32.dll", EntryPoint="FindWindow")]
internal static extern int FindWindow(
string lpClassName,
string lpWindowName); [DllImport("User32.dll", EntryPoint="SetParent")]
internal static extern int SetParent(
int hWndChild,
int hWndNewParent);
[DllImport("User32.dll", EntryPoint="SetWindowLong")]
internal static extern int SetWindowLong(
int hWnd,
int nIndex,
long dwNewLong ); [DllImport("User32.dll", EntryPoint="GetWindowLong")]
internal static extern long GetWindowLong(
int hWnd,
int nIndex ); [DllImport("user32.dll", EntryPoint="MoveWindow")]
internal static extern int MoveWindow(
int hWnd,
int x,
int y,
int cx,
int cy,
bool repaint);
}//查找Word程序的句柄
_WordHandle = User32.FindWindow(WORD_CLASS_NAME, null);
if(_WordHandle != 0){
//把Word程序丢到一个Panel上。
User32.SetParent(_WordHandle, this.panel1.Handle.ToInt32());
}
2.1.3. 如何去掉标题、边框
使用底层的API修改窗口的风格。
// 先取得当前的风格参数
long style = User32.GetWindowLong(_WordHandle, (int)GetWindowLongFlag.GWL_STYLE );
style &= ~(int)WindowStyles.WS_SIZEBOX; //去掉可变大小
style &= ~(int)WindowStyles.WS_BORDER; //去掉边框
style |= (int)WindowStyles.WS_MAXIMIZE; //最大化
// 设置风格
User32.SetWindowLong(_WordHandle, (int)GetWindowLongFlag.GWL_STYLE, style);
// 调整位置,这是必要的。
User32.MoveWindow(_WordHandle, 0, 0, this.panel1.Width, this.panel1.Height, true); 另外,为了使它自动适应Panel的大小变化,应当在Panel的Resize事件中用MoveWindow调整Word程序的尺寸。2.2. 功能控制
请参看MSDN中关于Office部分的模型描述,以及理解Word.ApplicationClass的内容。
还有一个更好的学习方法是,打开Word,录制新宏,进行相关的操作,停止录制,然后查看宏代码。这个代码就是对Office中的功能、模型的最好解说。
打开我的电脑--工具--文件夹选项--文件类型--在“已注册的文件类型”中选中扩展名为doc的那条--点击下面的“高级”按钮--将“下载后确认打开”前面的勾去掉
这样再在web中打开word文档就不会弹出那个提示对话框了
不过这个方法是解决本机的,如何通过编程实现我还不知道