JACOB操作WORD问题 现在情况是这样,我在手工打开一个WORD的情况下,然后用JACOB在打开一个程序就会报错,请问这个是什么原因?或者说我用JACOB打开一个WORD后,又用JACOB打开了一个WORD,这个WORD打开时就会报错,请问用JACOB连着打开2个WORD怎么解决,在线等,谢谢大家 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 报错是说word无法打开,因为你在别的地方同时打开了次文件 不知道你的问题在哪 我刚试了下 ,写的没有你这个问题。 代码如下:package com.mycode.util;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import javax.servlet.ServletOutputStream;import com.jacob.com.*;import com.jacob.activeX.*;public class WordBean{ private ActiveXComponent MsWordApp = null; private Dispatch document = null; private Dispatch selection = null; public WordBean() { } public void openWord( boolean makeVisible ) {//Open Word if we\'ve not done it already if ( MsWordApp == null ) { MsWordApp = new ActiveXComponent( "Word.Application" ); }//Set the visible property as required. Dispatch.put( MsWordApp, "Visible", new Variant( makeVisible ) ); } public void createNewDocument() {//Find the Documents collection object maintained by Word Dispatch documents = Dispatch.get( MsWordApp, "Documents" ).toDispatch();//Call the Add method of the Documents collection to create//a new document to edit selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch(); document = Dispatch.call( documents, "Add" ).toDispatch(); } /** * 创建表格 * * @param pos * 位置 * @param cols * 列数 * @param rows * 行数 */ public void createTable( int numCols, int numRows) { Dispatch tables = Dispatch.get(document, "Tables").toDispatch(); selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch(); Dispatch range = Dispatch.get(selection, "Range").toDispatch(); Dispatch newTable = Dispatch.call(tables, "Add", range, new Variant(numRows), new Variant(numCols)).toDispatch(); Dispatch.call(selection, "MoveRight"); } //填写内容 public void insertText( String textToInsert ) {// Get the current selection within Word at the moment. If// a new document has just been created then this will be at// the top of the new doc Dispatch selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch(); moveDown( 5 ); moveRight( 5 );//Put the specified text at the insertion point Dispatch.put( selection, "Text", textToInsert ); } //另存为 保存文件 public void saveFileAs( String filename ) { Dispatch.call( document, "SaveAs", filename ); } //保存文件 public void saveFile() { Dispatch.call( document, "SaveAs"); } public void printFile() {//Just print the current document to the default printer Dispatch.call( document, "PrintOut" ); } //关闭文档 public void closeDocument() {// Close the document without saving changes// 0 = wdDoNotSaveChanges// -1 = wdSaveChanges// -2 = wdPromptToSaveChanges Dispatch.call( document, "Close", new Variant( 0 ) ); document = null; } //删除文档 public void deleteDocument(String docPath) { Dispatch.call( document, "Delete",docPath); } //关闭word对象 public void closeWord() { Dispatch.call( MsWordApp, "Quit" ); MsWordApp = null; document = null; } //选择要填充的表格 public void putTxtToCell( int tableIndex, int cellRowIdx, int cellColIdx, String txt ) { // 所有表格 Dispatch tables = Dispatch.get( document, "Tables" ).toDispatch(); // 要填充的表格 Dispatch table = Dispatch.call( tables, "Item", new Variant( tableIndex ) ) .toDispatch(); Dispatch cell = Dispatch.call( table, "Cell", new Variant( cellRowIdx ), new Variant( cellColIdx ) ).toDispatch(); Dispatch.call( cell, "Select" ); if ( selection == null ) { selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch(); } Dispatch.put( selection, "Text", txt); } /** * 打开一个已存在的文档 * * @param docPath */ public void openDocument(String docPath) { try{ Dispatch documents = Dispatch.get( MsWordApp, "Documents" ).toDispatch(); document = Dispatch.call( documents, "Open", docPath ).toDispatch(); }catch (ComFailException e){ throw new ComFailException ( docPath + " 找不到该文件, 无法打开 \n" + e.getMessage()); } } //左移 public void moveLeft( int pos ) { if ( selection == null ) { selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch(); } for ( int i = 0; i < pos; i++ ) { Dispatch.call( selection, "MoveLeft" ); } } //右移 public void moveRight( int pos ) { if ( selection == null ) { selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch(); } for ( int i = 0; i < pos; i++ ) { Dispatch.call( selection, "MoveRight" ); } } //下移 public void moveDown( int pos ) { if ( selection == null ) { selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch(); } for ( int i = 0; i < pos; i++ ) { Dispatch.call( selection, "MoveDown" ); } } //上移 public void moveUp( int pos ) { if ( selection == null ) { selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch(); } for ( int i = 0; i < pos; i++ ) { Dispatch.call( selection, "MoveUp" ); } } //增加一行 public void addRow(int tableIndex) { Dispatch tables = Dispatch.get(document, "Tables").toDispatch(); // 要填充的表格 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)) .toDispatch(); // 表格的所有行 Dispatch rows = Dispatch.get(table, "Rows").toDispatch(); Dispatch.call(rows, "Add"); } public static void main( String[] args ) { WordBean word = new WordBean(); word.openWord( true ); // word.openDocument("c:\\ccc.doc"); word.createNewDocument(); // word.openDocument("c:\\ccc.doc"); word.createTable( 4,4); word.putTxtToCell( 1, 2, 2, "tangwei " ); word.putTxtToCell( 1, 2, 4, "JAVA工程师 " ); // word.insertText( "sinitek " ); word.saveFileAs( "c:\\testlcl.doc" ); }} 有段错误的代码是关于的网络的也是流Io读的错误,有意思的问我要 如何去掉文本里面的"\n" JAVA程序;号可以无处不在~~? 一个特殊的带参数查询问题~! 在哪里可以下载到Jbuilder9.0? 使用Java的勇气号都动不了啦,放弃java吧 一个超级菜鸟的表白:我简直是颗白菜,连final都不懂,帮我啊! 兄弟们! 为什么我运行《Thinking In Java》中的Socket例子的代码会报错啊!!! 高分! 这本书怎么样,大侠帮忙看一下! 请大家帮帮忙。 数字格式保存问题 ant的使用问题
import java.io.FileInputStream;
import java.io.IOException;import javax.servlet.ServletOutputStream;import com.jacob.com.*;
import com.jacob.activeX.*;public class WordBean
{
private ActiveXComponent MsWordApp = null;
private Dispatch document = null;
private Dispatch selection = null; public WordBean()
{ } public void openWord( boolean makeVisible )
{
//Open Word if we\'ve not done it already
if ( MsWordApp == null )
{
MsWordApp = new ActiveXComponent( "Word.Application" );
}//Set the visible property as required.
Dispatch.put( MsWordApp, "Visible",
new Variant( makeVisible ) );
} public void createNewDocument()
{
//Find the Documents collection object maintained by Word
Dispatch documents =
Dispatch.get( MsWordApp, "Documents" ).toDispatch();
//Call the Add method of the Documents collection to create
//a new document to edit
selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch(); document = Dispatch.call( documents, "Add" ).toDispatch();
} /**
* 创建表格
*
* @param pos
* 位置
* @param cols
* 列数
* @param rows
* 行数
*/
public void createTable( int numCols, int numRows) {
Dispatch tables = Dispatch.get(document, "Tables").toDispatch();
selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch();
Dispatch range = Dispatch.get(selection, "Range").toDispatch();
Dispatch newTable = Dispatch.call(tables, "Add", range,
new Variant(numRows), new Variant(numCols)).toDispatch();
Dispatch.call(selection, "MoveRight");
} //填写内容
public void insertText( String textToInsert )
{
// Get the current selection within Word at the moment. If
// a new document has just been created then this will be at
// the top of the new doc
Dispatch selection =
Dispatch.get( MsWordApp, "Selection" ).toDispatch();
moveDown( 5 );
moveRight( 5 );//Put the specified text at the insertion point
Dispatch.put( selection, "Text", textToInsert );
} //另存为 保存文件
public void saveFileAs( String filename )
{
Dispatch.call( document, "SaveAs", filename );
} //保存文件
public void saveFile()
{
Dispatch.call( document, "SaveAs");
} public void printFile()
{
//Just print the current document to the default printer
Dispatch.call( document, "PrintOut" );
} //关闭文档
public void closeDocument()
{
// Close the document without saving changes
// 0 = wdDoNotSaveChanges
// -1 = wdSaveChanges
// -2 = wdPromptToSaveChanges
Dispatch.call( document, "Close", new Variant( 0 ) );
document = null;
} //删除文档
public void deleteDocument(String docPath)
{
Dispatch.call( document, "Delete",docPath);
}
//关闭word对象
public void closeWord()
{
Dispatch.call( MsWordApp, "Quit" );
MsWordApp = null;
document = null;
}
//选择要填充的表格
public void putTxtToCell( int tableIndex, int cellRowIdx, int cellColIdx,
String txt )
{
// 所有表格
Dispatch tables = Dispatch.get( document, "Tables" ).toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call( tables, "Item", new Variant( tableIndex ) )
.toDispatch();
Dispatch cell = Dispatch.call( table, "Cell", new Variant( cellRowIdx ),
new Variant( cellColIdx ) ).toDispatch();
Dispatch.call( cell, "Select" );
if ( selection == null )
{
selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch();
}
Dispatch.put( selection, "Text", txt);
} /**
* 打开一个已存在的文档
*
* @param docPath
*/
public void openDocument(String docPath)
{
try{
Dispatch documents = Dispatch.get( MsWordApp, "Documents" ).toDispatch(); document = Dispatch.call( documents, "Open", docPath ).toDispatch();
}catch (ComFailException e){
throw new ComFailException ( docPath + " 找不到该文件, 无法打开 \n" + e.getMessage());
}
} //左移
public void moveLeft( int pos )
{
if ( selection == null )
{
selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch();
}
for ( int i = 0; i < pos; i++ )
{
Dispatch.call( selection, "MoveLeft" );
}
} //右移
public void moveRight( int pos )
{
if ( selection == null )
{
selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch();
}
for ( int i = 0; i < pos; i++ )
{
Dispatch.call( selection, "MoveRight" );
}
} //下移
public void moveDown( int pos )
{
if ( selection == null )
{
selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch();
}
for ( int i = 0; i < pos; i++ )
{
Dispatch.call( selection, "MoveDown" );
}
} //上移
public void moveUp( int pos )
{
if ( selection == null )
{
selection = Dispatch.get( MsWordApp, "Selection" ).toDispatch();
}
for ( int i = 0; i < pos; i++ )
{
Dispatch.call( selection, "MoveUp" );
}
}
//增加一行
public void addRow(int tableIndex) {
Dispatch tables = Dispatch.get(document, "Tables").toDispatch();
// 要填充的表格
Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex))
.toDispatch();
// 表格的所有行
Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
Dispatch.call(rows, "Add");
} public static void main( String[] args )
{
WordBean word = new WordBean();
word.openWord( true ); // word.openDocument("c:\\ccc.doc");
word.createNewDocument();
// word.openDocument("c:\\ccc.doc");
word.createTable( 4,4); word.putTxtToCell( 1, 2, 2, "tangwei " );
word.putTxtToCell( 1, 2, 4, "JAVA工程师 " );
// word.insertText( "sinitek " ); word.saveFileAs( "c:\\testlcl.doc" ); }
}