為了實現辦公的自動化,需要實現文檔的自動流轉。開發出的 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 中調試成功。
下載該向導:
然后解壓縮該文檔,按照 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?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_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
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
