Criando tabela no Word usando JACOB

2 respostas
L

Olá Gujeiros,

Alguem poderia me ajudar como criar uma tabela no word utilizando o Jacob?

Eu consigo criar a estrutura dela,como quantidade de linhas e colunas...só não consigo colocar bordas nessa tabela.

Pesquisando pela net, vi para eu criar uma macro, desenhar a tabela e depois ver o código gerado em VB, mas ai tá o problema, não estou conseguindo passar esse código usando o Jacob, não sei quais parametros utilizar e não tenho muita familiaridade com o Jacob também. :?

Alguém tem um exemplo ou link que possa me ajudar?

Segue o código do que tenho + ou -:

ActiveXComponent wordApp = new ActiveXComponent( "Word.Application" );
			wordApp.setProperty( "Visible", new Variant( true ) );
			// create a document
			Dispatch documents = wordApp.getProperty( "Documents" ).toDispatch();
			Dispatch document = Dispatch.call( documents, "Add" ).toDispatch();
			// write some text
			Dispatch range = Dispatch.call( document, "Range", new Variant( 0 ), new Variant( 0 ) ).toDispatch();
			Dispatch.call( range, "Select" );
			Dispatch.call( range, "InsertAfter", "TABLE" );
			// select to end of line and add newline
			Dispatch selection = Dispatch.get( wordApp, "Selection" ).toDispatch();
			Dispatch.call( selection, "EndKey" );
			Dispatch.call( selection, "TypeParagraph" );
			// create a table
			Variant start = Dispatch.get( selection, "Start" );
			
			range = Dispatch.call( document, "Range", start, new Variant( Dispatch.get( selection, "End" ).getInt() + 1 ) ).toDispatch();
			Dispatch.call( range, "Select" );
			Dispatch wordBasic = Dispatch.call( wordApp, "WordBasic" ).toDispatch();
			Dispatch.call( wordBasic, "TextToTable", "1", new Variant( 5 ) );
			// add rows to table
			Dispatch tables = Dispatch.get( document, "Tables" ).toDispatch();			
			Dispatch table = Dispatch.call( tables, "Item", "1" ).toDispatch();
			for( int i = 0; i < 4; i++ ) {
			
				Dispatch cell = Dispatch.call( table, "Cell", "1", Integer.toString( i + 2 ) ).toDispatch();			
				Dispatch.call( cell, "Select" );
				range = Dispatch.get( cell, "Range" ).toDispatch();
				Dispatch.call( range, "InsertAfter", "col" + (i + 1) );
			}
			Dispatch rows = Dispatch.get( table, "Rows" ).toDispatch();
			for( int i = 0; i < 5; i++ ) {
				rows.call( rows, "Add" );
				Dispatch cell = Dispatch.call( table, "Cell", Integer.toString( i + 2 ), "1" ).toDispatch();
				Dispatch.call( cell, "Select" );
				range = Dispatch.get( cell, "Range" ).toDispatch();
				Dispatch.call( range, "InsertAfter", "row" + (i + 1) );			
			}
		}

2 Respostas

L

Alguma idéia?

andrenl

Boa noite Luan, estou também tentando implementar um projeto chamando objetos COM atraves do Jacob para manipular o Word.
Passei pelo mesmo problema que voce resolvi o problema das tabelas da seguinte forma

Dispatch border = Dispatch.call(table, "Borders", "-5").toDispatch();
        Dispatch.put(border, "Visible", new Variant(true));

        border = Dispatch.call(table, "Borders", "-6").toDispatch();
        Dispatch.put(border, "Visible", new Variant(true));

        border = Dispatch.call(table, "Borders", "-4").toDispatch();
        Dispatch.put(border, "Visible", new Variant(true));

        border = Dispatch.call(table, "Borders", "-3").toDispatch();
        Dispatch.put(border, "Visible", new Variant(true));

        border = Dispatch.call(table, "Borders", "-2").toDispatch();
        Dispatch.put(border, "Visible", new Variant(true));

        border = Dispatch.call(table, "Borders", "-1").toDispatch();
        Dispatch.put(border, "Visible", new Variant(true));

Mas ainda estou quebrando a cabeça com algumas coisas aqui :)

Criado 13 de janeiro de 2011
Ultima resposta 20 de fev. de 2011
Respostas 2
Participantes 2