WebOffice.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. function WebOffice2015() {
  2. this.dVersion="2.16.1.1";
  3. this.dUpdateDate="2016-1-1";
  4. this.FileType = ""; // FileType:文档类型 .doc .xls .wps
  5. this.obj; // obj:object 对象
  6. this.Status = ""; //Status:状态信息
  7. this.ShowDialog = {// 枚举显示系统和控件定义的相关对话框内容
  8. DialogNew: 0, // 新建对象
  9. DialogOpen: 1, // 打开
  10. DialogSaveAs: 2, // 另存为
  11. DialogSaveCopyAs: 3, // 另存为拷贝
  12. DialogPrint: 4, // 打印
  13. DialogPageSetup: 5, // 打印设置
  14. DialogProperties: 6
  15. // 文档属性
  16. }
  17. this.FileName = ""; //FileName:文档名称
  18. this.EditType = 0;
  19. this.UserName ="";
  20. this.setObj = function (object) {// 设置2015对象
  21. this.obj = object;
  22. this.WebObject = this.obj; //设置VBA调用对象
  23. }
  24. this.DocType = {// 枚举所有文档类型这里只列举word 0,execl 1
  25. WOED: 0,
  26. EXECL: 1
  27. };
  28. this.HttpMethod = {
  29. Get: 0, //Http对象get方式
  30. Post: 1 //Http对象post方式
  31. };
  32. this.DOWN = "\\WebOffice\\Down\\"//指定隐藏路径并设置名称
  33. this.UP = "\\WebOffice\\UP\\"//指定隐藏路径并设置名称
  34. this.FilePath = null; //文档路径
  35. this.tempInsertName = ""; //插入文件的文件名称
  36. this.DocTypeValue = { DOC: 0, DOCX: 16, XLS: 56, XLSX: 51 }; //枚举所有的文档类型值用户保存用
  37. this.DocSuffixType = { 0: ".doc", 12: ".docx", 56: ".xls", 51: ".xlsx" }; //根据打开文档类型,来获取后缀名称
  38. this.setVersion = -1;
  39. this.OfficeVersion = { v2003: 1, vOther: 0} //判断Office版本,这个只要用于保存到本地
  40. this.WebObject = null; //设置WebObject对象,方便调用VBA 方法
  41. this.sendMode = null //设置异步调用模式
  42. this.iWebOfficeTempName = "iWebOfficeTempName.doc"
  43. this.Caption = "DEMO演示平台";
  44. this.SetCaption = function(){
  45. this.Caption="\""+this.UserName+"\"正在编辑文档";
  46. this.obj.Caption =this.Caption;
  47. }
  48. //设置控件标题
  49. this.Caption = "DEMO演示平台";
  50. this.SetCaption = function(){
  51. this.Caption="\""+this.UserName+"\"正在编辑文档";
  52. this.obj.Caption =this.Caption;
  53. }
  54. //设置手写签批用户
  55. this.SetUser = function(username){
  56. this.obj.User = username;
  57. }
  58. /*
  59. * @deprecated:新建一个文档。
  60. */
  61. this.CreateFile = function () {
  62. var docType = this.getDocType(this.FileType); // 获取文档类型
  63. switch (docType) {
  64. case this.DocType.WOED:
  65. this.obj.CreateNew("Word.Document"); // 创建word
  66. break;
  67. case this.DocType.EXECL:
  68. this.obj.CreateNew("Excel.Sheet"); // 创建execl
  69. break;
  70. default:
  71. this.obj.CreateNew("Word.Document"); // 默认创建word文档
  72. break;
  73. }
  74. }
  75. /*
  76. * 获取文档类型
  77. */
  78. this.getDocType = function (fileType) {
  79. if (fileType == ".doc" || fileType == ".docx" || fileType == ".wps") {// word
  80. return this.DocType.WOED;
  81. }
  82. if (fileType == ".xls" || fileType == ".xlsx" || fileType == ".et") {// execl
  83. return this.DocType.EXECL;
  84. }
  85. }
  86. this.HookEnabled = function () {//解决在Firefox和chrome浏览器下调用iWebPlugin崩溃崩溃的问题需要在load()函数下加如下代码
  87. if (this.getDocType(this.FileType) == this.DocType.WOED) {
  88. this.obj.Style.ShowToolSpace = true;
  89. this.obj.SelectionInformationEnabled = false;
  90. }
  91. if (!((window.ActiveXObject != undefined) || (window.ActiveXObject != null) || "ActiveXObject" in window)) {
  92. this.obj.HookEnabled = false;
  93. }
  94. }
  95. /******一下是打开本地窗口代码******/
  96. // 打开本地窗口
  97. this.WebOpenLocal = function () {
  98. this.setShowDialog(this.ShowDialog.DialogOpen, this
  99. .getOpenDocSuffix(this.FileType));
  100. }
  101. // 获取打开窗口的后缀
  102. this.getOpenDocSuffix = function (fileType) {
  103. if (fileType.length == 5) {
  104. fileType = fileType.substring(0, 4);
  105. }
  106. var exts;
  107. exts = "";
  108. if(this.isWPS() || !this.getOfficeVersion()){//如果是office2003是不支持x格式的文档
  109. exts += "*" + fileType + "x|*" + fileType + "x|";
  110. }
  111. exts += "*" + fileType + "|*" + fileType + "|";
  112. exts += this.getOpenSuffixName(fileType) + "(*" + fileType
  113. if(this.isWPS() || !this.getOfficeVersion()){//如果是office2003是不支持x格式的文档
  114. exts += ";*"+ fileType + "x"
  115. }
  116. exts += ")|*" + fileType
  117. if(this.isWPS()|| !this.getOfficeVersion()){//如果是office2003是不支持x格式的文档
  118. exts += ";*" + fileType + "x";
  119. }
  120. exts +="|";
  121. return exts;
  122. }
  123. //返回当前编辑器
  124. this.getEditVersion = function (){
  125. return this.obj.AppName;
  126. }
  127. //获取打开文档类型名称
  128. this.getOpenSuffixName = function (fileType) {
  129. var openSuffixName;
  130. switch (fileType) {
  131. case this.DocType.WOED: openSuffixName = "Word Files"; break; //创建word后缀名称
  132. case this.DocType.EXECL: openSuffixName = "Excel Files"; break; //创建execl后缀名称
  133. default: openSuffixName = "Word Files"; break;
  134. }
  135. return openSuffixName;
  136. }
  137. //设置打开窗口的类型,这里统一设置
  138. this.setShowDialog = function (thisType, exts) {
  139. switch (thisType) {
  140. case this.ShowDialog.DialogOpen: this.obj.ShowDialog(thisType, exts, 0); break; //打开本地文档
  141. case this.ShowDialog.DialogNew: //新建对象
  142. case this.ShowDialog.DialogPageSetup: //打印设置
  143. case this.ShowDialog.DialogPrint: //打印
  144. case this.ShowDialog.DialogProperties: //文档属性
  145. case this.ShowDialog.DialogSaveCopyAs: //另存为拷贝
  146. case this.ShowDialog.DialogSaveAs: //另存为
  147. this.blnIE() ? this.obj.ShowDialog(thisType) : this.obj.ExecuteScript(thisType + "", "ActiveObject.ShowDialog(" + thisType + ")"); break; //打开窗口
  148. default: ; break;
  149. }
  150. }
  151. /******End 打开本地窗口代码******/
  152. /*文档保存到本地(有对话框)*/
  153. this.WebSaveLocal = function () {
  154. this.setShowDialog(this.ShowDialog.DialogSaveCopyAs);
  155. }
  156. /*2015自带有窗口的页面设置*/
  157. this.WebPageSetup = function () {
  158. this.setShowDialog(this.ShowDialog.DialogPageSetup);
  159. }
  160. /*2015自带有窗口的打印设置*/
  161. this.WebOpenPrint = function () {
  162. this.setShowDialog(this.ShowDialog.DialogPrint);
  163. }
  164. /*关闭文档*/
  165. this.WebClose = function(){ //多浏览器调用关闭
  166. if (navigator.userAgent.indexOf("Firefox") >= 0){//卸载时候,火狐不能使用close方法
  167. return;
  168. }
  169. this.Close();
  170. }
  171. //打印预览
  172. this.PrintPreview = function () {
  173. this.obj.PrintPreview();
  174. }
  175. //退出打印预览
  176. this.PrintPreviewExit = function () {
  177. this.obj.PrintPreviewExit();
  178. }
  179. /*给文件菜单添加下拉菜单。提供的接口 */
  180. var MenuFile;
  181. this.AppendMenu = function (index, Caption) {
  182. var custommenu = this.obj.CustomMenu;
  183. if (MenuFile == undefined || MenuFile == null) {
  184. custommenu.Clear();
  185. MenuFile = custommenu.CreatePopupMenu();
  186. custommenu.Add(MenuFile, "文件(&F)");
  187. }
  188. custommenu.AppendMenu(MenuFile, index, false, Caption);
  189. custommenu.Update();
  190. }
  191. this.AddCustomMenu = function () {
  192. var custommenu = this.obj.CustomMenu;
  193. //创建文件菜单的条目
  194. var menufile = custommenu.CreatePopupMenu();
  195. var menufilelv2 = custommenu.CreatePopupMenu();
  196. custommenu.AppendMenu(menufilelv2, 6, false, "自定义二级菜单一");
  197. custommenu.AppendMenu(menufilelv2, 7, false, "自定义二级菜单二");
  198. custommenu.AppendMenu(menufilelv2, 0, false, "-");
  199. var menufilelv3 = custommenu.CreatePopupMenu();
  200. custommenu.AppendMenu(menufilelv3, 8, false, "自定义三级菜单一");
  201. custommenu.AppendMenu(menufilelv3, 0, false, "-");
  202. custommenu.AppendMenu(menufilelv3, 9, false, "自定义三级菜单二");
  203. custommenu.AppendMenu(menufilelv2, menufilelv3, true, "自定义三级菜单");
  204. custommenu.AppendMenu(menufilelv2, 10, false, "自定义二级菜单三");
  205. custommenu.AppendMenu(menufile, menufilelv2, true, "自定义二级菜单");
  206. //将文件菜单添加到顶级主菜单
  207. custommenu.AppendMenu(menufile, 17, false, "启用保存");
  208. custommenu.AppendMenu(menufile, 18, false, "禁止保存");
  209. custommenu.AppendMenu(menufile, 0, false, "-");
  210. custommenu.AppendMenu(menufile, 19, false, "启用打印");
  211. custommenu.AppendMenu(menufile, 20, false, "禁止打印");
  212. custommenu.Add(menufile, "编辑(&E)");
  213. //创建语言
  214. /* var menuLang = custommenu.CreatePopupMenu();
  215. custommenu.AppendMenu(menuLang, 22, false, "简体中文");
  216. custommenu.AppendMenu(menuLang, 23, false, "繁体中文(TW)");
  217. custommenu.AppendMenu(menuLang, 24, false, "繁体中文(HK)")
  218. custommenu.AppendMenu(menuLang, 25, false, "英文");
  219. custommenu.Add(menuLang, "多语言(&N)");*/
  220. //通知系统更新菜单
  221. custommenu.Update();
  222. }
  223. /*从服务器上取文档并打开,打开RecordID指定的文件 */
  224. this.WebOpen = function (mBoolean) {
  225. var httpclient = this.obj.Http; //设置http对象
  226. httpclient.Clear();
  227. this.WebSetMsgByName("USERNAME", this.UserName); //加载UserName
  228. this.WebSetMsgByName("FILENAME", this.FileName); //加载FileName
  229. this.WebSetMsgByName("FILETYPE", this.FileType); //加载FileType
  230. this.WebSetMsgByName("RECORDID", this.RecordID); //加载RecordID
  231. this.WebSetMsgByName("OPTION", "LOADFILE"); //发送请求LOADFILE
  232. httpclient.AddForm("FormData", this.WebSendMessage()); //这里是自定义json 传输格式。
  233. this.WebClearMessage(); //清除所有WebSetMsgByName参数
  234. if (this.LOADFILE(httpclient)) { //Http下载服务器文件
  235. this.Status = "打开文档成功"; //Status:状态信息
  236. return true;
  237. }
  238. this.Status = "打开文档失败"; //Status:状态信息
  239. return false;
  240. }
  241. /*保存到本地路径 FileName:绝对路径*/
  242. this.WebSaveLocalFile = function (FileName) {
  243. // this.WebDelFile(FileName);
  244. this.Save(FileName, this.getOfficeVersion(), this.FileType.substring(1).toUpperCase());
  245. }
  246. /*office2003的接口和其他的不一样,所有保存到本地要区分开来*/
  247. this.Save = function (FileName, is2003, FileType) {
  248. if (is2003) {
  249. this.obj.Save(FileName);
  250. return;
  251. }
  252. this.obj.Save(FileName, eval("this.DocTypeValue." + FileType), true);
  253. }
  254. /*保存文件*/
  255. this.WebSave = function () {
  256. var httpclient = this.obj.Http; //设置http对象
  257. httpclient.Clear();
  258. this.WebSetMsgByName("USERNAME", this.UserName);
  259. this.WebSetMsgByName("RECORDID", this.RecordID);
  260. this.WebSetMsgByName("OPTION", "SAVEFILE");
  261. this.WebSetMsgByName("FILENAME", this.FileName); //加载FileName
  262. this.WebSaveLocalFile(this.getFilePath() + this.FileName);
  263. this.Close();
  264. if (this.SAVEFILE(httpclient, this.FilePath + this.FileName)) {
  265. this.Status = "保存文件成功";
  266. return true;
  267. }
  268. this.Status = "保存文件失败";
  269. return false;
  270. }
  271. this.SAVEFILE = function (httpclient, FileName) {//时发生交互OPTION值为:SAVEFILE
  272. httpclient.AddForm("FormData", this.WebSendMessage());
  273. httpclient.AddFile("FileData", FileName); //需要上传的文件
  274. this.WebClearMessage();
  275. httpclient.ShowProgressUI = false; //隐藏进度条
  276. if (httpclient.Open(this.HttpMethod.Post, this.WebUrl, false)) {//这里采用同步方式打开文档。我要返回值
  277. if (!httpclient.Send()) {
  278. if (httpclient.Status == 0) {
  279. if (!httpclient.Send()) {
  280. return false;
  281. }
  282. }
  283. }
  284. httpclient.Clear();
  285. return true;
  286. }
  287. return false;
  288. }
  289. this.LOADFILE = function (httpclient) { //Http 下载文档
  290. httpclient.ShowProgressUI = true;//隐藏进度条
  291. if (httpclient.Open(this.HttpMethod.Post, this.WebUrl, false)) {//这里采用同步方式打开文档。我要返回值
  292. if (httpclient.Send()) {
  293. if (httpclient.GetResponseHeader("MsgError") == "404") { //判断服务器是否存在文件
  294. this.CreateFile();
  295. this.getOfficeVersion();//打开文档后,判断当前office版本
  296. httpclient.Clear();
  297. return true;
  298. }
  299. httpclient.Clear();
  300. if (this.hiddenSaveLocal(httpclient, this, false, false)) {//下载成功时
  301. if(this.OpenLocalFile(this.FilePath+this.FileName)==0){ //打开本地磁盘文件
  302. this.getOfficeVersion();//打开文档后,判断当前office版本
  303. return true;
  304. }else{
  305. var windows = window.confirm("打开文档错误,可能当前编辑软件不支持当前格式!\r\r单击“确定”关闭。单击“取消”继续。");
  306. if(windows == 1){
  307. window.close();
  308. }
  309. return false;
  310. }
  311. }
  312. }
  313. return false;
  314. }
  315. }
  316. /*获取临时路径*/
  317. this.getFilePath = function () {
  318. var fs = this.obj.FileSystem; //创建file对象:
  319. var filePath = fs.GetSpecialFolderPath(0x1a) + this.UP; //设置临时路径
  320. fs.CreateDirectory(filePath); //创建路径
  321. this.FilePath = filePath; //给对象赋值,方便删除和打开
  322. return this.FilePath;
  323. }
  324. /*打开本地磁盘文件*/
  325. this.OpenLocalFile = function (filePath) {
  326. return this.obj.Open(filePath);
  327. }
  328. /*保存到本地 isHidden 是否隐藏文件;isInsertFile是否是插入文件*/
  329. this.hiddenSaveLocal = function (httpclient, webOffice, isHidden, isInsertFile, OtherName) {
  330. if (isHidden) {
  331. httpclient.Hidden = true; //隐藏文件
  332. }
  333. var tempName = "";
  334. var fs = webOffice.obj.FileSystem; //WebOffice外面对象名称:
  335. var filePath = fs.GetSpecialFolderPath(0x1a) + webOffice.DOWN;
  336. fs.CreateDirectory(filePath);
  337. webOffice.FilePath = filePath; //这个保存的路径方便打开的时候再取。
  338. if (httpclient.Status == 200) {
  339. if (isInsertFile == undefined || isInsertFile == true) {
  340. tempName = "temp" + webOffice.RecordID;
  341. webOffice.tempInsertName = tempName + webOffice.FileName;
  342. }
  343. if (OtherName == undefined || OtherName == "") {
  344. OtherName = webOffice.FileName;
  345. }
  346. httpclient.ResponseSaveToFile(filePath + tempName + OtherName); //临时文件
  347. httpclient.Clear();
  348. return true;
  349. }
  350. httpclient.Clear();
  351. return false;
  352. }
  353. //获取office版本信息
  354. this.getOfficeVersion = function () {
  355. var getVersion = 0.0;
  356. try {
  357. if(this.setVersion == -1){
  358. getVersion = parseFloat(this.obj.ActiveDocument.Application.Version);
  359. this.setVersion = getVersion;
  360. }else{
  361. getVersion = this.setVersion;
  362. }
  363. if (getVersion == 11.0) {
  364. return this.OfficeVersion.v2003;
  365. } else {
  366. return this.OfficeVersion.vOther;
  367. }
  368. } catch (e) {
  369. return this.OfficeVersion.v2003;
  370. }
  371. }
  372. //关闭文档
  373. this.Close = function () {
  374. this.obj.Close();
  375. }
  376. //获取焦点
  377. this.Activate = function(blnValue){
  378. this.obj.Activate(blnValue);
  379. }
  380. //设置word的页码 ,IE和其他浏览器有区别,所以封装起来
  381. this.WebPageCode = function () {
  382. var FunctionString = "ActiveObject.ActiveDocument.Application.Dialogs(294).Show()";
  383. this.blnIE() ? this.obj.ActiveDocument.Application.Dialogs(294).Show() : this.ExecuteScript("WebPageCode", FunctionString);
  384. }
  385. /*ExecuteScript可将同步函数转换为异步,解决在FireFox、Chrome浏览器中有弹框或运行时间过长导致弹出不响应提示框的问题。*/
  386. this.ExecuteScript = function (mValue, StringObject) {
  387. this.obj.ExecuteScript(mValue, StringObject);
  388. }
  389. /*控制2015标题栏*/
  390. this.ShowTitleBar = function (mValue) {
  391. var style = this.obj.Style;
  392. style.ShowTitleBar = mValue;
  393. }
  394. /*控制2015菜单栏*/
  395. this.ShowMenuBar = function (mValue) {
  396. var style = this.obj.Style;
  397. style.ShowMenuBar = mValue;
  398. }
  399. /*控制Office工具栏*/
  400. this.ShowToolBars = function (mValue) {
  401. var style = this.obj.Style;
  402. style.ShowToolBars = mValue;
  403. }
  404. /*控制2015状态栏*/
  405. this.ShowStatusBar = function (mValue) {
  406. var style = this.obj.Style;
  407. style.ShowStatusBar = mValue;
  408. }
  409. /*控制2015全屏*/
  410. this.FullSize = function (mValue) {
  411. this.obj.FullSize = mValue;
  412. }
  413. /*
  414. * 显示和隐藏痕迹
  415. * 隐藏痕迹时之前的痕迹不受影响
  416. */
  417. this.WebShow = function (blnValue) {
  418. if (this.getDocType(this.FileType) == this.DocType.WOED) {
  419. this.VBAShowRevisions(blnValue);
  420. }
  421. }
  422. //是否启用iWebOffice对象内文档的保存功能
  423. this.SaveEnabled = function (mBoolean) {
  424. this.obj.SaveEnabled = mBoolean;
  425. }
  426. //是否启用iWebOffice对象内文档的打印功能
  427. this.PrintEnabled = function (mBoolean) {
  428. this.obj.PrintEnabled = mBoolean;
  429. }
  430. /*设置保护文档*/
  431. this.WebSetProtect = function (Boolean, password) {
  432. var docType = this.getDocType(this.FileType);
  433. if (password == "") {
  434. password = "123456";
  435. }
  436. Boolean ? this.VBAProtectDocument(docType, password) : this.VBAUnProtectDocument(docType, password);
  437. }
  438. /*设置书签植*/
  439. this.WebSetBookmarks = function (vbmName, vbmValue) {
  440. this.VBASetBookmarks(vbmName, vbmValue);
  441. }
  442. //打开书签窗口
  443. this.WebOpenBookMarks = function(){
  444. var FunctionString = "ActiveObject.ActiveDocument.Application.Dialogs(168).Show()";
  445. this.blnIE()?this.obj.ActiveDocument.Application.Dialogs(168).Show():this.ExecuteScript("WebPageCode",FunctionString);
  446. }
  447. //光标处添加书签
  448. this.WebAddBookMarks = function(BMarksName,BMarksValue){
  449. if(!this.obj.ActiveDocument.BookMarks.Exists(BMarksName)){//判断是否存在该书签
  450. var BMVLength = BMarksValue.length;
  451. this.obj.ActiveDocument.Application.Selection.TypeText(BMarksValue);//插入内容
  452. this.obj.ActiveDocument.Application.Selection.MoveLeft(Unit = 1, Count = BMVLength);
  453. var StartR = this.obj.ActiveDocument.Application.Selection.Start;
  454. var EndR = this.obj.ActiveDocument.Application.Selection.Start + BMVLength;
  455. this.obj.ActiveDocument.Range(Start = StartR, End = EndR).Select();
  456. this.obj.ActiveDocument.Bookmarks.Add(BMarksName);//添加书签
  457. }
  458. }
  459. //书签定位
  460. this.WebFindBookMarks = function(BMarksName){
  461. var range = this.obj.Range;
  462. range = this.obj.ActiveDocument.Bookmarks(BMarksName).Range;
  463. range.Select();
  464. }
  465. //删除书签
  466. this.WebDelBookMarks = function(BMarksName){
  467. if(this.obj.ActiveDocument.BookMarks.Exists(BMarksName)){//判断是否存在该书签
  468. this.obj.ActiveDocument.Bookmarks(BMarksName).Delete();//删除书签
  469. }
  470. }
  471. /*获取当前文档打开类型,以后缀名来区别*/
  472. this.WebGetDocSuffix = function () {
  473. try{
  474. var docType = this.getDocType(this.FileType); //判断是文档还是表格
  475. var FileTypeValue = 0; //判断打开文档的值 0:doc,12:docx,51:xls,56:xlsx
  476. if (docType == this.DocType.WOED) { //word 获取vba值的方法
  477. this.Activate(true);
  478. FileTypeValue = this.obj.ActiveDocument.SaveFormat;
  479. }
  480. if (docType == this.DocType.EXECL) { //Execl 获取方法
  481. this.obj.ExitExcelEditMode(); // 退出当前编辑模式
  482. FileTypeValue = this.obj.ActiveDocument.FileFormat;
  483. if(FileTypeValue <0){ //2003不支持该属性
  484. FileTypeValue = 56;
  485. }
  486. }
  487. return this.DocSuffixType[FileTypeValue];
  488. }catch(e){
  489. return this.FileType;
  490. }
  491. }
  492. //iWebOffice打开的文档全路径。
  493. this.WebFullName = function () {
  494. return this.obj.FullName;
  495. }
  496. this.BookMark = "";
  497. this.ImageName = "";
  498. this.WebInsertImage = function (BookMark, ImageName) {//插入远程图片:BookMark插入的书签的位置;ImageName远程图片的名称
  499. var httpclient = this.obj.Http;
  500. this.sendMode = "LoadImage";
  501. this.BookMark = BookMark;
  502. this.ImageName = ImageName;
  503. var URL = this.WebUrl.substring(0, this.WebUrl.lastIndexOf("/"));
  504. httpclient.ShowProgressUI = false;//隐藏进度条
  505. if (httpclient.Open(this.HttpMethod.Post, URL + "/Document/" + ImageName, true)) {
  506. httpclient.Send();
  507. httpclient.Clear();
  508. }
  509. }
  510. //更加书签插入图片
  511. this.InsertImageByBookMark = function () {
  512. if (this.BookMark == null || this.BookMark == 'null' || this.BookMark == "") {
  513. this.obj.ActiveDocument.Application.Selection.GoTo(-1, 0, 0, this.BookMark);
  514. }
  515. this.obj.ActiveDocument.Application.Selection.InlineShapes.AddPicture(this.FilePath + this.ImageName);
  516. this.obj.ActiveDocument.InlineShapes.Item(1).ConvertToShape(); //转为浮动型
  517. this.obj.ActiveDocument.Shapes.Item(1).WrapFormat.Type = 5; //0:四周型 1:紧密型 2:穿越型环绕 3:浮于文字上方 4:上下型环绕 5:衬于文字下方 6:浮于文字上方
  518. return true;
  519. }
  520. //根据文档名称插入文档
  521. this.WebInsertFile = function () {
  522. var httpclient = this.obj.Http; //设置http对象
  523. this.WebSetMsgByName("FILENAME", this.FileName); //加载FileName
  524. this.WebSetMsgByName("OPTION", "LOADFILE"); //发送请求LOADFILE
  525. httpclient.AddForm("FormData", this.WebSendMessage()); //这里是自定义json 传输格式。
  526. this.WebClearMessage(); //清除所有WebSetMsgByName参数
  527. if (this.LOADFILE(httpclient)) { //Http下载服务器文件
  528. this.Status = "打开文档成功"; //Status:状态信息
  529. return true;
  530. }
  531. this.Status = "打开文档失败"; //Status:状态信息
  532. return false;
  533. }
  534. //下载文档
  535. this.DownloadToFile = function (DownFileName, SavePath) {
  536. var httpclient = this.obj.Http;
  537. var URL = this.WebUrl.substring(0, this.WebUrl.lastIndexOf("/"));
  538. httpclient.ShowProgressUI = false;//隐藏进度条
  539. if (httpclient.Open(this.HttpMethod.Get, URL + "/Document/" + DownFileName, false)) {//指定下载模板的名称
  540. if (httpclient.Send()) {
  541. if (httpclient.Status == 200) {
  542. httpclient.ResponseSaveToFile(SavePath + DownFileName);
  543. httpclient.Clear();
  544. return true;
  545. }
  546. }
  547. }
  548. httpclient.Close();
  549. return false;
  550. }
  551. /**
  552. ×××××××××××××××××××××××××××××××××××××××××××××××××××
  553. * 公共方法
  554. * ×××××××××××××××××××××××××××××××××××××××××××××××××××
  555. */
  556. /*用来存储Http发送的表单数据*/
  557. this.ArrayList = function () {
  558. this.ObjArr = {}; //列表
  559. this.Count = 0; // 数量
  560. this.Add = function (key, value) {//添加
  561. this.ObjArr[key] = value;
  562. this.Count++;
  563. return true;
  564. }
  565. this.Clear = function () {//清空
  566. this.ObjArr = {}; this.Count = 0;
  567. }
  568. //按json格式输出
  569. this.toString = function () {
  570. var newArray = new Array(); //存储json字符串
  571. var i = 0; for (var i in this.ObjArr) {
  572. newArray.push("'" + i + "':'" + this.ObjArr[i] + "'");
  573. }
  574. return "{" + newArray + "}";
  575. }
  576. }
  577. this.ht = new this.ArrayList();
  578. /**
  579. * @deprecated:设置相应字段域值,等待发送给服务器
  580. * @param FieldName
  581. * @param FieldValue
  582. * @return
  583. */
  584. this.WebSetMsgByName = function (FieldName, FieldValue) {
  585. this.ht.Add(FieldName, FieldValue);
  586. }
  587. /*按json格式发送数据*/
  588. this.WebSendMessage = function () {
  589. return this.ht.toString();
  590. }
  591. /*@deprecated:清除由WebSetMsgByName设置的值*/
  592. this.WebClearMessage = function () {
  593. this.ht.Clear();
  594. }
  595. /*判断浏览器类型*/
  596. this.blnIE = function () {
  597. return (window.ActiveXObject != undefined) || (window.ActiveXObject != null) || ("ActiveXObject" in window)
  598. }
  599. /*判断是否是WPS*/
  600. this.isWPS = function(){
  601. return this.FileType.toUpperCase() == ".WPS" || this.FileType.toUpperCase() == ".ET";
  602. }
  603. /*
  604. * 控件版本
  605. */
  606. this.Version = function () {
  607. return this.obj.Version;
  608. }
  609. //是否启用iWebOffice对象内的拷贝功能。非控件的不受影响
  610. this.WebEnableCopy = function (mValue) {
  611. switch (mValue) {
  612. case 0:
  613. case false:
  614. case "0": this.obj.CopyEnabled = false; break; //启用
  615. case 1:
  616. case "1":
  617. case true: this.obj.CopyEnabled = true; break; //关闭
  618. default: ; return;
  619. }
  620. }
  621. //删除文件
  622. this.WebDelFile = function (FileName) {
  623. var fs = this.obj.FileSystem;
  624. fs.DeleteFile(FileName);
  625. }
  626. //设置状态
  627. this.setEditType = function (type) {
  628. try{
  629. switch (type) {
  630. case "0": this.VBAProtectDocument(this.getDocType(this.FileType), "123"); break;
  631. case "1": this.WebShow(false); break;
  632. case "2": this.WebShow(true); break;
  633. default: ;
  634. }
  635. }catch(e){
  636. return false;
  637. }
  638. }
  639. /**
  640. ×××××××××××××××××××××××××××××××××××××××××××××××××××
  641. * End 公共方法
  642. * ×××××××××××××××××××××××××××××××××××××××××××××××××××
  643. */
  644. /**
  645. ×××××××××××××××××××××××××××××××××××××××××××××××××××
  646. * VBA专区
  647. * ×××××××××××××××××××××××××××××××××××××××××××××××××××
  648. */
  649. //显示痕迹和隐藏痕迹
  650. this.VBAShowRevisions = function (mValue) {
  651. if(this.getDocType(this.FileType)== this.DocType.WOED && this.obj.ActiveDocument.ProtectionType == "-1"){
  652. this.obj.ActiveDocument.TrackRevisions = mValue; //显示标记和隐藏标记
  653. this.obj.ActiveDocument.ShowRevisions = mValue; //显示痕迹或隐藏
  654. return true;
  655. }
  656. return false;
  657. }
  658. //根据文档保护文档
  659. this.VBAProtectDocument = function (docType, password) {
  660. if (docType == this.DocType.WOED) {//word 保护模式
  661. if(this.obj.ActiveDocument.ProtectionType == "-1"){
  662. this.obj.ActiveDocument.Protect(3, false, password);
  663. return;
  664. }
  665. }
  666. if (docType == this.DocType.EXECL) {//word 保护模式,这里只保护表单1的其他的安自己需求编写
  667. if(!this.obj.ActiveDocument.Application.Sheets(1).ProtectContents){ //判断表单是否是保护的
  668. this.obj.ActiveDocument.Application.Sheets(1).Protect(password);
  669. }
  670. return;
  671. }
  672. return false
  673. }
  674. //根据密码解除保护
  675. this.VBAUnProtectDocument = function (docType, password) {
  676. if (docType == this.DocType.WOED) {//word 保护模式
  677. this.obj.ActiveDocument.Unprotect(password); ;
  678. return;
  679. }
  680. if (docType == this.DocType.EXECL) {//word 保护模式,这里只保护表单1的其他的安自己需求编写
  681. this.obj.ActiveDocument.Application.Sheets(1).Unprotect(password);
  682. return;
  683. }
  684. }
  685. //设置书签
  686. this.VBASetBookmarks = function (vbmName, vbmValue) {
  687. try{
  688. this.obj.ActiveDocument.Bookmarks(BookMarkName).Range.Text = BookMarkValue;
  689. }catch(e){
  690. return false;
  691. }
  692. }
  693. this.VBAInsertFile = function (Position, FileName) {
  694. try {
  695. this.obj.ActiveDocument.Application.Selection.GoTo(-1, 0, 0, Position);
  696. this.obj.Activate(true);
  697. this.obj.ActiveDocument.Application.Selection.InsertFile(FileName, "", false, false, false);
  698. return true;
  699. } catch (e) {
  700. return false;
  701. }
  702. }
  703. //接受所有痕迹
  704. this.WebAcceptAllRevisions = function () {
  705. try {
  706. this.obj.Activate(true);
  707. this.obj.ActiveDocument.Revisions.AcceptAll();
  708. return this.obj.ActiveDocument.Revisions.Count >= 0 ? true : false;
  709. } catch (e) {
  710. return false;
  711. }
  712. }
  713. //设置WORD用户名
  714. this.VBASetUserName = function (UserName) {
  715. try{
  716. this.obj.ActiveDocument.Application.UserName = UserName;
  717. }catch(e){
  718. return false;
  719. }
  720. }
  721. //设置域
  722. this.ShowField = function(){
  723. try{
  724. this.obj.ActiveDocument.ActiveWindow.View.ShowDrawings = true;
  725. this.obj.ActiveDocument.ActiveWindow.View.ShowFieldCodes = true;
  726. }catch(e){
  727. return false;
  728. }
  729. }
  730. //区域保护
  731. this.WebAreaProtect = function(BMarksName){
  732. if(!this.obj.ActiveDocument.BookMarks.Exists(BMarksName)){//判断是否存在该书签
  733. alert("文件内没有名称为'"+BMarksName+"'的书签 ,请先添加书签!");
  734. return;
  735. }
  736. var range = this.obj.Range;
  737. range = this.obj.ActiveDocument.Bookmarks(BMarksName).Range;
  738. range.Select();
  739. this.obj.ActiveDocument.bookmarks(BMarksName).range.editors.add(-1); //常量:wdeditoreveryone=-1
  740. this.obj.ActiveDocument.Protect(3,false,"123",false,false);//常量:wdAllowOnlyReading=3
  741. this.obj.ActiveDocument.Application.Selection.MoveLeft(Unit = 1, Count = 1);
  742. this.obj.ActiveDocument.ActiveWindow.View.ShadeEditableRanges = false;//取消"突出显示可编辑区域"
  743. }
  744. //取消区域保护
  745. this.WebAreaUnprotect = function(BMarksName){
  746. if(this.obj.ActiveDocument.BookMarks.Exists(BMarksName)){//判断是否存在该书签
  747. try{
  748. this.obj.ActiveDocument.Unprotect("123");//解保护
  749. var range = this.obj.Range;
  750. range = this.obj.ActiveDocument.Bookmarks(BMarksName).Range;
  751. range.Select();//选定书签内容
  752. this.obj.ActiveDocument.DeleteAllEditableRanges(-1); //去掉突出显示
  753. }catch(e){
  754. }
  755. }else{
  756. alert("不存在该书签!");
  757. return;
  758. }
  759. }
  760. /**
  761. ×××××××××××××××××××××××××××××××××××××××××××××××××××
  762. * End VBA专区
  763. * ×××××××××××××××××××××××××××××××××××××××××××××××××××
  764. */
  765. /**
  766. ×××××××××××××××××××××××××××××××××××××××××××××××××××
  767. * 皮肤
  768. * ×××××××××××××××××××××××××××××××××××××××××××××××××××
  769. */
  770. this.Skin = function (typeColor) {//设置边框的演示,使得iWebOffice更好看
  771. switch (typeColor) {
  772. case "purple": this.setColor("purple"); this.Status = ("set pink success"); break;
  773. case "black": this.setColor("black"); this.Status = ("set black success"); break;
  774. case "white": this.setColor("white"); this.Status = ("set black success"); break;
  775. case "blue": this.setColor("blue"); this.Status = ("set black success"); break;
  776. case "yellow": this.setColor("yellow"); this.Status = ("set black success"); break;
  777. }
  778. }
  779. this.setColor = function (typeColor) { //设置边框演示
  780. var titleBarColor;
  781. var menuBarStartColor;
  782. var menuBarButtonStartColor;
  783. var menuBarButtonEndColor;
  784. var menuBarButtonFrameColor;
  785. var CustomToolbarStartColor;
  786. switch (typeColor) {
  787. case "purple": titleBarColor = 0xCC99CC; CustomToolbarStartColor = 0xCC99CC;menuBarStartColor = 0xCC99CC; menuBarButtonStartColor = 0xFFFFFF; menuBarButtonEndColor = 0xAD8DAD; menuBarButtonFrameColor = 0x663366; break;
  788. case "black": titleBarColor = 0x646464; CustomToolbarStartColor = 0x646464;menuBarStartColor = 0xB7B5B4; menuBarButtonStartColor = 0xF2EAF5; menuBarButtonEndColor = 0xF2EAF5; menuBarButtonFrameColor = 0xF2EAF5; break;
  789. case "white": titleBarColor = 0xC6C1BE; CustomToolbarStartColor = 0xC6C1BE;menuBarStartColor = 0xF5F3F2; menuBarButtonStartColor = 0xF2EAF5; menuBarButtonEndColor = 0xF2EAF5; menuBarButtonFrameColor = 0xF2EAF5; break;
  790. case "blue": titleBarColor = 0xD5B69F; CustomToolbarStartColor = 0xD5B69F;menuBarStartColor = 0xFCF3EF; menuBarButtonStartColor = 0xF2EAF5; menuBarButtonEndColor = 0xF2EAF5; menuBarButtonFrameColor = 0xF2EAF5; break;
  791. case "yellow": titleBarColor = 0x00CCFF; CustomToolbarStartColor = 0x00CCFF;menuBarStartColor = 0x6AB9FF; menuBarButtonStartColor = 0xF2EAF5; menuBarButtonEndColor = 0xF2EAF5; menuBarButtonFrameColor = 0xF2EAF5; break;
  792. }
  793. var style = this.obj.Style;
  794. style.TitleBarColor = titleBarColor;
  795. style.TitleBarTextColor = 0x000000;
  796. style.MenuBarStartColor = menuBarStartColor;
  797. style.MenuBarEndColor = 0xFFFFFF;
  798. style.MenuBarTextColor = 0x000000;
  799. style.MenuBarHighlightTextColor = 0x000000;
  800. style.MenuBarButtonStartColor = menuBarButtonStartColor;
  801. style.MenuBarButtonEndColor = menuBarButtonEndColor;
  802. style.MenuBarButtonFrameColor = menuBarButtonFrameColor;
  803. style.CustomToolbarStartColor=CustomToolbarStartColor;
  804. style.Invalidate();
  805. }
  806. /**
  807. ×××××××××××××××××××××××××××××××××××××××××××××××××××
  808. * End 皮肤
  809. * ×××××××××××××××××××××××××××××××××××××××××××××××××××
  810. */
  811. /**
  812. ×××××××××××××××××××××××××××××××××××××××××××××××××××
  813. * 手写签批功能
  814. * ×××××××××××××××××××××××××××××××××××××××××××××××××××
  815. */
  816. //添加手写签批工具栏
  817. this.AddToolbar=function() {
  818. //this.obj.Style.ShowCustomToolbar=true;
  819. this.obj.Handwritting.Show = !this.obj.Handwritting.Show;
  820. var URL = this.WebUrl.substring(0, this.WebUrl.lastIndexOf("/"));
  821. var customtoolbar = this.obj.CustomToolbar;
  822. customtoolbar.AddToolButton(301,"手写签名",URL+"/HandWrite/"+"shouxie24.png", "手写签名",0);
  823. customtoolbar.AddToolButton(302,"停止手写",URL+"/HandWrite/"+"tingzhi24.png","停止手写",0);
  824. customtoolbar.AddToolButton(303,"文字签名",URL+"/HandWrite/"+"wenzi24.png","文字签名",0);
  825. customtoolbar.AddToolButton(300,"-","","",1);
  826. customtoolbar.AddToolButton(304,"图形签名",URL+"/HandWrite/"+"tuxing24.png","图形签名",0);
  827. customtoolbar.AddToolButton(305,"取消上一个签批",URL+"/HandWrite/"+"quxiao24.png","取消上一个签批",0);
  828. customtoolbar.AddToolButton(306,"显示用户签批",URL+"/HandWrite/"+"xianshi24.png","显示用户签批",0);
  829. }
  830. //手写签批
  831. this.HandWriting = function(penColor,penWidth){
  832. var handwritting = this.obj.Handwritting;
  833. var handsetting = handwritting.DrawingSetting;
  834. handsetting.PenThicker = penWidth;
  835. handsetting.PenColor = penColor;
  836. handwritting.AnnotateDraw();
  837. this.ShowMenuBar(false);//签批时隐藏菜单栏
  838. this.ShowToolBars(false);//签批时隐藏工具栏
  839. }
  840. //停止手写签批
  841. this.StopHandWriting = function(){
  842. var handwritting = this.obj.Handwritting;
  843. handwritting.StopAnnotate();
  844. WebOffice.ShowMenuBar(true);//停止签批时显示菜单栏
  845. WebOffice.ShowToolBars(true);//停止签批时显示工具栏
  846. }
  847. //文字签名
  848. this.TextWriting = function(){
  849. var handwritting = this.obj.Handwritting;
  850. var textsetting = handwritting.TextSetting;
  851. textsetting.TextSize = 32;
  852. textsetting.TextColor = 0xbb00ff;
  853. textsetting.FontName = "宋体";
  854. handwritting.AnnotateText();
  855. this.ShowMenuBar(false);//签批时隐藏菜单栏
  856. this.ShowToolBars(false);//签批时隐藏工具栏
  857. }
  858. //图形签批
  859. this.ShapeWriting = function(){
  860. var handwritting = this.obj.Handwritting;
  861. var shapesetting = handwritting.ShapeSetting;
  862. shapesetting.ShapeType = 0;
  863. shapesetting.BackgroundColor = 0xffffff;
  864. shapesetting.BorderColor = 0xff0000;
  865. shapesetting.BorderWidth = 6;
  866. handwritting.AnnotateShape();
  867. this.ShowMenuBar(false);//签批时隐藏菜单栏
  868. this.ShowToolBars(false);//签批时隐藏工具栏
  869. }
  870. //取消上一次签批
  871. this.RemoveLastWriting = function(){
  872. var handwritting = this.obj.Handwritting;
  873. handwritting.RemoveLast();
  874. }
  875. //显示某用户的签批
  876. this.ShowWritingUser = function(bVal,username){
  877. var strxml = this.obj.GetAnnotations();
  878. var json = eval('(' + strxml + ')');
  879. if (username != "" && username != null && username!=undefined) {
  880. for(var i=0;i<json.Annotations.length;i++)
  881. {
  882. if(json.Annotations[i].Annotation.User != username) {
  883. var id = json.Annotations[i].Annotation.ID;
  884. this.obj.GetAnnotationByID(id).Visible = bVal;
  885. }
  886. }
  887. }else {
  888. for(var i=0;i<json.Annotations.length;i++)
  889. {
  890. var id = json.Annotations[i].Annotation.ID;
  891. this.obj.GetAnnotationByID(id).Visible = bVal;
  892. }
  893. }
  894. }
  895. //显示/隐藏手写签批工具栏
  896. this.ShowCustomToolbar = function(bVal){
  897. this.obj.Style.ShowCustomToolbar=bVal;
  898. //if(bVal==true) this.obj.Style.CustomToolbarStartColor=0xDDA0DD; //设置签批工具栏颜色
  899. }
  900. /**
  901. ×××××××××××××××××××××××××××××××××××××××××××××××××××
  902. * 手写签批功能
  903. * ×××××××××××××××××××××××××××××××××××××××××××××××××××
  904. */
  905. /**
  906. ×××××××××××××××××××××××××××××××××××××××××××××××××××
  907. * 扩展功能
  908. * ×××××××××××××××××××××××××××××××××××××××××××××××××××
  909. */
  910. //保存为PDF
  911. this.WebSavePDF = function(){
  912. var httpclient = this.obj.Http; //设置http对象
  913. httpclient.Clear();
  914. this.WebSetMsgByName("RECORDID",this.RecordID);
  915. this.WebSetMsgByName("OPTION","SAVEPDF");
  916. this.WebSetMsgByName("FILENAME",this.RecordID + ".pdf");//加载FileName
  917. this.SaveAsPdf(this.getFilePath()+this.RecordID + ".pdf");
  918. if(this.SAVEFILE(httpclient,this.getFilePath()+this.RecordID + ".pdf")){
  919. this.Status = "保存文件成功";
  920. alert("保存PDF成功!");
  921. return true;
  922. }
  923. this.Status = "保存文件失败";
  924. return false;
  925. }
  926. this.SaveAsPdf = function(FilePath){
  927. //var FilePath=this.getFilePath() + this.RecordID + ".pdf";
  928. if ((this.FileType == ".doc") || (this.FileType == ".docx") || (this.FileType == ".wps"))
  929. {
  930. try
  931. {
  932. this.obj.ActiveDocument.ExportAsFixedFormat(FilePath, 17, false, 0, 0, 1, 1, 0, true, true, 0, true, true, true);
  933. }
  934. catch (e)
  935. {
  936. return this.e.description;
  937. }
  938. return true;
  939. }
  940. }
  941. //保存为html
  942. this.SaveAsHtml = function(FilePath){
  943. if ((this.FileType == ".doc") || (this.FileType == ".docx") || (this.FileType == ".wps"))
  944. {
  945. try
  946. {
  947. var ret = this.obj.ActiveDocument.SaveAs(FilePath,8,false,"",false,"",false,false,false,false,false,0);
  948. this.obj.ActiveDocument.Application.ActiveWindow.View.type = 3;//3是页面视图
  949. }
  950. catch (e)
  951. {
  952. return this.e.description;
  953. }
  954. return true;;
  955. }
  956. }
  957. this.WebSaveHtml = function(){
  958. if(this.SaveAsHtml("D:\\"+this.RecordID + ".html"))
  959. {
  960. alert("保存Html成功!\n保存路径: "+"D:\\"+this.RecordID + ".html");
  961. }
  962. }
  963. //执行宏
  964. this.WebRunMacro = function(MarcroName,MacroValue){
  965. try{
  966. var VBAStr = MacroValue;
  967. var VBCom;
  968. VBCom = this.obj.ActiveDocument.VBProject.VBComponents.Add(1);
  969. VBCom.CodeModule.AddFromString(VBAStr);
  970. this.obj.ActiveDocument.Application.Run(MarcroName);
  971. return true;
  972. }catch(e){
  973. return false;
  974. }
  975. }
  976. /**
  977. ×××××××××××××××××××××××××××××××××××××××××××××××××××
  978. * 扩展功能
  979. * ×××××××××××××××××××××××××××××××××××××××××××××××××××
  980. */
  981. }