为了实现办公的自动化,需要实现文档的自动流转。开发出的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("\t" + 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中调试成功。

下载该向导:

http://bbs.wps.cn/forum.php?mod=viewthread&tid=22410767&extra=page%3D1%26filter%3Dtypeid%26typeid%3D151%26typeid%3D151

然后解压缩该文档,按照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_USER\Software\Kingsoft\Office\WPS\Addins\kgsPro.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_USER\Software\Kingsoft\Office\WPS\Addins\kgsPro.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 οnclick="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_ROOT\form]

"URL Protocol"="D:\\form.exe"

@="form"

[HKEY_CLASSES_ROOT\form\DefaultIcon]

@="D:\\form.exe,1"

[HKEY_CLASSES_ROOT\form\shell]

[HKEY_CLASSES_ROOT\form\shell\open]

[HKEY_CLASSES_ROOT\form\shell\open\command]

@="\"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_ROOT\wps]

@="wps"

"URL Protocol"="C:\\Users\\Administrator\\AppData\\Local\\Kingsoft\\WPS Office\\9.1.0.4468\\office6\\wps.exe"

[HKEY_CLASSES_ROOT\wps\DefaultIcon]

@="C:\\Users\\Administrator\\AppData\\Local\\Kingsoft\\WPS Office\\9.1.0.4468\\office6\\wps.exe,1"

[HKEY_CLASSES_ROOT\wps\shell]

@="open"

[HKEY_CLASSES_ROOT\wps\shell\open]

@="open"

[HKEY_CLASSES_ROOT\wps\shell\open\command]

@="\"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_ROOT\word]

@="word"

"URL Protocol"="C:\\Program Files (x86)\\Microsoft Office\\Office14\\WINWORD.EXE"

[HKEY_CLASSES_ROOT\word\DefaultIcon]

@="C:\\Program Files (x86)\\Microsoft Office\\Office14\\WINWORD.EXE,1"

[HKEY_CLASSES_ROOT\word\shell]

@="open"

[HKEY_CLASSES_ROOT\word\shell\open]

@="open"

[HKEY_CLASSES_ROOT\word\shell\open\command]

@="\"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_USER\Software\Microsoft\Office\15.0\Word

Word 2010

HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Word

Word 2007

HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Word

Word 2003

HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Word

Word 2002

HKEY_CURRENT_USER\Software\Microsoft\Office\10.0\Word

Word 2000

HKEY_CURRENT_USER\Software\Microsoft\Office\9.0\Word

Word和WPS插件开发总结相关推荐

  1. WPS插件开发流程(2)

    上一篇文章WPS插件开发流程(1)中,我们详细讲了如何在Visual Studio中如何创建插件.在WPS中显示插件的方法.今天我们完成剩下的部分,讲解如何导出PIA.DLL强签名.打包安装文件夹的方 ...

  2. easypoi导出word表格_java如何导出word和wps文档

    使用场景:打开一个表单页面,导出word或wps文件,代码框架基于springboot+jpa 一.准备word模板 二.pom.xml文件中引入依赖 <dependency><gr ...

  3. WPS2012交叉引用技巧,word比wps这点强更新參考文献

                WPS2012交叉引用技巧,word比wps这点强更新參考文献 到时生成仅仅有有一条线,好像WPS不能够,word能够,假设谁知道能够补充.^_^ 1.写论文,參考文献的改动非 ...

  4. 计算机文档排版考试,Word和WPS通用的文档排版技巧

    无论用Word还是wps排版文档时难免会使用文本对齐.段落设置的功能,本文总结了Word和WPS通用的文本排版技巧,希望对大家排版的工作有所帮助. 1.单元格数字小数点对齐 选中要处理小数点对齐的数字 ...

  5. word、wps图文复制一键粘贴到富文本编辑器

    从word.wps里图文复制粘贴到如tinymce.ckeditor.ueditor等富文本编辑器里,file:///C:/Users/lifuhai/AppData/Local/Temp/ksoht ...

  6. java 导出wps_java如何导出word和wps文档

    使用场景:打开一个表单页面,导出word或wps文件,代码框架基于springboot+jpa 一.准备word模板 二.pom.xml文件中引入依赖 cn.afterturn easypoi-bas ...

  7. python和wps-基于python的docx模块处理word和WPS的docx格式文件方式

    Python docx module for Word or WPS processing 本文是通过docx把word中的表格中的某些已填好的内容提取出来,存入excel表格. 首先安装docx的p ...

  8. 利用python的docx模块处理word和WPS的docx格式文件

    Python docx module for Word or WPS processing 本文是通过docx把word中的表格中的某些已填好的内容提取出来,存入excel表格. 首先安装docx的p ...

  9. word打开wps文件乱码_Word打开WPS文档成乱码怎么办

    Word打开WPS文档成乱码怎么办 最近一位同事拿来一篇WPS做的论文,请我帮他修改后再打印出来,我的电脑中安装的字处理软件是Word XP.记得Office XP中自带了WPS文档转换器,一向没机会 ...

  10. wps word打开是html,用Word打开WPS文件的两种方法,WPS文件如何打开?

    方法一 问:请问,怎样才能在word中正常使用wps文件? 答:一般来说,wps打开word文件是比较顺利的,但word打开wps文件时,若设置不对就无法打开.设置的方法是:启动word后,在&quo ...

最新文章

  1. 备考12月份电子学会青少年编程能力等级测试(图形化)的公益训练营即将开营
  2. matlab bfs函数,matlab练习程序(广度优先搜索BFS、深度优先搜索DFS)
  3. Java反射原理剖析一
  4. Qt Creator指定动态属性
  5. python cgi打印html代码
  6. I.MX6 Linux Qt 启动流程跟踪
  7. python目录结构生成库,使用CMake构建平台无关的目录结构
  8. 服务器配置 | 3306端口被占用,phpStudy无法启动Apache
  9. Clojure Project 工程文件分析
  10. 英语作业(general version an narrow version about sth)
  11. js array 删除指定元素_JS数组
  12. c语言折半排序的程序,C语言实现九大排序算法的实例代码
  13. 代码雨和N个本地磁盘的制作
  14. python实现大规模邻域搜索(LNS)求解旅行商问题(TSP)
  15. cncert/cc DDOS 清洗 流量清洗 IDC AFC AFD ICP
  16. 嵌入式系统中鲁棒性的理解
  17. 取消RadioButton前面小圆圈的方法
  18. windows与ipad互联传文件
  19. 关于计算机专业的调整与优化,基于oracle数据库系统性能调整与优化分析-计算机应用技术专业论文.docx...
  20. Ubuntu更换国内源

热门文章

  1. QQ音乐特有的.qmc3文件 转换成.mp3
  2. maven安装教程+Eclipse整合
  3. 微信小程序之网易云音乐的实现-云音乐
  4. C3D行为识别(一):UCF101视频数据集预处理
  5. UCF101数据集提取帧+TDN部署(Anaconda+Python3.7+Pytorch)
  6. 等距更纱黑体 T SC regular下载
  7. 100套精品PPT模板免费拿!以后再也不用怕老板叫你制作PPT了
  8. app商品详情原数据 API ——淘宝/天猫
  9. 阿里云公司简介介绍资料
  10. 201903股票投资与实践入门三:资金流向与K线入门