viernes, noviembre 04, 2011

Crear un fichero .doc por programación

A veces puede interesar automatizar por programación la creación de un fichero .doc (Word).

Esto te puede ayudar en dos casos:


  1. Si generas periódicamente, o frecuentemente, el mismo tipo de documento con ligeras variaciones (facturas, informes, etc.)



  2. Si tienes que crear un documento a partir de un montón de imágenes, ficheros, otros documentos...

Por ejemplo organismos que generan miles de de documentos diarios como los tribunales de Ginebra utilizan un módulo Perl: MsOffice-Word-HTML-Writer-1.01

Para usar ese módulo Perl no hace falta ni Windows, ni tener el Word instalado.

Código fuente copiado del enlace indicado:


use MsOffice::Word::HTML::Writer;
my $doc = MsOffice::Word::HTML::Writer->new(
title => "My new doc",
WordDocument => {View => 'Print'},
);

$doc->write("<p>hello, world</p>",
$doc->page_break,
"<p>hello from another page</p>");

$doc->create_section(
page => {size => "21.0cm 29.7cm",
margin => "1.2cm 2.4cm 2.3cm 2.4cm"},
header => sprintf("Section 2, page %s of %s",
$doc->field('PAGE'),
$doc->field('NUMPAGES')),
footer => sprintf("printed at %s",
$doc->field('PRINTDATE')),
new_page => 1, # or 'left', or 'right'
);
$doc->write("this is the second section, look at header/footer");

$doc->attach("my_image.gif", $path_to_my_image);
$doc->write("<img src='files/my_image.gif'>");

$doc->save_as("/path/to/some/file");


Si tenemos que hacerlo en un PC Windows va a ser más sencillo utilizar VB.NET como nos explican aquí: Cómo automatizar Word desde Visual Basic .NET para crear un nuevo documento


Código fuente copiado del enlace:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
Dim oPara3 As Word.Paragraph, oPara4 As Word.Paragraph
Dim oRng As Word.Range
Dim oShape As Word.InlineShape
Dim oChart As Object
Dim Pos As Double

'Start Word and open the document template.
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add

'Insert a paragraph at the beginning of the document.
oPara1 = oDoc.Content.Paragraphs.Add
oPara1.Range.Text = "Heading 1"
oPara1.Range.Font.Bold = True
oPara1.Format.SpaceAfter = 24 '24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter()

'Insert a paragraph at the end of the document.
'** \endofdoc is a predefined bookmark.
oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
oPara2.Range.Text = "Heading 2"
oPara2.Format.SpaceAfter = 6
oPara2.Range.InsertParagraphAfter()

'Insert another paragraph.
oPara3 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
oPara3.Range.Text = "This is a sentence of normal text. Now here is a table:"
oPara3.Range.Font.Bold = False
oPara3.Format.SpaceAfter = 24
oPara3.Range.InsertParagraphAfter()

'Insert a 3 x 5 table, fill it with data, and make the first row
'bold and italic.
Dim r As Integer, c As Integer
oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)
oTable.Range.ParagraphFormat.SpaceAfter = 6
For r = 1 To 3
For c = 1 To 5
oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
Next
Next
oTable.Rows.Item(1).Range.Font.Bold = True
oTable.Rows.Item(1).Range.Font.Italic = True

'Add some text after the table.
'oTable.Range.InsertParagraphAfter()
oPara4 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
oPara4.Range.InsertParagraphBefore()
oPara4.Range.Text = "And here's another table:"
oPara4.Format.SpaceAfter = 24
oPara4.Range.InsertParagraphAfter()

'Insert a 5 x 2 table, fill it with data, and change the column widths.
oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 5, 2)
oTable.Range.ParagraphFormat.SpaceAfter = 6
For r = 1 To 5
For c = 1 To 2
oTable.Cell(r, c).Range.Text = "r" & r & "c" & c
Next
Next
oTable.Columns.Item(1).Width = oWord.InchesToPoints(2) 'Change width of columns 1 & 2
oTable.Columns.Item(2).Width = oWord.InchesToPoints(3)

'Keep inserting text. When you get to 7 inches from top of the
'document, insert a hard page break.
Pos = oWord.InchesToPoints(7)
oDoc.Bookmarks.Item("\endofdoc").Range.InsertParagraphAfter()
Do
oRng = oDoc.Bookmarks.Item("\endofdoc").Range
oRng.ParagraphFormat.SpaceAfter = 6
oRng.InsertAfter("A line of text")
oRng.InsertParagraphAfter()
Loop While Pos >= oRng.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)
oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
oRng.InsertBreak(Word.WdBreakType.wdPageBreak)
oRng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
oRng.InsertAfter("We're now on page 2. Here's my chart:")
oRng.InsertParagraphAfter()

'Insert a chart and change the chart.
oShape = oDoc.Bookmarks.Item("\endofdoc").Range.InlineShapes.AddOLEObject( _
ClassType:="MSGraph.Chart.8", FileName _
:="", LinkToFile:=False, DisplayAsIcon:=False)
oChart = oShape.OLEFormat.Object
oChart.charttype = 4 'xlLine = 4
oChart.Application.Update()
oChart.Application.Quit()
'If desired, you can proceed from here using the Microsoft Graph
'Object model on the oChart object to make additional changes to the
'chart.
oShape.Width = oWord.InchesToPoints(6.25)
oShape.Height = oWord.InchesToPoints(3.57)

'Add text after the chart.
oRng = oDoc.Bookmarks.Item("\endofdoc").Range
oRng.InsertParagraphAfter()
oRng.InsertAfter("THE END.")

'All done. Close this form.
Me.Close()

End Sub


Saludos.

5 comentarios:

dunkel kruspe dijo...

por q la imagen cuando la mando a llamar no me aparece

Anónimo dijo...

dunkel kruspe.. pudiste crear un word con imagen... si es asi puedes ayudarme...ç

patricio_ureta@hotmail.com

dunkel kruspe dijo...

si, si pude generar lo deja reviso el codigo y te lo mando dia de mañana por q no lo traigo horita

Anónimo dijo...

Gracias.... estare atento ami correo te agradesco de antemanto...
slds.

Patricio
patricio_ureta@hotmail.com

Chris-Oz dijo...

Hola he intentado adjuntar una imagen al documento que creo, pero no he podido. Serian tan amables de ilustrarme como es la sintaxis correcta para hacer esto.
Gracias.