JScript implementation of Microsoft Office VBA ActiveX programming

Source: Internet
Author: User

This article highlights:

    1. Using VBArray in JScript
    2. Calling methods in JScript that need to pass in a VB array
    3. Calling VBA's named parameterized method in JScript
    4. Example Word.Application file write, picture Insert, file save, etc.
    5. Example Excel.Application cells, multiple rows, region replication, and so on

/************************* Common Functions **********************************//* tips:an alert Emulator for Wscript Host */function alert (msg) {WScript.Echo (msg+ "");} /* Tips:auto Close popup in 3 seconds */function popup (msg,delay) {delay = Delay | | 3 var sh = new ActiveXObject ("W Script.    Shell ");    var tip = "Tips in%d Seconds". Replace ("%d", delay);  Sh.popup (msg+ "", delay, Tip, 64); }/* tips:use safe VB array for VBArray in JScript 8.0, the This method can is use in multiple VB array * http://stackoverflow . Com/questions/848246/how-can-i-use-javascript-within-an-excel-macro * Return Keys as a SafeArray, then can is Use:new VB Array (SafeArray);    */function Tovbarray (Jsarray) {var dict = new ActiveXObject ("Scripting.Dictionary"); for (var i in Jsarray) Dict.    ADD (i, jsarray[i]); Return Dict. Keys ();} /************************* Office Word Application ***************************/var wdsaveoptions = { wddonotsavechanges:0, Wdprompttosavechanges:-2, wdsavechanges:-1}var WdBuiltinstyle = {wdstylenormal:-1,//body.     Wdstyleheading1:-2,//title 1~10. Wdstyleheading2:-3, Wdstyleheading3:-4, wdstyleheading4:-5, wdstyleheading5:-6, wdstyleheading6:-7, WdStyle     Heading7:-8, Wdstyleheading8:-9, wdstyleheading9:-10, wdstylenormalindent:-29,//body indent.    wdstylebodytext:-67,//body text.     wdstylehyperlink:-86,//hyperlinks.     wdstyleplaintext:-91,//plain text.     wdstylequote:-181//reference.     ... and More};var WdSaveFormat = {wdformatdocument:0,//Microsoft Office Word format.     Wdformatdostext:4,//Microsoft DOS text format.     Wdformatdostextlinebreaks:5,//Microsoft DOS text format and preserves line breaks.     Wdformatencodedtext:7,//encoded text format.     Wdformatfilteredhtml:10,//filtered HTML format.     Wdformathtml:8,//Standard HTML format.     Wdformatrtf:6,//RTF format.     Wdformattemplate:1,//Word template format.     wdformatdocument97:0,//Microsoft Word 97 document format. WDFORMATDOCUMENTDEFAULT:16//Word default document file format.     For Microsoft Office Word 2007, this is the DOCX format. ...};/* 1. Create WinworD Application and let it visible */var Word = new ActiveXObject ("Word.Application"); Word.visible = true; Word.displayalerts = false;/* 2. Prepare a new document to writing, Note:the method ADD does follow parentheses */var Doc = WORD.DOCUMENTS.ADD ();/* 3. Writes a contents and set style */var P = Doc.Paragraphs.Add ();p. Range.Text = "comming";p. style = Doc.styles (wdbuiltinstyl E.WDSTYLEHEADING1); var p = Doc.Paragraphs.Add (); Create a new paragraph but the Range is the SAMEP. Range.Text + = "Hot baby to this document";p. Style = Doc.styles (wdbuiltinstyle.wdstylenormal); var p = Doc.Paragraphs.Add () ; P.range.text + = "Hot baby to this document";//p.range.insertfile ("./some.txt.xls.doc");p opup ("Insert picture", 6);D OC. SaveAs ("./t.doc", wdsaveformat.wdformatdocument97); var path = "./md1.png"; var p = Doc.Paragraphs.Add (); Add empty on Avoid replace when Paste (). var p = Doc.Paragraphs.Add ();p. Range.Text = "PLACEHOLDER"; var ishape = Doc.inli Neshapes.addpicture (path); Shapes or InlineshapesishApe. Range.cut ();//ishape.range.select ();//word.selection.cut ();p. Range.Paste ();p opup ("Winword is ready to exit", 6);/* 4. Delete or Save you document */doc.save ();//doc.close (wdsaveoptions.wddonotsavechanges);//doc.saveas ("./t.doc");/* 5. Writes a paragraph */word.quit ();/*************************** Office Excel Application ************************//* 1. Create Excel application, and setting options * Excel.Application can be found in Reg Hkcr\excel.application or EXCEL.E XE */var Excel = new ActiveXObject ("Excel.Application");//var Sheet = new ActiveXObject ("Excel.Sheet"); Excel.visible = true; Excel.displayalerts = false;/* 2. Prepare Workbook to Working, Note:the method Add not follow parentheses */var Workbook = excel.workbooks.add/* Tips:reor Der Sheets use named parameters method in VBA */workbook.sheets ("Sheet1"). Move (NULL, Workbook.sheets ("Sheet3")); Move after sheet2/* Tips:delete wasted sheets */var a = Tovbarray (["Sheet1", "Sheet2", "Sheet3"]); Workbook.sheets (a). Delete ();* 3. Optionally you can create a new Sheet */var Sheet = EXCEL.SHEETS.ADD; Sheet.name = "New Sheet"; Sheet.cells (a). Value = "This is column A, row 1";//workbook.sheets ("Sheet1"). Move ("After", Workbook.sheets ("Sheet2"));/* 4. Optionally save or delete your work *///workbook.close (false); Workbook.saveas ("./t.xls");p opup ("T.xls save to:\r\n" +sheet.application.activeworkbook.path);/* 5. Keep away Close application */excel.quit ();

Finally, the author absolutely has reason to believe that at the end of the article to add mywife.cc such words will greatly increase the amount of this article views! haha:)


JScript implementation of Microsoft Office VBA ActiveX programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.