Trying to use the GoTo method of the Word.Application ActiveX object in JavaScript

Discussion in 'Javascript' started by MayBoy, Dec 11, 2006.

  1. MayBoy

    MayBoy Guest

    Hi There

    I am trying to use the Goto method of the Word ActiveX object. I am
    trying to open a document and go to a named bookmark. If I use this
    code in VB it works, so I'm sure the approach is possible, I just can't
    get JavaScript to work with it.

    Here is the code I am using, the error I get from IE is Object
    Expected:
    Hope someone can help! Any help would be much appreciated

    function PageLoad()
    {
    var WordApp = new ActiveXObject('Word.Application');
    WordApp.Visible = true;
    var documentlocation = crmForm.all.new_documentlocation.DataValue;
    var wd = WordApp.Documents.Open(documentlocation);
    wd.Select();
    var Name = 'TestFred';
    var wr = wd.Selection.GoTo(What : Word.WdGoToItem.wdGoToBookmark,Name :
    'TestFred');
    }
     
    MayBoy, Dec 11, 2006
    #1
    1. Advertising

  2. MayBoy wrote:

    > Hi There


    Hi

    > I am trying to use the Goto method of the Word ActiveX object. I am
    > trying to open a document and go to a named bookmark. If I use this
    > code in VB it works, so I'm sure the approach is possible, I just can't
    > get JavaScript to work with it.
    >
    > Here is the code I am using, the error I get from IE is Object
    > Expected:


    It would help if you could identify which line, and which part of that
    line, in your code is triggering this error.

    > Hope someone can help! Any help would be much appreciated
    >
    > function PageLoad()
    > {
    > var WordApp = new ActiveXObject('Word.Application');
    > WordApp.Visible = true;
    > var documentlocation = crmForm.all.new_documentlocation.DataValue;
    > var wd = WordApp.Documents.Open(documentlocation);


    I am assuming for the sake of argument that you get this far.

    > wd.Select();
    > var Name = 'TestFred';


    Why have you defined this variable but not used it?

    > var wr = wd.Selection.GoTo(What : Word.WdGoToItem.wdGoToBookmark,Name :
    > 'TestFred');


    This is the line which has a number of possible errors in it.

    Error 1
    ============

    "wd" contains a reference to a Document object. The Selection object
    is a member of the Application object, not a Document object. So this
    should be "WordApp.Selection".

    Alternatively, you can call Document.GoTo().

    Document.GoTo() : "Returns a Range object that represents the start
    position of the specified item, such as a page, bookmark, or field"
    Application.Selection.GoTo(): "Moves the insertion point to the
    character position immediately preceding the specified item, and
    returns a Range object (except for the wdGoToGrammaticalError,
    wdGoToProofreadingError, or wdGoToSpellingError constant)."

    Errors 2 & 3
    ============

    Firstly, JavaScript does not support NAMED arguments.

    So this "GoTo(What : " is a syntax error.

    Secondly, you must as a result supply the arguments in the order that
    Word expects.

    "expression.GoTo(What, Which, Count, Name)"

    So you will need to supply default values for Which and Count.


    Errors 4 & 5
    ============

    "Word.WdGoToItem.wdGoToBookmark"

    There are two errors here:

    Firstly, you cannot access the constant wdGoToBookmark as a property of
    WdGoToItem.

    Secondly, apart from on the Server, you cannot access in JavaScript
    Word constants, such as "wdGoToBookmark" by name.

    You need to supply the constant value instead. The following is taken
    from Word's ObjectBrowser facility in its Macro editor:-

    "Const wdGoToBookmark = -1 (&HFFFFFFFF)"
    "Const Const wdGoToFirst = 1"

    Result
    ============

    Try:-

    var myRange = Document.GoTo(-1, 1, 1, 'TestFred');
    or
    WordApp.Selection.GoTo(-1, 1, 1, 'TestFred');

    I have not tested these, so there may be something else to consider as
    well. JavaScript does work, with a bit of fiddling.

    Regards

    Julian
     
    Julian Turner, Dec 12, 2006
    #2
    1. Advertising

  3. MayBoy

    MayBoy Guest

    Hi Julian

    I tried your answer but still get an error, when you put any value in
    for Which it says "You entered multiple destinations for a
    page.line.footnoteendnote or comment"

    I tried the same in VB and it works with a nothing as the Which and
    Count parameters but I can't do that in javascript.

    Any ideas?


    Julian Turner wrote:
    > MayBoy wrote:
    >
    > > Hi There

    >
    > Hi
    >
    > > I am trying to use the Goto method of the Word ActiveX object. I am
    > > trying to open a document and go to a named bookmark. If I use this
    > > code in VB it works, so I'm sure the approach is possible, I just can't
    > > get JavaScript to work with it.
    > >
    > > Here is the code I am using, the error I get from IE is Object
    > > Expected:

    >
    > It would help if you could identify which line, and which part of that
    > line, in your code is triggering this error.
    >
    > > Hope someone can help! Any help would be much appreciated
    > >
    > > function PageLoad()
    > > {
    > > var WordApp = new ActiveXObject('Word.Application');
    > > WordApp.Visible = true;
    > > var documentlocation = crmForm.all.new_documentlocation.DataValue;
    > > var wd = WordApp.Documents.Open(documentlocation);

    >
    > I am assuming for the sake of argument that you get this far.
    >
    > > wd.Select();
    > > var Name = 'TestFred';

    >
    > Why have you defined this variable but not used it?
    >
    > > var wr = wd.Selection.GoTo(What : Word.WdGoToItem.wdGoToBookmark,Name :
    > > 'TestFred');

    >
    > This is the line which has a number of possible errors in it.
    >
    > Error 1
    > ============
    >
    > "wd" contains a reference to a Document object. The Selection object
    > is a member of the Application object, not a Document object. So this
    > should be "WordApp.Selection".
    >
    > Alternatively, you can call Document.GoTo().
    >
    > Document.GoTo() : "Returns a Range object that represents the start
    > position of the specified item, such as a page, bookmark, or field"
    > Application.Selection.GoTo(): "Moves the insertion point to the
    > character position immediately preceding the specified item, and
    > returns a Range object (except for the wdGoToGrammaticalError,
    > wdGoToProofreadingError, or wdGoToSpellingError constant)."
    >
    > Errors 2 & 3
    > ============
    >
    > Firstly, JavaScript does not support NAMED arguments.
    >
    > So this "GoTo(What : " is a syntax error.
    >
    > Secondly, you must as a result supply the arguments in the order that
    > Word expects.
    >
    > "expression.GoTo(What, Which, Count, Name)"
    >
    > So you will need to supply default values for Which and Count.
    >
    >
    > Errors 4 & 5
    > ============
    >
    > "Word.WdGoToItem.wdGoToBookmark"
    >
    > There are two errors here:
    >
    > Firstly, you cannot access the constant wdGoToBookmark as a property of
    > WdGoToItem.
    >
    > Secondly, apart from on the Server, you cannot access in JavaScript
    > Word constants, such as "wdGoToBookmark" by name.
    >
    > You need to supply the constant value instead. The following is taken
    > from Word's ObjectBrowser facility in its Macro editor:-
    >
    > "Const wdGoToBookmark = -1 (&HFFFFFFFF)"
    > "Const Const wdGoToFirst = 1"
    >
    > Result
    > ============
    >
    > Try:-
    >
    > var myRange = Document.GoTo(-1, 1, 1, 'TestFred');
    > or
    > WordApp.Selection.GoTo(-1, 1, 1, 'TestFred');
    >
    > I have not tested these, so there may be something else to consider as
    > well. JavaScript does work, with a bit of fiddling.
    >
    > Regards
    >
    > Julian
     
    MayBoy, Dec 13, 2006
    #3
  4. MayBoy

    MayBoy Guest

    Hi there

    I just found out how, just create an empty object and use that.

    ie var empty;
    var wr =WordApp.Selection.GoTo(-1,empty,0,'TestFred);

    Thanks!!
    MayBoy wrote:
    > Hi Julian
    >
    > I tried your answer but still get an error, when you put any value in
    > for Which it says "You entered multiple destinations for a
    > page.line.footnoteendnote or comment"
    >
    > I tried the same in VB and it works with a nothing as the Which and
    > Count parameters but I can't do that in javascript.
    >
    > Any ideas?
    >
    >
    > Julian Turner wrote:
    > > MayBoy wrote:
    > >
    > > > Hi There

    > >
    > > Hi
    > >
    > > > I am trying to use the Goto method of the Word ActiveX object. I am
    > > > trying to open a document and go to a named bookmark. If I use this
    > > > code in VB it works, so I'm sure the approach is possible, I just can't
    > > > get JavaScript to work with it.
    > > >
    > > > Here is the code I am using, the error I get from IE is Object
    > > > Expected:

    > >
    > > It would help if you could identify which line, and which part of that
    > > line, in your code is triggering this error.
    > >
    > > > Hope someone can help! Any help would be much appreciated
    > > >
    > > > function PageLoad()
    > > > {
    > > > var WordApp = new ActiveXObject('Word.Application');
    > > > WordApp.Visible = true;
    > > > var documentlocation = crmForm.all.new_documentlocation.DataValue;
    > > > var wd = WordApp.Documents.Open(documentlocation);

    > >
    > > I am assuming for the sake of argument that you get this far.
    > >
    > > > wd.Select();
    > > > var Name = 'TestFred';

    > >
    > > Why have you defined this variable but not used it?
    > >
    > > > var wr = wd.Selection.GoTo(What : Word.WdGoToItem.wdGoToBookmark,Name :
    > > > 'TestFred');

    > >
    > > This is the line which has a number of possible errors in it.
    > >
    > > Error 1
    > > ============
    > >
    > > "wd" contains a reference to a Document object. The Selection object
    > > is a member of the Application object, not a Document object. So this
    > > should be "WordApp.Selection".
    > >
    > > Alternatively, you can call Document.GoTo().
    > >
    > > Document.GoTo() : "Returns a Range object that represents the start
    > > position of the specified item, such as a page, bookmark, or field"
    > > Application.Selection.GoTo(): "Moves the insertion point to the
    > > character position immediately preceding the specified item, and
    > > returns a Range object (except for the wdGoToGrammaticalError,
    > > wdGoToProofreadingError, or wdGoToSpellingError constant)."
    > >
    > > Errors 2 & 3
    > > ============
    > >
    > > Firstly, JavaScript does not support NAMED arguments.
    > >
    > > So this "GoTo(What : " is a syntax error.
    > >
    > > Secondly, you must as a result supply the arguments in the order that
    > > Word expects.
    > >
    > > "expression.GoTo(What, Which, Count, Name)"
    > >
    > > So you will need to supply default values for Which and Count.
    > >
    > >
    > > Errors 4 & 5
    > > ============
    > >
    > > "Word.WdGoToItem.wdGoToBookmark"
    > >
    > > There are two errors here:
    > >
    > > Firstly, you cannot access the constant wdGoToBookmark as a property of
    > > WdGoToItem.
    > >
    > > Secondly, apart from on the Server, you cannot access in JavaScript
    > > Word constants, such as "wdGoToBookmark" by name.
    > >
    > > You need to supply the constant value instead. The following is taken
    > > from Word's ObjectBrowser facility in its Macro editor:-
    > >
    > > "Const wdGoToBookmark = -1 (&HFFFFFFFF)"
    > > "Const Const wdGoToFirst = 1"
    > >
    > > Result
    > > ============
    > >
    > > Try:-
    > >
    > > var myRange = Document.GoTo(-1, 1, 1, 'TestFred');
    > > or
    > > WordApp.Selection.GoTo(-1, 1, 1, 'TestFred');
    > >
    > > I have not tested these, so there may be something else to consider as
    > > well. JavaScript does work, with a bit of fiddling.
    > >
    > > Regards
    > >
    > > Julian
     
    MayBoy, Dec 13, 2006
    #4
  5. MayBoy wrote:

    > Hi there
    >
    > I just found out how, just create an empty object and use that.
    >
    > ie var empty;
    > var wr =WordApp.Selection.GoTo(-1,empty,0,'TestFred);
    >
    > Thanks!!


    Hi

    Good answer. Effectively you are supplying "undefined" as an argument,
    which presumably word knows therefore to ignore. Sometimes null also
    works, or an empty string.

    Best Wishes

    Jules
     
    Julian Turner, Dec 13, 2006
    #5
  6. MayBoy

    MayBoy Guest

    Hi Julian

    I tried null and an empty sting but this was the ol=nly solution that
    worked.

    Hope you have a Merry Xmas!

    and Thanks again.

    Andy
    Julian Turner wrote:
    > MayBoy wrote:
    >
    > > Hi there
    > >
    > > I just found out how, just create an empty object and use that.
    > >
    > > ie var empty;
    > > var wr =WordApp.Selection.GoTo(-1,empty,0,'TestFred);
    > >
    > > Thanks!!

    >
    > Hi
    >
    > Good answer. Effectively you are supplying "undefined" as an argument,
    > which presumably word knows therefore to ignore. Sometimes null also
    > works, or an empty string.
    >
    > Best Wishes
    >
    > Jules
     
    MayBoy, Dec 13, 2006
    #6
  7. MayBoy wrote:

    > Hi Julian
    >
    > I tried null and an empty sting but this was the ol=nly solution that
    > worked.
    >
    > Hope you have a Merry Xmas!
    >
    > and Thanks again.
    >
    > Andy


    Hi

    Merry Xmas to you too. Your undefined variable is a solution I will
    make use of myself.

    Regards

    Julian
     
    Julian Turner, Dec 14, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    924
  2. vml
    Replies:
    0
    Views:
    1,088
  3. dare ruby

    how to use "goto" method inruby?

    dare ruby, Dec 19, 2007, in forum: Ruby
    Replies:
    6
    Views:
    126
  4. Pandit
    Replies:
    1
    Views:
    291
    Dag Sunde
    Jun 15, 2006
  5. Sylum
    Replies:
    1
    Views:
    117
    Randy Webb
    Jun 22, 2006
Loading...

Share This Page