Board index » delphi » Automating Word: Go to specific page number!

Automating Word: Go to specific page number!

Hello everybody,

these are my{*word*128}steps into the magic world of Word automation... I have a
very simple task, but I am having some trouble (surprise surprise!).

I want to open a document in Word and go to a specific page number; I've
managed to get Word running and loading the document, but I just can't
figure out how to go to a specific page number.

I am trying to use the "ActiveDocument.GoTo_" function as illustrated here:

var
    What, Which : OleVariant;
    WhereShouldIGo : OleVariant;
begin
    What := wdGoToPage;
    Which := wdGoToFirst;

    WhereShouldIGo := 2; // Go to page 2 of the document

    WordApplication.ActiveDocument.GoTo_(What, Which, EmptyParam,
EmptyParam);

I know that this example code wont work since I havent specified the page
number (WhereShouldIGo) anywhere in the call to GoTo_... I have tried both
of the last parameters but without any luck. So, could anyone please tell me
what I am doing wrong, or at least what I should do to make it work.

Regards,
Thomas

 

Re:Automating Word: Go to specific page number!


Quote
>     WordApplication.ActiveDocument.GoTo_(What, Which, EmptyParam,
> EmptyParam);

Use:
  WordApplication.Selection.GoTo_(What, Which, Where, WhereName);

"Where" in this case is the page Number and WhereName := ''

HTH,

--
Deepak Shenoy (TeamB)
Agni Software
http://www.agnisoft.com

Re:Automating Word: Go to specific page number!


Hello again,

thanks for your information, but I still cant get it to work so far...
here's the code I'm using:

var
  FileName, What, Which, Where, WhereName : OleVariant;
begin
  // Open word (or connect if already open)
  WordApplication.Connect;
  WordApplication.Visible := True;

  // Set filename for document to open
  FileName := 'c:\WordDoc.doc';

  // Open document in word
  WordApplication.Documents.Open(FileName, EmptyParam, EmptyParam,
    EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
    EmptyParam, EmptyParam);

  // Set parameters for call to GoTo_
  What := wdGoToPage;
  Which := wdGoToFirst;
  Where := 3;
  WhereName := '';

  WordApplication.ActiveDocument.GoTo_(What, Which, Where, WhereName);

The document is opened in word, but nothing happens when the call to GoTo_
gets executed. What am I doing wrong?

Regards,
Thomas

Re:Automating Word: Go to specific page number!


<<Thomas Rose:
  Which := wdGoToFirst;
  Where := 3;

Quote

If you want to go to the third page, not the first, that
should be

  Which := wdGoToAbsolute;
  Where := 3;

--
Deborah Pate (TeamB) http://delphi-jedi.org

  TeamB don't see posts sent via Google or ISPs
  Use the real Borland server: newsgroups.borland.com
  http://www.borland.com/newsgroups/genl_faqs.html

Re:Automating Word: Go to specific page number!


Hmm, it still doesn't work... It just opens the word document, but doesn't
go to page 3... perhaps it's another problem. I am trying to get this to
work with Word97 on a XP machine - and I've also tested it on a Win95 box
but still without luck.

And if you wonder why I am using Word97 on XP I really dont know. I have a
new job where I just got a box with this configuration. Go figure.

Well, any ideas as to what might be wrong?

Regards,
Thomas

"Deborah Pate (TeamB)" <d.p...@blueyonder.co.not-this-bit.uk> skrev i en
meddelelse news:VA.00001b1d.034af631@blueyonder.co.not-this-bit.uk...

Quote
> <<Thomas Rose:
>   Which := wdGoToFirst;
>   Where := 3;

> If you want to go to the third page, not the first, that
> should be

>   Which := wdGoToAbsolute;
>   Where := 3;

> --
> Deborah Pate (TeamB) http://delphi-jedi.org

>   TeamB don't see posts sent via Google or ISPs
>   Use the real Borland server: newsgroups.borland.com
>   http://www.borland.com/newsgroups/genl_faqs.html

Re:Automating Word: Go to specific page number!


<<Thomas Rose:
Well, any ideas as to what might be wrong?

Quote

Well, it works for me. However I usually regard the use of
Goto methods as a warning sign in Word automation - it's
much better to avoid having anything to do with the
Selection object if you can. So why, exactly, do you want
to move the selection to page 3? What do you want to happen
when you get there?

--
Deborah Pate (TeamB) http://delphi-jedi.org

  TeamB don't see posts sent via Google or ISPs
  Use the real Borland server: newsgroups.borland.com
  http://www.borland.com/newsgroups/genl_faqs.html

Other Threads