Adithyakiran.k has asked for the wisdom of the Perl Monks concerning the following question:

I have to convert a xml file to MS Word Document. I have predefined styles in a .dotx file which should be applied to the text extracted from the XML. Like there are specific styles to "Title", "Paragraphs", "Lists", etc.,. I am using win32::ole to create MS Word Document. I am not able to get the footnote references at the places I want them to be. Please suggest how can I get the footnotes at right places in the Word document. Please see the below code.

use warnings; use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft.Word'; # wd constants use Win32::OLE::Const 'Microsoft Office'; # mso constants my $cur_style = 'a'; my $cur_bookmark = 'a'; sub bold { my $document = shift; my $bold = shift; $document->ActiveWindow->Selection->{Font}->{Bold} = $bold ? -1 : 0; } sub save_doc_as { my $document = shift; my $filename = shift; $document->SaveAs($filename); } sub create_style { my $document = shift; my $fontname = shift; my $font_size = shift; my $bold = shift; my $italic = shift; my $style = $document->Styles->Add($cur_style); my $style_font = $style->{Font}; $style_font -> {Name } = $fontname; $style_font -> {Size } = $font_size; $style_font -> {Bold } = $bold; $style_font -> {Italic} = $italic; my %style; $style{name} = $cur_style++; return \%style; } sub set_style { my $document = shift; my $style_arg = shift; $document->ActiveWindow->Selection -> {Style} = $style_arg -> {name} +; } sub text { my $document = shift; my $text = shift; $document->ActiveWindow->Selection -> TypeText($text); } # aka new line, newline or NL sub enter { my $document = shift; $document->ActiveWindow->Selection -> TypeParagraph; } my $word = CreateObject Win32::OLE 'Word.Application' or die $!; $word->{'Visible'} = 1; my $document = $word->Documents->Add('new.dotx',0,0,-1); # selection is the insertion point. my $selection = $word->Selection; $selection->{Style}="Title" ; text($document, "135: This is the Document Title"); text($document,"\n"); $selection->{Style}="Normal" ; bold($document, 1); text($document, "Authors:"); bold($document,0); enter($document); text($document, "some text ............"); #---------------------------------------- #Insert Footnote Here $selection->Footnotes->Add($selection->{Range},"1", "This is the First + Footnote"); #---------------------------------------- text($document, "some other text ........."); #---------------------------------------- #Insert Footnote Here $selection->Footnotes->Add($selection->{Range},"2", "This is the secon +d Footnote"); #---------------------------------------- enter($document); bold($document, 1); text($document, "Introduction"); bold($document,0); enter($document); text($document, "This will have the introduction .... "); $selection->{Style}=" :000" ; text($document, "Quick Look"); $selection->{Style}="Normal" ; enter($document); text($document, "This is the Normal Paragraph Data"); save_doc_as($document, 'output.docx');

Replies are listed 'Best First'.
Re: How to add a footnote in MS Word using win32::ole
by hdb (Monsignor) on Aug 28, 2013 at 11:13 UTC

    I understand that your problem is that the footnote symbol (like superscript 1) is in the wrong place. I do not really know what the problem is, but when I manually type word documents I usually first type a space, then go one char left, add a footnote, and then continue typing after the space. This workaround seems to be ok for Perl as well. The following works for me:

    #---------------------------------------- #Insert Footnote Here $selection->Move(-1); $selection->Footnotes->Add( $selection->{Range},"1", "This is the Firs +t Footnote"); $selection->Move(1);

    Again, I do not really know why but you could encapsulate it into another subroutine.

      Thank you. This worked.

      Hi, Is there anyway to format the footnote text that is added to the Word doc?

      #---------------------------------------- #Insert Footnote Here $selection->Move(-1); $selection->Footnotes->Add( $selection->{Range},"1", "This is the <i>F +irst Footnote</i>"); $selection->Move(1);

      The text within the "i" tags should be in Italics Format in Footnote in word document.

Re: How to add a footnote in MS Word using win32::ole
by Anonymous Monk on Aug 28, 2013 at 00:22 UTC
    What happens (whats wrong)?

      All the footnotes are coming at the end of the document.

        All the footnotes are coming at the end of the document.

        What do you mean by that?

        Cause that kind of makes sense, they're called footnotes :) and you usually read them at the end of a page or document

        It looks like you're using the documented api correctly, so I'm out of ideas :)