WIN32OLE MS Word Help

Hi all,
I am trying to create word document in ruby using WIN32OLE.
as…

require ‘win32ole’
word = WIN32OLE.new(“Word.Application”)
word.visible = true
word.documents.add
selection = word.selection
word.Selection.TypeText “This is some text.”


.

Can we set header and footer to document? I want to generate a
document such that its header contains an image on right side and a
note on left side. please help me out.
Thanks.

This is not a Rails specific Question - wrong Board ;). It would also be
better to ask this question on a group that knows the OLE-Api, as it is
not documented in ruby.

Anyways:

As far as i was able to google it, header and footer are specific views
in a document that you can address with switch_view. In Perl, the method
is:

insert something into the header

switch_view($document, wdSeekCurrentPageHeader);
my $st_header = create_style($document, “Verdana”, 8, 0, 0);
set_style($document, $st_header);
text ($document, “this is the document’s header, it will be repeated
on all pages”);

insert something into the footer

switch_view($document, wdSeekCurrentPageFooter);
my $st_footer = create_style($document, “Verdana”, 8, 0, 0);
set_style($document, $st_footer);
text ($document, “this is the document’s footer, it will be repeated
on all pages”);

I’m sorry that i can’t find example code in ruby, but you should be able
to translate it. I do not own a windows box, so i test it for you. I
hope it helps.

Florian
Thank you for your response. In VB it can be done as

Dim pDoc as Word.Document
Set pDoc = WordApp.Documents.Open(FileName)
With pDoc
.Headers(wdHeaderFooterPrimary).Range.Text = “Header that I want
to set”

with .Footers(wdHeaderFooterPrimary).Range
    .Text = "Footer Left text" & vbTab & "Footer center"  & vbTab
    .Fields.Add Range:=ActiveDocument.Range(.End, .End),

Type:=wdFieldPage

end with
end with

There might be some APIs in ruby also to set header and footer.
Regards,

Florian G. wrote:

This is not a Rails specific Question - wrong Board ;). It would also be
better to ask this question on a group that knows the OLE-Api, as it is
not documented in ruby.

Anyways:

As far as i was able to Google it, header and footer are specific views
in a document that you can address with switch_view. In Perl, the method
is:

insert something into the header

switch_view($document, wdSeekCurrentPageHeader);
my $st_header = create_style($document, “Verdana”, 8, 0, 0);
set_style($document, $st_header);
text ($document, “this is the document’s header, it will be repeated
on all pages”);

insert something into the footer

switch_view($document, wdSeekCurrentPageFooter);
my $st_footer = create_style($document, “Verdana”, 8, 0, 0);
set_style($document, $st_footer);
text ($document, “this is the document’s footer, it will be repeated
on all pages”);

I’m sorry that i can’t find example code in ruby, but you should be able
to translate it. I do not own a windows box, so i test it for you. I
hope it helps.

Try the following code:

require ‘win32ole’
word = WIN32OLE.new(“Word.Application”)
word.visible = true
word.documents.add
word.Selection.TypeText “This is the main body”
word.ActiveWindow.ActivePane.View.SeekView = 9 #
wdSeekCurrentPageHeader
word.Selection.TypeText “This is the header”

To figure out the value of wdSeekCurrentPageHeader, you need to google
for it, print it out in VB or use some other method.
I don’t believe the statement “selection = word.selection” in your
original code is doing anything, I was able to run with it.
For some hints on converting VB to ruby, check out the blog at

he’s got some great hints that have helped me significantly.

– Dan

Playing with this a little more I came up with this:


require ‘win32ole’
word = WIN32OLE.new(“Word.Application”)
current_W = $VERBOSE
$VERBOSE = nil
WIN32OLE.const_load(word)
$VERBOSE = current_W
word.visible = true
word.documents.add
word.Selection.TypeText “This is the main body”
word.ActiveWindow.ActivePane.View.SeekView =
WIN32OLE::WdSeekCurrentPageHeader
word.Selection.TypeText “This is the header”

the line “WIN32OLE.const_load(word)” loads the VB constants for Word,
so you can use the constant “WIN32OLE::WdSeekCurrentPageHeader” later
in the code.
When you’re translating constants from VB, add “WIN32OLE::” to the
front, and capitalize the first char of the variable name.
The lines with “$VERBOSE” mask the warnings that come from the
const_load operation, take those lines out to see what I mean.

– Dan

Yes it works, :~}
Thank you Dan. That is great.
Regards,

-Rahul

Playing with this a little more I came up with this:


require ‘win32ole’
word = WIN32OLE.new(“Word.Application”)
current_W = $VERBOSE
$VERBOSE = nil
WIN32OLE.const_load(word)
$VERBOSE = current_W
word.visible = true
word.documents.add
word.Selection.TypeText “This is the main body”
word.ActiveWindow.ActivePane.View.SeekView =
WIN32OLE::WdSeekCurrentPageHeader
word.Selection.TypeText “This is the header”

the line “WIN32OLE.const_load(word)” loads the VB constants for Word,
so you can use the constant “WIN32OLE::WdSeekCurrentPageHeader” later
in the code.
When you’re translating constants from VB, add “WIN32OLE::” to the
front, and capitalize the first char of the variable name.
The lines with “$VERBOSE” mask the warnings that come from the
const_load operation, take those lines out to see what I mean.

– Dan