为了实现办公的自动化,需要实现文档的自动流转。开发出的WORD和WPS插件的功能包括显示批注、隐藏批注、引入文件、附加对象、保存文档、退出应用。
1 Word插件开发
1.1 插件开发方法
1.1.1 开发语言
开发语言的选择,可以选择C++和C#。
1.1.2 Visual studio开发说明
Visual Studio 2010提供了各个版本Office的插件开发,新建工程-按照的模板-Visual C#-Office-2010,运行程序时其会调用本地安装的Office;文件-选项-加载项-COM加载项,在此可以选择COM加载项。
(1) Word开发引用的文件有Microsoft.Office.Interop.Word及Office
(2) 使用的命名空间
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Word;
1.1.3 Visual studio的Samples
Visual Studio提供了本地WORD插件开发的示例,所在路径
Visual Studio2010-Help-Samples-local Samples folder。
1.2 三种访问word的方法
1.2.0.1 官方API
根据官方提供的API,可以实现对word的操作。
1.2.0.2 调用对话框(属于API范畴)
通过word提供的对话框方法调用Word.WdWordDialog.wdDialogInsertObject,打开“附加对象“对话框。
this.Application.Dialogs[Word.WdWordDialog.wdDialogInsertObject].Execute();
1.2.0.3 word控件
获取其控件,然后执行Excute,打开“附加对象”对话框
this.Application.ActiveDocument.CommandBars["Menu Bar"].Controls[4]).CommandBar.Controls[16].Execute();
1.3 对WORD文档的操作
1.3.1 打开文档
Word.Application app = new Word.Application(); Word.Document dd = app.Documents.Open("C:\hi12.docx");
1.3.2 word2010控件的操作方法
微软在word使用了Ribbon Interface,通过该接口可以获取所有的菜单和工具栏的命令。具体见:
http://office.microsoft.com/en-us/outlook-help/learn-where-menu-and-toolbar-commands-are-in-office-2010-and-related-products-HA101794130.aspx#_Toc268688374
1.3.2.1 两层节点访问方式
word一般是通过CommandBar/CommandBars和Control/Controls访问控件。只有两层的访问方式:第一层,直接访问CommandBar/CommandBars,通过CommandBar/CommandBars访问其下的Control/Controls。
如:
获取Show Markup命令条下几个Controls:
Globals.IRHelperBar.Application.ActiveDocument.CommandBars["Show Markup"].Controls.Count;
执行Show Markup命令条下的第7个Controls的实体的方法:
Globals.IRHelperBar.Application.ActiveDocument.CommandBars["Show Markup"].Controls[7].Excute();
1.3.2.2 多层节点的访问方式
1)子节点访问方式
如果Control下面还有子节点,查看其CommandBar下的Name;通过此名获访问其下的Controls。类似多叉树的访问方式。
Globals.IRHelperBar.Application.ActiveDocument.CommandBars["Reviewers"].Controls[1];
2)三层节点的访问方式
正常只是提供两级的访问结构CommandBars-Controls;如果有第三级并且需要访问,那么把Control提升为Command,创新构造两级结构Command-Control,从而实现第三层的访问。
This.Application.ActiveDocument.CommandBars["Menu Bar"].Controls[4]).CommandBar.Controls[16].Execute();
1.3.3 Word的部分功能
1.3.3.1 显示/隐藏标注
1)隐藏标注
调用API方式
wdApp.ActiveWindow.View.ShowRevisionsAndComments = False
控件方式
((dynamic)Globals.ThisAddIn.Application.ActiveDocument.CommandBars["Reviewing"].Controls[1]).Control.ListIndex = 2;
2)显示标注
调用API方式
wdApp.ActiveWindow.View.ShowRevisionsAndComments = True
控件方式
((dynamic)Globals.ThisAddIn.Application.ActiveDocument.CommandBars["Reviewing"].Controls[1]).Control.ListIndex = 1;
3)切换显示/隐藏标注
Globals.ThisAddIn.Application.Application.ActiveDocument.CommandBars["Show Markup"].Controls[3].Execute();
1.3.3.2 隐藏/显示审阅窗格
执行下面的语句,可以开始打开/关闭审阅窗格
doc.CommandBars["Reviewing"].Controls["Reviewing Pane"].Execute();
1.3.3.3 引入文件
((dynamic)Globals.ThisAddIn.Application.ActiveDocument.CommandBars["Menu Bar"].Controls[4]).CommandBar.Controls[16].Execute();
1.3.3.4 附加对象
((dynamic)Globals.ThisAddIn.Application.ActiveDocument.CommandBars["Menu Bar"].Controls[4]).CommandBar.Controls[17].Execute();
1.3.3.5 保存文件
Globals.ThisAddIn.Application.ActiveDocument.CommandBars["Standard"].Controls[3].Execute();
1.3.3.6 退出应用
((dynamic)Globals.ThisAddIn.Application.ActiveDocument.CommandBars["Menu Bar"].Controls[1]).CommandBar.Controls[21].Execute();
1.4 遍历Word2010的一级和二级控件
//Create Word Application
Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
//Create a new txt file to record controls' list
StreamWriter sw = System.IO.File.CreateText(@"C:Word Command Bar Control List.txt");
//loop through wordApp.CommandBars to get all CommandBars
foreach (Office.CommandBar cb in wordApp.CommandBars)
{
sw.WriteLine(cb.Name);
//loop through each CommandBar's Controls collection to get all controls
foreach (Office.CommandBarControl cbc in cb.Controls)
{
sw.WriteLine(" " + cbc.Caption);
}
}
1.5 Word文档的窗体事件
http://support.microsoft.com/kb/302817
最小化word窗口时,关闭
Form1 fm;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
fm = new Form1();
fm.Show();
// fm.TopMost=false;
// this.Application.ActiveDocument.Windows.Count;
object aa = this.Application.ActiveDocument.CommandBars["Show Markup"].Controls[3].OnAction;
this.Application.WindowSize+=new Word.ApplicationEvents4_WindowSizeEventHandler(Application_WindowSize);
}
private void Application_WindowSize(Word.Document Doc, Word.Window Wn)
{
if (this.Application.WindowState == Word.WdWindowState.wdWindowStateMinimize)
{
fm.Hide();
}
else
{
fm.Show();
}
}
2 WPS插件开发
WPS插件开发可以在WPS二次开发论坛http://bbs.wps.cn/forum-wpsercikaifa-1.html找到开发的资料。
开发语言
WPS可以使用C++、VB6/VAB、.net三种语言
本人实现了C++、VB6/VAB两种语言的开发。
2.1 三种访问WPS的方式
2.2 使用C++向导实现插件开发(V9.1.0.4468)
目前该种方法只在版本号为V9.1.0.4468中调试成功。
下载该向导:
然后解压缩该文档,按照setup_vs2008.js,显示安装成功及代表插件开发向导按照成功。
打开vs2008-新建工程,即可以看到WPS Office插件开发模板。
在OnConnection函数中添加插件功能。
2.2.1 关键代码
功能实现部分,test.h文件。
#pragma once
class __declspec(uuid("{D31D0AB3-B6A5-4FA7-A0C0-179DB9FBFF72}")) test;
_declspec(selectany) _ATL_FUNC_INFO OnClickButtonInfo =
{
CC_STDCALL,
VT_EMPTY,
2,
{ VT_DISPATCH, VT_BYREF | VT_BOOL }
};
using namespace AddInDesignerObjects;
class test :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<test, &__uuidof(test)>,
public IDispatchImpl<_IDTExtensibility2, &IID__IDTExtensibility2, &LIBID_AddInDesignerObjects>,
public IDispEventSimpleImpl<1, test, &DIID__CommandBarButtonEvents>,
public IDispEventSimpleImpl<2, test, &DIID__CommandBarButtonEvents>,
public IDispEventSimpleImpl<3, test, &DIID__CommandBarButtonEvents>,
public IDispEventSimpleImpl<4, test, &DIID__CommandBarButtonEvents>,
public IDispEventSimpleImpl<5, test, &DIID__CommandBarButtonEvents>,
public IDispEventSimpleImpl<6, test, &DIID__CommandBarButtonEvents>,
public IDispEventSimpleImpl<7, test, &DIID__CommandBarButtonEvents>
{
private:
WPS::_ApplicationPtr m_spWPSApp;
_CommandBarButtonPtr m_spButton1;
_CommandBarButtonPtr m_spButton2;
_CommandBarButtonPtr m_spButton3;
_CommandBarButtonPtr m_spButton4;
_CommandBarButtonPtr m_spButton5;
_CommandBarButtonPtr m_spButton6;
_CommandBarButtonPtr m_spButton7;
public:
DECLARE_REGISTRY_RESOURCEID(IDR_WPSCOMADDONS)
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(test)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(_IDTExtensibility2)
END_COM_MAP()
BEGIN_SINK_MAP(test)
SINK_ENTRY_INFO(1, DIID__CommandBarButtonEvents, 0x01, OnClickButton1, &OnClickButtonInfo)
SINK_ENTRY_INFO(2, DIID__CommandBarButtonEvents, 0x01, OnClickButton2, &OnClickButtonInfo)
SINK_ENTRY_INFO(3, DIID__CommandBarButtonEvents, 0x01, OnClickButton3, &OnClickButtonInfo)
SINK_ENTRY_INFO(4, DIID__CommandBarButtonEvents, 0x01, OnClickButton4, &OnClickButtonInfo)
SINK_ENTRY_INFO(5, DIID__CommandBarButtonEvents, 0x01, OnClickButton5, &OnClickButtonInfo)
SINK_ENTRY_INFO(6, DIID__CommandBarButtonEvents, 0x01, OnClickButton6, &OnClickButtonInfo)
SINK_ENTRY_INFO(7, DIID__CommandBarButtonEvents, 0x01, OnClickButton7, &OnClickButtonInfo)
//SINK_ENTRY_INFO(3, __uuidof(ET::_ApplicationEvents),0x113005, SheetActivate, &SheetActivateInfo)
END_SINK_MAP()
typedef IDispEventSimpleImpl<1, test, &DIID__CommandBarButtonEvents> CommandBarButtonEvents1;
typedef IDispEventSimpleImpl<2, test, &DIID__CommandBarButtonEvents> CommandBarButtonEvents2;
typedef IDispEventSimpleImpl<3, test, &DIID__CommandBarButtonEvents> CommandBarButtonEvents3;
typedef IDispEventSimpleImpl<4, test, &DIID__CommandBarButtonEvents> CommandBarButtonEvents4;
typedef IDispEventSimpleImpl<5, test, &DIID__CommandBarButtonEvents> CommandBarButtonEvents5;
typedef IDispEventSimpleImpl<6, test, &DIID__CommandBarButtonEvents> CommandBarButtonEvents6;
typedef IDispEventSimpleImpl<7, test, &DIID__CommandBarButtonEvents> CommandBarButtonEvents7;
test()
{
}
~test()
{
}
public:
STDMETHOD(OnConnection)(IDispatch * Application,
ext_ConnectMode ConnectMode, IDispatch * AddInInst, SAFEARRAY * * custom)
{
try
{
m_spWPSApp = Application;
_CommandBarsPtr spCommandBars = m_spWPSApp->CommandBars;
CommandBarPtr spCommandBar = spCommandBars->Add("MOKA工具条",1 ,"",TRUE);
CommandBarControlsPtr ETCtrls =spCommandBar ->Controls;
KSO::CommandBarControlPtr popupButton1=ETCtrls->Add(1,"","",TRUE);
popupButton1->Caption = _bstr_t(L"显示标注");
KSO::CommandBarControlPtr popupButton2=ETCtrls->Add(1,"","",TRUE);
popupButton2->Caption = _bstr_t(L"隐藏标注");
KSO::CommandBarControlPtr popupButton3=ETCtrls->Add(1,"","",TRUE);
popupButton3->Caption = _bstr_t(L"打开文件");
KSO::CommandBarControlPtr popupButton4=ETCtrls->Add(1,"","",TRUE);
popupButton4->Caption = _bstr_t(L"附件对象");
KSO::CommandBarControlPtr popupButton5=ETCtrls->Add(1,"","",TRUE);
popupButton5->Caption = _bstr_t(L"保存文件");
KSO::CommandBarControlPtr popupButton6=ETCtrls->Add(1,"","",TRUE);
popupButton6->Caption = _bstr_t(L"退出应用");
//KSO::CommandBarControlPtr popupButton7=ETCtrls->Add(1,"","",TRUE);
//popupButton7->Caption = _bstr_t(L"文档模板");
CommandBarButtonEvents1::DispEventAdvise(popupButton1);
CommandBarButtonEvents2::DispEventAdvise(popupButton2);
CommandBarButtonEvents3::DispEventAdvise(popupButton3);
CommandBarButtonEvents4::DispEventAdvise(popupButton4);
CommandBarButtonEvents5::DispEventAdvise(popupButton5);
CommandBarButtonEvents6::DispEventAdvise(popupButton6);
//CommandBarButtonEvents7::DispEventAdvise(popupButton7);
}
catch(const _com_error&)
{
}
return S_OK;
}
STDMETHOD(OnDisconnection)(ext_DisconnectMode RemoveMode, SAFEARRAY * * custom)
{
return S_OK;
}
STDMETHOD(OnAddInsUpdate)(SAFEARRAY * * custom)
{
return S_OK;
}
STDMETHOD(OnStartupComplete)(SAFEARRAY * * custom)
{
return S_OK;
}
STDMETHOD(OnBeginShutdown)(SAFEARRAY * * custom)
{
return S_OK;
}
//隐藏标注
void __stdcall OnClickButton1(
IDispatch* pCtrl,
VARIANT_BOOL* pbCancelDefault)
{
try
{
m_spWPSApp->ActiveWindow->View->ShowRevisionsAndComments = true;
}
catch (const _com_error& )
{
}
return;
}
//
void __stdcall OnClickButton2(
IDispatch* pCtrl,
VARIANT_BOOL* pbCancelDefault)
{
try
{
m_spWPSApp->ActiveWindow->View->ShowRevisionsAndComments = false;
}
catch (const _com_error& )
{
}
return;
}
//打开文件
void __stdcall OnClickButton3(
IDispatch* pCtrl,
VARIANT_BOOL* pbCancelDefault)
{
try
{
//m_spWPSApp->Selection->InsertFile("D:/win.txt",&vtMissing,&vtMissing,&vtMissing,&vtMissing);
// 引入文件
// WPS::WpsDialog aa = WPS::WpsDialog::wpsDialogInsertFile;
WPS::WpsDialog aa = WPS::WpsDialog::wpsDialogOpenFile;
m_spWPSApp->Dialogs->Item(aa)->Show();
}
catch (const _com_error& )
{
}
return;
}
//附加对象
void __stdcall OnClickButton4(
IDispatch* pCtrl,
VARIANT_BOOL* pbCancelDefault)
{
try
{
// WPS::ShapeNodePtr pp = m_spWPSApp->ActiveDocument->Shapes->AddShape(ksoShapeActionButtonMovie, 100, 100, 200, 200,&vtMissing);
//m_spWPSApp->ActiveDocument->InlineShapes->AddOLEControl();
WPS::WpsDialog aa = WPS::WpsDialog::wpsDialogInsertOLEObject;
m_spWPSApp->Dialogs->Item(aa)->Execute();
}
catch (const _com_error& )
{
}
return;
}
//保存所有的文档
void __stdcall OnClickButton5(
IDispatch* pCtrl,
VARIANT_BOOL* pbCancelDefault)
{
try
{
// m_spWPSApp->ActiveDocument->Save();
m_spWPSApp->CommandBars->Item[L"TabMenu Popup Menu"]->Controls->Item[L"保存所有文档(&E)"]->Execute();
}
catch (const _com_error& )
{
}
return;
}
//退出所有的文档并且一一询问是否需要保存修改过的文档,最后关闭应用
void __stdcall OnClickButton6(
IDispatch* pCtrl,
VARIANT_BOOL* pbCancelDefault)
{
try
{
// m_spWPSApp->Documents->Close();
_variant_t tt=WPS::wpsPromptToSaveChanges;
m_spWPSApp->Quit(&tt,&vtMissing,&vtMissing);
}
catch (const _com_error& )
{
}
return;
}
};
程序运行后会直接调用本地安装WPS2013(V9.1.0.4468),该插件在开发工具-COM加载项中显示,并可以勾选决定是否加载该插件。
2.2.2 C++获取WPS的一级和二级控件
ofstream outfile("d://b.txt");
if(!outfile){
cout << "Unable to open otfile";
exit(1); // terminate with error
}
_bstr_t bstr = m_spWPSApp->CommandBars->Count;
CString strSql = (LPCSTR)bstr;
int b=_ttoi(strSql);
int a=0;
for(int a=1;a<=b;a++)
{
string strSql = (LPCSTR)m_spWPSApp->CommandBars->Item[a]->Name;
outfile<<a <<" "<<strSql<<endl;
_bstr_t bstr2 = m_spWPSApp->CommandBars->Item[a]->Controls->Count ;
CString strSql2 = (LPCSTR)bstr2;
int m=_ttoi(strSql2);
for(int n=1;n<=m;n++)
{
string strSql = (LPCSTR)m_spWPSApp->CommandBars->Item[a]->Controls->Item[n]->Caption;
outfile<<" "<<a <<"."<<n<<" "<<strSql<<endl;
}
}
outfile.close();
2.3 VB6制作COM加载项
VB6与VAB的区别
VB是编程工具,与VS2008相似;VAB作为程序的自动化脚本而存在,必须依赖于宿主程序。
他们的主要区别是:
1. VB是设计用于创建标准的应用程序,而VBA是使已有的应用程序(EXCEL等)自动化
2. VB具有自己的开发环境,而VBA必须寄生于已有的应用程序.
3. 要运行VB开发的应用程序,用户不必安装VB,因为VB开发出的应用程序是可执行文件(*.EXE),而VBA开发的程序必须依赖于它的父应用程序,例如EXCEL.
2.3.1 VB6制作COM加载项的步骤
1.新建工程,选择ActiveX Dll。 2.工程、引用、选择Kingsoft Add-In Designer、Kingsoft Office 1.0 Object Library、Kingsoft WPS 2.0 object Library。 3.将工程名原来的“工程1”改为“kgsPro”,类名称的“Class1”改为“AddCommand” (这里的修改的名称根据实际情况而定义,但在后面的注册时会用到这两个名字) 4.写入如下的代码:
代码如下两节所示
5.单击文件、生成***.dll,保存到C盘下,文件名为kgsPro.dll。 6.Dll生成完成,下面就是注册的步骤了。 7.新建一个文本文档,保存为AddDemo.reg,写入如下的内容 Windows Registry Editor Version 5.00 [HKEY_CURRENT_USERSoftwareKingsoftOfficeWPSAddinskgsPro.AddCommand] "FriendlyName"="WPS加载项Demo" "Description"="Konguisheng的Demo系列之加载项" "LoadBehavior"=dword:00000003 "CommandLineSafe"=dword:00000001 8.双击AddDemo.reg,将此导入到注册表中。 9.单击Windows的“运行”,输入regsvr32 C:kgsPro.dll完成 10.如果要删除这个加载项 A.新建一个文本文档,保存为DeleteDemo.reg,写入如下的内容 Windows Registry Editor Version 5.00 [-HKEY_CURRENT_USERSoftwareKingsoftOfficeWPSAddinskgsPro.AddCommand] B.单击Windows的“运行”,regsvr32 /u C:kgsPro.dll (此步不是必须)
2.3.2 WPS2009生产COM加载项的代码
Option Explicit
Implements IDTExtensibility2
Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
End Sub
Private Sub IDTExtensibility2_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
End Sub
Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
End Sub
Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
End Sub
Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
End Sub
2.3.3 WPS2013(9.1.0.4468)生产COM加载项
Option Explicit
Implements IDTExtensibility2
Private WithEvents btnNew1 As CommandBarButton
Private WithEvents btnNew2 As CommandBarButton
Private WithEvents btnNew3 As CommandBarButton
Private WithEvents btnNew4 As CommandBarButton
Private WithEvents btnNew5 As CommandBarButton
Private WithEvents btnNew6 As CommandBarButton
Private Sub IDTExtensibility2_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
Dim comb As CommandBar
Set comb = Application.CommandBars.Add("我的工具栏"
Set btnNew1 = comb.Controls.Add
btnNew1.Caption = "退出程序"
btnNew1.SetPictureByPath ("D:/wps开发文档/VB-9.1.0.4468/quit.jpg")
Set btnNew2 = comb.Controls.Add
btnNew2.Caption = "保存文档"
Set btnNew3 = comb.Controls.Add
btnNew3.Caption = "引入文件"
Set btnNew4 = comb.Controls.Add
btnNew4.Caption = "附加对象"
Set btnNew5 = comb.Controls.Add
btnNew5.Caption = "显示标注"
Set btnNew6 = comb.Controls.Add
btnNew6.Caption = "隐藏标注"
End Sub
Private Sub btnNew1_Click(ByVal Ctrl As KSO.CommandBarButton, CancelDefault As Boolean)
Application.Quit SaveChanges:=wpsPromptToSaveChanges
End Sub
Private Sub btnNew2_Click(ByVal Ctrl As KSO.CommandBarButton, CancelDefault As Boolean)
Application.Documents.Save
End Sub
Private Sub btnNew3_Click(ByVal Ctrl As KSO.CommandBarButton, CancelDefault As Boolean)
Dialogs(wpsDialogInsertFile).Show
End Sub
Private Sub btnNew4_Click(ByVal Ctrl As KSO.CommandBarButton, CancelDefault As Boolean)
Dialogs(wpsDialogInsertOLEObject).Execute
End Sub
Private Sub btnNew5_Click(ByVal Ctrl As KSO.CommandBarButton, CancelDefault As Boolean)
Application.ActiveWindow.View.ShowRevisionsAndComments = True
End Sub
Private Sub btnNew6_Click(ByVal Ctrl As KSO.CommandBarButton, CancelDefault As Boolean)
Application.ActiveWindow.View.ShowRevisionsAndComments = False
End Sub
Private Sub IDTExtensibility2_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
End Sub
Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
End Sub
Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
End Sub
Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
End Sub
2.3.4 VB6的一些操作说明
下载VB6.0精装版。
文件-输出XX.dll文件
工具-引用,选择应用的库
视图-对象浏览器,查看库提供的具体方法及属性。
F5执行程序
F8单步执行程序
将光标放置在某一个函数内按F5,只执行该函数。
2.4 VAB开发环境
首先安装好WPS,此时开发工具中的VB编辑器是灰色的(假定此版WPS没有带VAB开发功能)
安装对应版本的VAB,安装好后,VB编辑器亮色,表示可以使用。
新建VAB工程
开发工具-VB编辑器-F5,若此文档未定义宏,弹出对话框,要求输入宏的名称。
Hello World程序示例:
Sub Test
Dim st As String
st = "Hello Word!"
MsgBox st
End Test
2.4.1 WPS2009代码示例–VAB示例
Sub test()
'声明一个工具栏对象
Dim comb As CommandBar
'添加一个新的工具栏并命名为“我的工具栏”
Set comb = Application.CommandBars.Add("我的工具栏")
'添加一个按钮 名字为“文字” 指定单击时调用宏“InsertText”
With comb.Controls.Add(KsoControlType.ksoControlButton)
.Caption = "文字"
.OnAction = "InsertText"
End With
'添加一个按钮 名字为“图片” 指定单击时调用宏“InsertImg”
With comb.Controls.Add(KsoControlType.ksoControlButton)
.Caption = "图片"
.OnAction = "InsertImg"
End With
'添加一个按钮 名字为“表格” 指定单击时调用宏“InsertTable”
With comb.Controls.Add(KsoControlType.ksoControlButton)
.Caption = "表格"
.OnAction = "InsertTable"
End With
End Sub
Sub InsertText()
'写入文本
Selection.TypeText "Kingsoft Office 2009"
'居中对齐
Selection.ParagraphFormat.Alignment = wpsAlignParagraphCenter
'新段落
Selection.TypeParagraph
End Sub
Sub InsertTable()
'插入表格
Dim mytable As Table
'指定表格的行列数
Set mytable = Selection.Tables.Add(Selection.Range, 3, 3)
End Sub
Sub InsertImg()
'插入图片
Dim myimg As InlineShape
'图片路径为与文档同一目录下名字为"logo.png"
Set myimg = Selection.InlineShapes.AddPicture(ThisDocument.Path & "logo.png")
End Sub
2.4.2 WPS2013代码示例-9.1.0.4468 –VAB示例
Sub test()
'声明一个工具栏对象
Dim comb As CommandBar
'添加一个新的工具栏并命名为"我的工具栏"
Set comb = Application.CommandBars.Add("我的工具栏")
With comb.Controls.Add(ControlButton)
.Caption = "退出"
.OnAction = "Quit"
End With
With comb.Controls.Add(ControlButton)
.Caption = "保存"
.OnAction = "Save"
End With
With comb.Controls.Add(ControlButton)
.Caption = "打开文件"
.OnAction = "InsertFile"
End With
With comb.Controls.Add(ControlButton)
.Caption = "附加对象"
.OnAction = "InsertObject"
End With
With comb.Controls.Add(ControlButton)
.Caption = "显示标注"
.OnAction = "ShowComments"
End With
With comb.Controls.Add(ControlButton)
.Caption = "隐藏标注"
.OnAction = "HideComments"
End With
End Sub
Sub Quit()
Application.Quit SaveChanges:=wpsPromptToSaveChanges
End Sub
Sub Save()
Application.Documents.Save
End Sub
Sub InsertFile()
'Selection.InsertFile FileName:="d:win.txt", Link:=True
Dialogs(wpsDialogOpenFile).Execute
End Sub
Sub InsertObject()
'Application.ActiveDocument.InlineShapes.AddOLEObject ClassType:="Excel.Sheet", FileName:="ww", LinkToFile:=True, DisplayAsIcon:=True, IconFileName, IconIndex, IconLabel, Range
'Application.ActiveDocument.InlineShapes.AddOLEObject "Excel.Sheet", "ww", True, True
'Application.ActiveDocument.InlineShapes.AddOLEObject "Excel.Sheet"
'Application.ActiveDocument.InlineShapes.AddOLEObject "Equation.3", , True
'Application.ActiveDocument.InlineShapes.AddOLEObject , "d:win.txt", True, True, , 2
With Dialogs(wpsDialogInsertOLEObject)
.Execute
End With
End Sub
Sub ShowComments()
ThisDocument.ActiveWindow.View.ShowRevisionsAndComments = True
End Sub
Sub HideComments()
ThisDocument.ActiveWindow.View.ShowRevisionsAndComments = False
End Sub
2.4.3 WPS2013代码示例-9.1.0.4842 –VAB示例
Sub test()
'声明一个工具栏对象
Dim comb As CommandBar
'添加一个新的工具栏并命名为“我的工具栏”
Set comb = Application.CommandBars.Add("我的工具栏")
'添加一个按钮 名字为“文字” 指定单击时调用宏“InsertText”
With comb.Controls.Add(ControlButton)
.Caption = "退出"
.OnAction = "Quit"
End With
With comb.Controls.Add(ControlButton)
.Caption = "保存"
.OnAction = "Save"
End With
With comb.Controls.Add(ControlButton)
.Caption = "引入"
.OnAction = "InsertFile"
End With
With comb.Controls.Add(ControlButton)
.Caption = "附加"
.OnAction = "InserObject"
End With
With comb.Controls.Add(ControlButton)
.Caption = "显痕"
.OnAction = "ShowComments"
End With
With comb.Controls.Add(ControlButton)
.Caption = "隐痕"
.OnAction = "HideComments"
End With
End Sub
Sub Quit()
Application.Quit SaveChanges:=wpsPromptToSaveChanges
End Sub
Sub Save()
Application.Documents.Save
End Sub
Sub InsertFile()
Selection.InsertFile FileName:="d:win.txt", Link:=True
End Sub
Sub InsertObject()
'Application.ActiveDocument.InlineShapes.AddOLEObject ClassType:="Excel.Sheet", FileName:="ww", LinkToFile:=True, DisplayAsIcon:=True, IconFileName, IconIndex, IconLabel, Range
'Application.ActiveDocument.InlineShapes.AddOLEObject "Excel.Sheet", "ww", True, True
'Application.ActiveDocument.InlineShapes.AddOLEObject "Excel.Sheet"
'Application.ActiveDocument.InlineShapes.AddOLEObject "Equation.3", , True
'Application.ActiveDocument.InlineShapes.AddOLEObject , "d:win.txt", True, True, , 2
With Dialogs(wdDialogHelpAbout)
.Show
End With
End Sub
Sub ShowComments()
ThisDocument.ActiveWindow.View.ShowRevisionsAndComments = True
End Sub
Sub HideComments()
ThisDocument.ActiveWindow.View.ShowRevisionsAndComments = False
End Sub
3 B/S调用本地word应用
两种方式:ActiveX、注册表的方式。
3.1 ActiveX方式
ActiveX方式只有IE浏览器提供,但是chrome、Opera、firefox都不支持该控件,此种方式逐渐被抛弃。
示例:打开服务器的doc文件
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
</head>
<body>
<button onclick="openDoc()">openDoc</button>
<script type="text/javascript">
function openDoc () {
// body...
var openDocObj;
openDocObj = new ActiveXObject("SharePoint.OpenDocuments.2"); // 为了兼容Office XP,可以创建“SharePoint.OpenDocuments.1”
openDocObj.ViewDocument("http://localhost//葫芦岛三日游行程.doc");
}
</script>
</body>
</html>
“IE已限制此网页运行脚本或ActiveX控件”,允许运行该AtiveX控件,确定,即可以下载服务器的doc文档,在本地运行。但是chrome、Opera、firefox都不支持该控件。
3.2 注册表
b/s程序不允许调用本地的exe,如果是这样的话,互联网没有安全可言了
3.2.1 自己编写的程序
可以通过注册一个自己的协议的办法,如
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOTform]
"URL Protocol"="D:\form.exe"
@="form"
[HKEY_CLASSES_ROOTformDefaultIcon]
@="D:\form.exe,1"
[HKEY_CLASSES_ROOTformshell]
[HKEY_CLASSES_ROOTformshellopen]
[HKEY_CLASSES_ROOTformshellopencommand]
@=""D:\form.exe" "%1""
注册表工具的版本信息
HKEY_CLASSWES_ROOT添加form树,树的名称对应自定义的URLProtocol的名称,web调用中需要用到这个名称
协议的名称,任意字符,后面不会用到
可应用程序的路径,只能是exe的程序
form添加一个分支,照抄
应用程序的路径,1照抄
form添加一个分支,照抄
form添加一个分支,照抄
应用程序路径,%1表示参数
注:
1) 路径使用双杠“\”
2) 如果字符串中有双引号(”),那么需要加转义字符“”
3) 将文件名称改为form.reg,双击文件执行,将这些项写入到注册表
检验是否注册成功
开始-运行 输入form:://test/,如果可以运行该程序,表示注册成功了;或者在浏览器的地址栏直接输入:form:://test/,可以运行则表示注册成功。
3.2.2 调用本地程序
3.2.2.1 启动本地WPS
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOTwps]
@="wps"
"URL Protocol"="C:\Users\Administrator\AppData\Local\Kingsoft\WPS Office\9.1.0.4468\office6\wps.exe"
[HKEY_CLASSES_ROOTwpsDefaultIcon]
@="C:\Users\Administrator\AppData\Local\Kingsoft\WPS Office\9.1.0.4468\office6\wps.exe,1"
[HKEY_CLASSES_ROOTwpsshell]
@="open"
[HKEY_CLASSES_ROOTwpsshellopen]
@="open"
[HKEY_CLASSES_ROOTwpsshellopencommand]
@=""C:\Users\Administrator\AppData\Local\Kingsoft\WPS Office\9.1.0.4468\office6\wps.exe" "%1""
1) 将文件名改为wps.reg,双击执行该文件,注册上述各项。
2) 在程序-开始/运行/各种浏览器中输入wps:://test/(wps::/test/、wps:/test/、wps: /test/、wps: )即可以启动本地安装的WPS软件。
3.2.2.2 启动本地Word
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOTword]
@="word"
"URL Protocol"="C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE"
[HKEY_CLASSES_ROOTwordDefaultIcon]
@="C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE,1"
[HKEY_CLASSES_ROOTwordshell]
@="open"
[HKEY_CLASSES_ROOTwordshellopen]
@="open"
[HKEY_CLASSES_ROOTwordshellopencommand]
@=""C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE" "%1""
1) 将文件名改为wps.reg,双击执行该文件,注册上述各项。
2) 在程序-开始/运行/各种浏览器中输入word:://test /(word::/test/、word:/test/、word: /test/、word: )即可以启动本地安装的word软件。
3) 在程序-开始/运行/各种浏览器中输入word:://id: /(word::/ id: /、word:/ id: /、word: / id: /、word: /id: )即可以启动本地安装的word软件。
3.2.2.3 各版本Word的注册表查询
http://support.microsoft.com/kb/822005/zh-cn
Word 2013 HKEY_CURRENT_USERSoftwareMicrosoftOffice15.0Word
Word 2010
HKEY_CURRENT_USERSoftwareMicrosoftOffice14.0Word
Word 2007
HKEY_CURRENT_USERSoftwareMicrosoftOffice12.0Word
Word 2003
HKEY_CURRENT_USERSoftwareMicrosoftOffice11.0Word
Word 2002
HKEY_CURRENT_USERSoftwareMicrosoftOffice10.0Word
Word 2000
HKEY_CURRENT_USERSoftwareMicrosoftOffice9.0Word