如何实现用jacob来调用word的宏
word操作是难题,JACOB是目前比较成熟的解决方案了,采用桥连的方式间接的操作word,效率不行啊。
如果要操作excel,为什么不用POI呢,这可是非常成熟的,word是没办法才用jacob的,这玩意效率太低,虽然简单。
宏到还真没用过,macroName这个是宏的名字吧,String subname = "RenameSheet";Dispatch.call(workbook, subname);这里你填个页签名干吗?
如何利用Java
1. 初始化com的线程,非常重要,否则第二次创建com对象的时候会出现can't co-create object异常 (参见jacob的帮助文档),完成操作com组件后要调用 realease方法 ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法 2. 初始化word应用程序,新建一个空白文档,取得文档内容对象//Instantiate objWord //Declare word object ActiveXComponent objWord = new ActiveXComponent("Word.Application"); //Assign a local word object Dispatch wordObject = (Dispatch) objWord.getObject(); //Create a Dispatch Parameter to show the document that is opened Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word应用程序可见 Tip:设置一个对象的属性的时候,利用Dispatch的put方法,给属性赋值。
上面这行语句相当于vb的 wordObject.Visible = true 语句 //Instantiate the Documents Property Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序) //Add a new word document, Current Active Document Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档 Tip:调用一个对象的方法的时候,利用Dispatch的call方法,上面的语句相当于vb的document = documents.Add() 语句。
Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容 Tip:取得一个对象的成员变量(属性)时利用Dispatch的get方法,上面的语句相当于vb的wordContent = document.Content语句 3. 取得word文档的内容后,可以对其内容进行操作 Dispatch.call(wordContent, "InsertAfter", "这里是一个段落的内容");//插入一个段落 4. 设置刚插入的段落的文字格式 Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落 int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数 // 找到刚输入的段落,设置格式 Dispatch lastParagraph = Dispatch.call(paragraphs, "Item", new Variant(paragraphCount)). toDispatch(); // 最后一段 Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range"). toDispatch(); Dispatch font = Dispatch.get(lastParagraphRange, "Font").toDispatch(); Dispatch.put(font, "Bold", new Variant(true)); // 设置为黑体 Dispatch.put(font, "Italic", new Variant(true)); // 设置为斜体 Dispatch.put(font, "Name", new Variant("宋体")); // Dispatch.put(font, "Size", new Variant(12)); //小四 注意:如果想插入一个新的空白行,也需要设置段落的文字格式,否则新插入行的文字格式会于刚插入的段落的格式相同。
5. 将当前文档保存 Dispatch.call(document, "SaveAs", new Variant("C: abc.doc")); // 保存一个新文档 6. 释放COM线程 ComThread.Release();//释放com线程。
根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理 完整测试代码:(StudyJacob.java 附件中有本文章和java源文件) import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; import com.jacob.com.ComThread; public class StudyJacob { public static void main(String[] args) { ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法 //Instantiate objWord //Declare word object ActiveXComponent objWord = new ActiveXComponent("Word.Application"); //Assign a local word object Dispatch wordObject = (Dispatch) objWord.getObject(); //Create a Dispatch Parameter to show the document that is opened Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word应用程序可见 //Instantiate the Documents Property Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序) //Add a new word document, Current Active Document Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档 Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容 Dispatch.call(wordContent, "InsertAfter", "这里是一个段落的内容");//插入一个段落 Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落 int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数 // 找到刚输入的段落,设置格式 Dispatch lastParagraph = Dispatch.call(paragraphs, "Item", new Variant(paragraphCount)). toDispatch(); // 最后一段 Dispatch lastParagraphRange = ...
java jacob 操作word,电脑上是不是必须要已经安装了office的word才可...
展开全部 word宏操作的问题,出现这种错误应该是你操作的word文档宏被禁用或者宏错误;不允许Jacob对此文档进行操作。
你只要用一个新的文档就可以了,另存为好像不行,必须新建文档将源文档内容拷贝过去,你试试吧……希望可以帮到你——汝州市文豪电脑...
怎么用freemarker生成的word文档需要另存为一下poi才能读
Jacob解决Word文档的读写问题收藏Jacob是Java-COMBridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。
使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。
Jacob下载的地址为:.Variant;publicclassWordOperate{publicstaticvoidmain(Stringargs[]){ActiveXComponentwordApp=newActiveXComponent("Word.Application");//启动word//Setthevisiblepropertyasrequired.Dispatch.put(wordApp,"Visible",newVariant(true));////设置word可见Dispatchdocs=wordApp.getProperty("Documents").toDispatch();//StringinFile="d:\\test.doc";//Dispatchdoc=Dispatch.invoke(docs,"Open",Dispatch.Method,//newObject[]{inFile,newVariant(false),newVariant(false)},//参数3,false:可写,true:只读//newint[1]).toDispatch();//打开文档Dispatchdocument=Dispatch.call(docs,"Add").toDispatch();//createnewdocumentStringuserName=wordApp.getPropertyAsString("Username");//显示用户信息System.out.println("用户名:"+userName);//文档对齐,字体设置////////////////////////Dispatchselection=Dispatch.get(wordApp,"Selection").toDispatch();Dispatchalign=Dispatch.get(selection,"ParagraphFormat").toDispatch();//行列格式化需要的对象Dispatchfont=Dispatch.get(selection,"Font").toDispatch();//字型格式化需要的对象//标题处理////////////////////////Dispatch.put(align,"Alignment","1");//1:置中2:靠右3:靠左Dispatch.put(font,"Bold","1");//字型租体Dispatch.put(font,"Color","1,0,0,0");//字型颜色红色Dispatch.call(selection,"TypeText","Word文档处理");//写入标题内容Dispatch.call(selection,"TypeParagraph");//空一行段落Dispatch.put(align,"Alignment","3");//1:置中2:靠右3:靠左Dispatch.put(selection,"Text","");Dispatch.call(selection,"MoveDown");//光标标往下一行//表格处理////////////////////////Dispatchtables=Dispatch.get(document,"Tables").toDispatch();Dispatchrange=Dispatch.get(selection,"Range").toDispatch();Dispatchtable1=Dispatch.call(tables,"Add",range,newVariant(3),newVariant(2),newVariant(1)).toDispatch();//设置行数,列数,表格外框宽度//所有表格VarianttableAmount=Dispatch.get(tables,"count");System.out.println(tableAmount);//要填充的表格Dispatcht1=Dispatch.call(tables,"Item",newVariant(1)).toDispatch();Dispatcht1_row=Dispatch.get(t1,"rows").toDispatch();//所有行intt1_rowNum=Dispatch.get(t1_row,"count").getInt();Dispatch.call(Dispatch.get(t1,"columns").toDispatch(),"AutoFit");//自动调整intt1_colNum=Dispatch.get(Dispatch.get(t1,"columns").toDispatch(),"count").getInt();System.out.println(t1_rowNum+""+t1_colNum);for(inti=1;i
VB 如何调用Word的宏代码
这里安装没有VB ,不过你试一试下面的代码Dim wdSel As ObjectSet wdSel = objWordP.SelectionwdSel.Find.ClearFormattingwdSel.Find.Replacement.ClearFormattingWith wdSel.Find.Text = "aa" '要查找的内容.Replacement.Text = "bb" '要替换的内容.Forward = True.Wrap = wdFindContinue.Format = False.MatchCase = False.MatchWholeWord = False.MatchByte = True.MatchWildcards = False.MatchSoundsLike = False.MatchAllWordForms = FalseEnd WithwdSel.Find.Execute , , , , , , , , , , wdReplaceAll
WORD宏中的“引用 Normal”是什么作用,为什么提示有加载宏
展开全部一般来说的例子sub b()msgbox "hi"end sub可以写成sub a()bend sub也可以写成sub a()call bend sub如果b有参数,sub b(a)msgbox aend sub可以写成sub a()b ("hi")end sub或者sub a()call b("hi")end suba,b要在同一个文档里,或者被调用的b在normal模板里
JACOB类使用问题
public class Jacob { /** * 打开文件 * * @param documents * @param inputDocPath * @return */ private Dispatch open(Dispatch documents, String inputDocPath) { return Dispatch.call(documents, "Open", inputDocPath).toDispatch(); } /** * 选定内容 * * @param word * @return */ private Dispatch select(ActiveXComponent word) { return word.getProperty("Selection").toDispatch(); } /** * 把插入点移动到文件首位置 * * @param selection */ private void moveStart(Dispatch selection) { Dispatch.call(selection, "HomeKey", new Variant(6)); } /** * 从选定内容或插入点开始查找文本 * * @param selection * 选定内容 * @param toFindText * 要查找的文本 * @return true:查找到并选中该文本;false:未查找到文本。
*/ private boolean find(Dispatch selection, String toFindText) { // 从selection所在位置开始查询 Dispatch find = Dispatch.call(selection, "Find").toDispatch(); // 设置要查找的内容 Dispatch.put(find, "Text", toFindText); // 向前查找 Dispatch.put(find, "Forward", "True"); // 设置格式 Dispatch.put(find, "format", "True"); // 大小写匹配 Dispatch.put(find, "MatchCase", "True"); // 全字匹配 Dispatch.put(find, "MatchWholeWord", "True"); // 查找并选中 return Dispatch.call(find, "Execute").getBoolean(); } /** * 把选定内容替换为设定文本 * * @param selection * @param newText */ private void replace(Dispatch selection, String newText) { Dispatch.put(selection, "Text", newText); } /** * 全局替换 * * @param selection * @param oldText * @param replaceObj */ private void replaceAll(Dispatch selection, String oldText, Object replaceObj) { moveStart(selection); String newText = (String) replaceObj; while (find(selection, oldText)) { replace(selection, newText); Dispatch.call(selection, "MoveRight"); } } /** * 打印 * * @param document */ private void print(Dispatch document) { Dispatch.call(document, "PrintOut"); } /** * 保存文件 * * @param word * @param outputPath */ private void save(ActiveXComponent word, String outputPath) { Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(), "FileSaveAs", outputPath); } /** * 关闭文件 * * @param doc */ private void close(Dispatch doc) { Dispatch.call(doc, "Close", new Variant(true)); } /** * 保存打印doc文档 * * @param inputDocPath * @param outPutDocPath * @param data * @param isPrint */ public void saveDoc(String inputDocPath, String outPutDocPath, HashMap data, boolean isPrint) { // 初始化com的线程 ComThread.InitSTA(); // word运行程序对象 ActiveXComponent word = new ActiveXComponent("Word.Application"); // 文档对象 Dispatch wordObject = (Dispatch) word.getObject(); // 设置属性 Variant(true)表示word应用程序可见 Dispatch.put((Dispatch) wordObject, "Visible", new Variant(false)); // word所有文档 Dispatch documents = word.getProperty("Documents").toDispatch(); // 打开文档 Dispatch document = this.open(documents, inputDocPath); Dispatch selection = this.select(word); Iterator keys = data.keySet().iterator(); String oldText; Object newValue; while (keys.hasNext()) { oldText = (String) keys.next(); newValue = data.get(oldText); this.replaceAll(selection, oldText, newValue); } // 是否打印 if (isPrint) { this.print(document); } this.save(word, outPutDocPath); this.close(document); word.invoke("Quit", new Variant[0]); // 关闭com的线程 ComThread.Release(); } }
转载请注明出处51数据库 » jacob调用word宏
Ashark