应用对象是Word对象模型中最大的对象.是根对象.通过启动应用可以访问到其他对象实体.应用对象也具有一些应用层级的设置,这对Word自动化很有帮助.
当代码对Word设置了一系列变化,通过设置Application对象的屏幕更新(ScreenUpdating)属性到false就可以阻止word在代码执行时更新屏幕.同时把SrceenUpdate关闭后有力提高程序性能.
加入有一个加载项会长期操作word,为了提高性能把SU给设为fasle.而这个加载项可能会触发某一时间,把SU又设定回ture.那么这个加载项的加速就此结束,如果把SU的值在改动提前保存起来,在行为调用结束之后再恢复,那么性能则基本不受损失.
对于这种情况使用C#的异常处理机制是一个合适的选择,看下面的例子.
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
Word.Application app = this.Application;
bool oldScreenUpdateSetting = app.ScreenUpdating;
Word.Range range = this.Content;
try
{
app.ActiveWindow.View.Zoom.Percentage = 15;
app.ScreenUpdating = false;
Random r = new Random();
for (int i = 1; i < 5000; i++)
{
range.InsertAfter(r.NextDouble().ToString());
if (i % 1000 == 0)
{
app.ScreenUpdating = true;
app.ScreenRefresh();
app.ScreenUpdating = false;
}
}
}
finally
{
app.ScreenUpdating = oldScreenUpdateSetting;
}
}
上面一段程序在程序启动之前把SU状态保存到bool变量,然后在try里生成5000个随机小数,同时执行5次的SU开关,最终在finally块中把SU恢复初始态.
编写的代码有可能会导致Word禅城提示对话框和告警信息.如果在调试时出现类似这种情况,则需要想法屏蔽掉.可以通过DiaplayAlerts属性来控制,其下有枚举类型WdAlertLevel,如果设置为wdAlertsNone.这样就不会发生告警.同样有wdAlerts-MessageBox来控制消息盒的显隐.同时,也要注意在代码端的最后写入finally来恢复默认值.
All—全部显示,MessageBox—仅显示消息,None—全不显示
在长期的Word操作中在某些情况下也需要改变鼠标的指针,例如在执行某一过程时,鼠标变成沙漏让用户等待.WD应用对象有一个系统属性可以返回一个系统对象.Cursor属性对应的为WdCursorType包含:I型,正常,西北箭头和用户等待.见示例:
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
Word.Application app = this.Application;
Word.WdCursorType oldCursor = app.System.Cursor;
Word.Range range = this.Content;
try
{
app.ActiveWindow.View.Zoom.Percentage = 50;
app.System.Cursor = Word.WdCursorType.wdCursorWait;
Random r = new Random();
for (int i = 1; i < 5000; i++)
{
range.InsertAfter(((char)r.Next()).ToString());
}
}
finally
{
app.System.Cursor = oldCursor;
}
}
Application可以设置用户自定义信息显示在状态条上,Status-Bar属性用来给其赋值,这一点与上面不同它不能保存既有值,只能手动恢复,并且SB属性是只写属性不能读取.
使用Caption属性可设置在标题栏显示的信息.见下例:
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
Word.Application app = this.Application;
app.StatusBar = "自定义显示状态条内容";
app.Caption = "zhaoyi Studio 2017";
// app.System.Cursor old = Word.WdCursorType.wdCursorWait;
app.System.Cursor = Word.WdCursorType.wdCursorWait;
Word.Range range = this.Content;
range.InsertAfter("这是关于标题栏和状态的示例!");
}
下表的属性可以控制Word的外观显示
属性名 | 数据类型 | 功效 |
DisplayRecentFiles | Bool | 可以控制是否显示近期操作的历时文件,通过操作Recent-Files数量由0-50 |
DisplayScreenTips | Bool | 对于有评论的文字是否显示pop-up |
DisplayScrollBars | Bool | 是否显示滚动条,同时还有DHSB和DVSB来控制水平和垂直 |
Height | Int | 当windowsState设置为wdWindowsStateNormal时来设置显示高度 |
Left | Int | 当windowsState设置为wdWindowsStateNormal时来设置显示居左位置 |
ShowWindows-inTaskBar | Bool | |
Top | Int | 当windowsState设置为wdWindowsStateNormal时来设置显示顶部位置 |
Visible | Bool | 设置Word应用是否可见 |
Width | Int | 当windowsState设置为wdWindowsStateNormal时来设置显示宽度 |
WindowsState | WdWindowState | 包含最大化,最小化和正常 |
综合示例:
Application对象拥有一些激活对象(某些在Word中激活或选中的对象)的有关属性:
属性名 | 类型 | 作用 |
ActiveDocument | Document | 返回被激活文档,如果没有打开的文档则报异常 |
ActivePrinter | String | 返回当前激活的打印机信息 |
ActiveWindow | Window | 返回激活的窗口 |
NormalTemplate | Template | 返回当前使用的模板对象 |
Selection | Selection | 返回当前选取或者插入点的Selection对象 |
具体示例:
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
Word.Application app = this.Application;
ShowItem("ActiveDocument", app.ActiveDocument.Name);
ShowItem("活跃打印机", app.ActivePrinter);
ShowItem("活跃窗口", app.ActiveWindow.Caption);
ShowItem("所用模板", app.NormalTemplate.Name);
ShowItem("当前选取位置", app.Selection.End.ToString());
}
private void ShowItem(string name, string status)
{
MessageBox.Show(status, name);
}
Application可以设置经常用到的重要的集合体的性质.常见的集合体见下表:
属性名 | 作用 |
CommandBars | 返回命令条集合体,用来改变或添加工具条或菜单,自定义 |
Dialogs | 返回对话框集合体,用来内建Word对话框 |
documents | 返回所有开着的Document集合体 |
fontNames | 返回所有已安装的字体 |
KeyBindings | 返回快捷键集合体,可以让用户自定义快捷键 |
RecentFiles | 返回近期文件集合体,可以检查并打开近期50个文件的任意 |
TaskPanes | 返回任务面板集合体,让检测或检测18个内建的任务面板 |
Templates | 返回模板集合体,检测已安装模板及其性质 |
Windows | 返回窗口集合体. |
示例:
[赵毅1]private void ThisDocument_Startup(object sender, System.EventArgs e)
{
Word.Application app = this.Application;
app.DisplayDocumentInformationPanel = Getbool("是否显示文档属性?",
app.DisplayDocumentInformationPanel);
app.DisplayRecentFiles = Getbool("是否显示最近文件?", app.DisplayRecentFiles);
app.DisplayScreenTips = Getbool("是否显示提示?", app.DisplayScreenTips);
app.DisplayScrollBars = Getbool("是否显示滚动条?", app.DisplayScrollBars);
app.ShowWindowsInTaskbar = Getbool("是否显示人物条?", app.ShowWindowsInTaskbar);
app.Visible = Getbool("是否后台显示?", app.Visible);
app.WindowState = Word.WdWindowState.wdWindowStateNormal;
app.Width = 200;
app.Height = 500;
app.Top = 50;
app.Left = 100;
}
[赵毅2]/// <summary>
/// 这是一个用来返回设定值为bool的工具,MB的yesorno的
/// 返回值与Yes比较后出现 bool值
/// </summary>
/// <param name="message"></param>
/// <param name="currentValue"></param>
/// <returns></returns>
private bool Getbool(string message, bool currentValue)
{
return MessageBox.Show(String.Format("{0}(当前设置为{1})",
message, currentValue), "Word UI Demo",
MessageBoxButtons.YesNo) == DialogResult.Yes;
}
咨询电话
0371-68632068