Ver Mensaje Individual
  #8  
Antiguo 28-01-2008
Avatar de ixMike
ixMike ixMike is offline
Miembro
 
Registrado: feb 2004
Posts: 1.151
Reputación: 24
ixMike Va por buen camino
Creo (no estoy demasiadao puesto en C++) que la traducción a Object Pascal sería:


Código Delphi [-]
// CREO EL DOCUMENTO A PARTIR DE UNA PLANTILLA QUE ABRO COMO SOLO LECTURA
var
  Word, doc, Selection, Font, Parrafo, rango, tablas, tabla, celda: Variant;
  rows, cols, i, j: integer;
begin
  word := Variant.CreateObject('Word.Application');
  word.OlePropertySet( 'Visible', (Variant) true );
  doc := word.OlePropertyGet('Documents');
  if FileExists(ExtractFilePath(Application.ExeName)+ 'plantillas\plantilla.doc')) then
    doc.OleFunction('Open', (ExtractFilePath(Application.ExeName)+ 'plantillas\plantilla.doc').c_str, Unassigned, true);
  else
    begin
     MessageBox(Handle, 'El archivo no existe', 'Error' , MB_APPLMODAL or MB_OK or MB_ICONERROR);
     word.OleProcedure('Quit');
     Exit;
    end;
// CREO EL ENCABEZADO
  Selection := word.OlePropertyGet('Selection');
  Font := Selection.OlePropertyGet('Font');
  Parrafo := Selection.OlePropertyGet('ParagraphFormat');
  Parrafo.OlePropertySet('Alignment','1');
  Font.OlePropertySet('Name','Verdana');
  Font.OlePropertySet('Bold','1');
  Font.OlePropertySet('Size','12');
  Selection.OleProcedure('TypeText', 'TÍTULO'+#13#13#13);
  Parrafo.OlePropertySet('Alignment','0');
  Font.OlePropertySet('Size','10');
  Selection.OleProcedure('TypeText', 'Nombre:'+#9);
  Font.OlePropertySet('Bold','0');
  Selection.OleProcedure('TypeText', (cboNombre.Text + #13).c_str);
  Font.OlePropertySet('Bold','1');
  Selection.OleProcedure('TypeText', 'Fecha:\t');
  Font.OlePropertySet('Bold','0');
  Selection.OleProcedure('TypeText', (txtFecha.Text + #13#13).c_str);
 
  // Lo anterior te generará ésto:
  //
  //     TÍTULO
  //
  // Nombre: Gabo
  // Fecha: 03/12/2008
  //

 // CREO LA TABLA 
  doc := word.OlePropertyGet('ActiveDocument');
  rango := doc.OleFunction('Range');
  rango.OleProcedure('Collapse', 0);
  tablas := rango.OlePropertyGet('Tables');
  rows := ListView1.Items.Count + 1; 
  // En este ejemplo creo tantas filas como ítemes en un ListView (+ 1 para los encabezados de columna)
  cols := 2;
  Parrafo.OlePropertySet('Alignment','0');
  tabla := tablas.OleFunction('Add', rango, rows, cols);
  celda := tabla.OleFunction('Cell', 1, 1);
  celda.OlePropertyGet('Range').OleProcedure('InsertAfter', 'Producto');
  celda := tabla.OleFunction('Cell', 1, 2);
  celda.OlePropertyGet('Range').OleProcedure('InsertAfter', 'Cantidad');
  // A continuación recorro el ListView para ir poblando de datos la tabla
  i:=1;
  while i < rows do
   begin
    j:=1;
    while j<=cols
      begin
        if j=1
          begin
             celda := tabla.OleFunction('Cell', i+1, j);
             celda.OlePropertyGet('Range').OleProcedure('InsertAfter', (ListView1.Items.Item[i-1].Caption).c_str);
          end
         else
          begin
             celda := tabla.OleFunction('Cell', i+1, j);
             celda.OlePropertyGet('Range').OleProcedure('InsertAfter', (ListView1.Items.Item[i-1].SubItems[0][0]).c_str);
          end;
       Inc(j);
      end;
    Inc(i);
   end;
  // Lo siguiente es para salir de la tabla y poder seguir insertando texto normal
  Selection.OleProcedure('EndKey', 6);


Espero no haber cometido ningún error.


Saludos.

Última edición por ixMike fecha: 28-01-2008 a las 15:21:01.
Responder Con Cita