爲了實現辦公的自動化,須要實現文檔的自動流轉。開發出的WORD和WPS插件的功能包括顯示批註、隱藏批註、引入文件、附加對象、保存文檔、退出應用。javascript
開發語言的選擇,能夠選擇C++和C#。php
Visual Studio 2010提供了各個版本Office的插件開發,新建工程-按照的模板-Visual C#-Office-2010,運行程序時其會調用本地安裝的Office;文件-選項-加載項-COM加載項,在此能夠選擇COM加載項。html
(1) Word開發引用的文件有Microsoft.Office.Interop.Word及Officejava
(2) 使用的命名空間web
using Word = Microsoft.Office.Interop.Word;chrome
using Office = Microsoft.Office.Core;shell
using Microsoft.Office.Tools.Word;編程
Visual Studio提供了本地WORD插件開發的示例,所在路徑瀏覽器
Visual Studio2010-Help-Samples-local Samples folder。安全
根據官方提供的API,能夠實現對word的操做。
經過word提供的對話框方法調用Word.WdWordDialog.wdDialogInsertObject,打開「附加對象「對話框。
this.Application.Dialogs[Word.WdWordDialog.wdDialogInsertObject].Execute();
獲取其控件,而後執行Excute,打開「附加對象」對話框
this.Application.ActiveDocument.CommandBars["Menu Bar"].Controls[4]).CommandBar.Controls[16].Execute();
Word.Application app = new Word.Application(); Word.Document dd = app.Documents.Open("C:\\hi12.docx");
微軟在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
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)子節點訪問方式
若是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)隱藏標註
調用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();
執行下面的語句,能夠開始打開/關閉審閱窗格
doc.CommandBars["Reviewing"].Controls["Reviewing Pane"].Execute();
((dynamic)Globals.ThisAddIn.Application.ActiveDocument.CommandBars["Menu Bar"].Controls[4]).CommandBar.Controls[16].Execute();
((dynamic)Globals.ThisAddIn.Application.ActiveDocument.CommandBars["Menu Bar"].Controls[4]).CommandBar.Controls[17].Execute();
Globals.ThisAddIn.Application.ActiveDocument.CommandBars["Standard"].Controls[3].Execute();
((dynamic)Globals.ThisAddIn.Application.ActiveDocument.CommandBars["Menu Bar"].Controls[1]).CommandBar.Controls[21].Execute();
//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);
}
}
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();
}
}
WPS插件開發能夠在WPS二次開發論壇http://bbs.wps.cn/forum-wpsercikaifa-1.html找到開發的資料。
開發語言
WPS可使用C++、VB6/VAB、.net三種語言
本人實現了C++、VB6/VAB兩種語言的開發。
目前該種方法只在版本號爲V9.1.0.4468中調試成功。
下載該向導:
而後解壓縮該文檔,按照setup_vs2008.js,顯示安裝成功及表明插件開發嚮導按照成功。
打開vs2008-新建工程,便可以看到WPS Office插件開發模板。
在OnConnection函數中添加插件功能。
功能實現部分,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加載項中顯示,並能夠勾選決定是否加載該插件。
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.
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 (此步不是必須)
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
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
下載VB6.0精裝版。
文件-輸出XX.dll文件
工具-引用,選擇應用的庫
視圖-對象瀏覽器,查看庫提供的具體方法及屬性。
F5執行程序
F8單步執行程序
將光標放置在某一個函數內按F5,只執行該函數。
首先安裝好WPS,此時開發工具中的VB編輯器是灰色的(假定此版WPS沒有帶VAB開發功能)
安裝對應版本的VAB,安裝好後,VB編輯器亮色,表示可使用。
新建VAB工程
開發工具-VB編輯器-F5,若此文檔未定義宏,彈出對話框,要求輸入宏的名稱。
Hello World程序示例:
Sub Test
Dim st As String
st = "Hello Word!"
MsgBox st
End Test
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
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
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
兩種方式: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,若是是這樣的話,互聯網沒有安全可言了
能夠經過註冊一個本身的協議的辦法,如
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/,能夠運行則表示註冊成功。
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軟件。
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軟件。
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