Tablas en Office

Tablas en Office

Postby antolin » Fri Feb 11, 2011 11:42 am

Buenas a todos.

Hace un tiempo hice algunos pinitos creando documentos Office (Word y Excel) desde FiveWin, con TOLEAUTO, todo muy bien, los textos tenían comodines que se sustituían con campos de mis base de datos en tiempo de ejecución. Pero ahora me surge un problemilla, necesito crear, y rellenar tablas con Office (Word). ¿Alguien sabe que comandos hay que utilizar para:

- Crear una tabla
- Insertar/borrar filas en esa tabla
- Escribir/Borrar texto en una celda
- Cambiar el color de una fila/Celda
- Cambiar las propiedades de una fila/Celda

Una punctualización, entonces trabajaba con Ofiice 2003. ¿Se trabaja igual con el 2007?

Gracias de antemano.
Peaaaaaso de foro...
antolin
 
Posts: 492
Joined: Thu May 10, 2007 8:30 pm
Location: Sevilla

Re: Tablas en Office

Postby anserkk » Sat Feb 12, 2011 5:36 am

This program reads the Customer.DBF available in \FWH\Samples folder and dumps the data onto a table on a word document

Code: Select all  Expand view
Include "FiveWin.ch"

#DEFINE wdAlignParagraphLeft    0
#DEFINE wdAlignParagraphCentre  1
#DEFINE wdAlignParagraphRight   2

#DEFINE wdStory                 6
#DEFINE wdCollapseEnd           0

#DEFINE wdBorderTop            -1
#DEFINE wdLineStyleDouble       7

#DEFINE CR                     CHR(13)

//--------------------------//
Function Main()

Local oWord,oRange,oTable,nRecCount,nRow,nTotSalary:=0

USE \FWH\Samples\CUSTOMER
nRecCount:=RecCount()

oWord:=CREATEOBJECT("Word.Application")
oWord:Documents:Add()

oRange:=oWord:ActiveDocument:Range()

// Move to the end of the document, leave 2 empty lines
oRange:MoveEnd( wdStory )
oRange:Collapse( wdCollapseEnd )
oRange:InsertAfter( CR + CR )
oRange:Collapse( wdCollapseEnd )

// Add a table with 2 rows and 3 columns
oTable:=oWord:ActiveDocument:Tables:Add(oRange,2,3)

WITH OBJECT oTable
   // Set up borders and shading
   // If u dont want borders then set the below lines 2 lines to .F.
   :Borders:InsideLineStyle:=.T.
   :Borders:OutsideLineStyle:=.T.

   // Shade first row for headings
   :Rows[1]:Shading:Texture = 100


   // Put heading text in and set alignment
   :Cell(1,1):Range:ParagraphFormat:Alignment:=wdAlignParagraphLeft
   :Cell(1,1):Range:InsertAfter("Last Name")  
   
   
   :Cell(1,2):Range:ParagraphFormat:Alignment:=wdAlignParagraphRight
   :Cell(1,2):Range:InsertAfter("Salary")

   :Cell(1,3):Range:ParagraphFormat:Alignment:=wdAlignParagraphLeft
   :Cell(1,3):Range:InsertAfter("Hire Date")

   // Format data cells
   :Cell(2,1):Range:ParagraphFormat:Alignment:=wdAlignParagraphLeft
   :Cell(2,2):Range:ParagraphFormat:Alignment:=wdAlignParagraphRight
   :Cell(2,3):Range:ParagraphFormat:Alignment:=wdAlignParagraphLeft

   // Add data and format
   nTotSalary:=0  
   For nRow:=1 to 20 // nRecCount

       WITH OBJECT :Rows[nRow + 1]    
           :Cells[1]:Range:InsertAfter( Customer->LAST  )
           :Cells[2]:Range:InsertAfter( Customer->SALARY  )      
           :Cells[3]:Range:InsertAfter( Customer->HIREDATE  )

       END
       
       // Add a new Row
       :Rows:Add()
       
       // Calculating total
       nTotSalary+=Customer->SALARY

       Skip

   Next
   
   // Total row shade and place total
   :Rows[ nRow + 1 ]:Shading:Texture = 100
   WITH OBJECT :Rows[ nRow + 1 ]
       :Cells[1]:Range:InsertAfter("Total Salary")
       :Cells[2]:Range:InsertAfter(nTotSalary)
   
   END
   
   // Size columns, for simplicity, let word do the work
   :Columns:Autofit()
END
oWord:ActiveDocument:SaveAs("D:\Anser")
oWord:ActiveDocument:Close()
oWord:Quit()
MsgInfo("Finished")
Return


Screen Snapshot
Image

Regards
Anser
User avatar
anserkk
 
Posts: 1331
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Tablas en Office

Postby antolin » Mon Feb 14, 2011 6:56 am

Muchas gracias anserkk, es exactamente lo que estaba buscando. Una maravilla.
Peaaaaaso de foro...
antolin
 
Posts: 492
Joined: Thu May 10, 2007 8:30 pm
Location: Sevilla

Re: Tablas en Office

Postby antolin » Mon Feb 14, 2011 7:52 am

Sólo una cosa más, si no es mucha molestia.

Mi intención, es modificar/completar una archivo modelo, entonces:

1- Cómo añado texto al final de, por ejemplo, la 3ª línea.
2- Cómo selecciono una tabla ya existente para modificarlla.

Gracias
Peaaaaaso de foro...
antolin
 
Posts: 492
Joined: Thu May 10, 2007 8:30 pm
Location: Sevilla

Re: Tablas en Office

Postby anserkk » Mon Feb 14, 2011 10:01 am

antolin wrote:Sólo una cosa más, si no es mucha molestia.

Mi intención, es modificar/completar una archivo modelo, entonces:

1- Cómo añado texto al final de, por ejemplo, la 3ª línea.
2- Cómo selecciono una tabla ya existente para modificarlla.

Gracias


The following code will read data from an existing table available in a word file and will modify the contents of the table. You may first create the word file using my sample code posted above.
Code: Select all  Expand view

#Include "FiveWin.ch"

//--------------------------//
Function Main()

Local oWord,oDoc,oTable,nRow,nCol,nColor

oWord:=CREATEOBJECT("Word.Application")
oDoc = oWord:Documents:Open("D:\Anser.docx")

oTable = oDoc:Tables:Item(1) // 1 means, First table on the document

WITH OBJECT oTable
    nColor:=1
    For nRow = 2 To oTable:Rows:Count
        For nCol = 1 To oTable:Columns:Count
       
            // Changing the text color of the cell
            :Cell(nRow, nCol):Range:Font:ColorIndex = nColor
           
            // Write to the table cell
            :Cell(nRow, nCol):Range:Text = "Replaced with " + Str(nRow - 1)
           
            // If you want to read the contents then
            // MsgInfo( :Cell(nRow, nCol):Range:Text )
           
            iif(nColor == 16,nColor:=1,nColor++)
        Next
    Next
   
    // Size columns, for simplicity, let word do the work
    :Columns:Autofit()
END

oWord:Visible := .T.
Return


Screen Snapshot
Image


Regards
Anser
User avatar
anserkk
 
Posts: 1331
Joined: Fri Jun 13, 2008 11:04 am
Location: Kochi, India

Re: Tablas en Office

Postby antolin » Tue Feb 15, 2011 11:42 am

Muchas Gracias Anser, con esto me sobra para empezar.

Saludos
Peaaaaaso de foro...
antolin
 
Posts: 492
Joined: Thu May 10, 2007 8:30 pm
Location: Sevilla

Re: Tablas en Office

Postby fergonm » Thu Mar 10, 2011 7:20 pm

Buenas tardes.

Intento enlazar el ejemplo de ANSERKK pero tengo el siguiente error "Unresolved external HB_FUN_CREATEOBJECT"

Creo que se trata de una libería de Hb que me falta. Enlazo con HbOLE y OLE2.

Un saludo
Saludos. Fernando
fergonm
 
Posts: 133
Joined: Fri Nov 30, 2007 11:34 am
Location: Zaragoza (España)

Re: Tablas en Office

Postby Daniel Garcia-Gil » Fri Mar 11, 2011 2:47 am

Saludos

que version de [x]Harbour usas?
User avatar
Daniel Garcia-Gil
 
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 pm
Location: Isla de Margarita

Re: Tablas en Office

Postby fergonm » Fri Mar 11, 2011 8:16 am

Buenos días Daniel.

La versión que utilizo creo que es la 1.4, que es la que descargué de Five Tech al adquirir FWH 7.07

Un saludo. Fernando
Saludos. Fernando
fergonm
 
Posts: 133
Joined: Fri Nov 30, 2007 11:34 am
Location: Zaragoza (España)


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 67 guests