Recent

Author Topic: [SOLVED] Searching for a bookmark in MS Word  (Read 7429 times)

madref

  • Hero Member
  • *****
  • Posts: 953
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
[SOLVED] Searching for a bookmark in MS Word
« on: September 27, 2016, 07:39:04 am »
When you are working on a windows machine you have MS Access which can 'talk' to MS Word.
And then you can also use Visual Basic for Applications and then you can do something like this
Code: [Select]
                cSQL = "SELECT * FROM qry_Overzicht_Leden WHERE LID_ID = " & ID
                Set rst = CurrentDb.OpenRecordset(cSQL)
                If rst.EOF = False Then
                    ' Document laden.
                    .Documents.Add (cSjabDir & SjabAlgemeen)
                    ' Vul de gegevens in.
                    .Selection.GoTo What:=wdGoToBookmark, Name:="bkmAan"
                    .Selection.TypeText Text:=rst!Lid_Voornaam & " " & rst!Lid_Tussenvoegsel & _
                            IIf(IsNull(rst!Lid_Tussenvoegsel) Or rst!Lid_Tussenvoegsel = "", "", " ") & _
                            IIf(IsNull(rst!Lid_Achternaam), "", rst!Lid_Achternaam)
                    .Selection.GoTo What:=wdGoToBookmark, Name:="bkmAdres"
                    .Selection.TypeText Text:=IIf(IsNull(rst!Lid_Adres), "", rst!Lid_Adres)
                    .Selection.GoTo What:=wdGoToBookmark, Name:="bkmPostcode"
                    .Selection.TypeText Text:=IIf(IsNull(rst!Lid_Postcode), "", SchrijfPostcode(rst!Lid_Postcode) & "  ") & _
                                              IIf(IsNull(rst!Lid_Woonplaats), "", Format(rst!Lid_Woonplaats, ">"))
                    .Selection.GoTo What:=wdGoToBookmark, Name:="bkmVoornaam"
                    .Selection.TypeText Text:=IIf(IsNull(rst!Lid_Voornaam), "", rst!Lid_Voornaam)
                    ' Opties in bestand aanpassen.
                    .ActiveWindow.View.type = wdPageView
                    .ActiveWindow.View.ShowBookmarks = False
                    ' Laat Word gemaximaliseerd zien.
                    .visible = True
                    .Application.WindowState = wdWindowStateMaximize
So in short: i search for a predefined bookmark and then i replace that bookmark with something from the search-query.


Is this also possible in Lazarus on a Mac?
« Last Edit: September 28, 2016, 02:11:00 pm by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey


madref

  • Hero Member
  • *****
  • Posts: 953
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Searching for a bookmark in MS Word
« Reply #2 on: September 27, 2016, 05:16:22 pm »
Thanks Phil.

That first part was already clear to me.

But how do i make such a script
And how to run it from within lazarus
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Searching for a bookmark in MS Word
« Reply #3 on: September 28, 2016, 02:48:15 am »
But how do i make such a script
And how to run it from within lazarus

The answers to both questions are in the links I posted.

This might also be useful:

https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptX/AppleScriptX.html


madref

  • Hero Member
  • *****
  • Posts: 953
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Searching for a bookmark in MS Word
« Reply #4 on: September 28, 2016, 11:35:13 am »
well i am trying but i get an error of: Failed to execute .... Error 127 on the following code;
Code: Pascal  [Select][+][-]
  1. SysUtils.ExecuteProcess('osascript /Documenten/Lazarus/Ledenadministratie OSX/db_scriptmsw.applescript', '', [])
And this is the script file:
Code: [Select]
Tell Application "Microsoft Word"
    Do Visual Basic "   Selection.GoTo What:=wdGoToBookmark, Name:="bkmAan""
    Do Visual Basic "   Selection.TypeText Text:=Aschwin van Loon"
end Tell
Whats wrong?
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

rvk

  • Hero Member
  • *****
  • Posts: 6171
Re: Searching for a bookmark in MS Word
« Reply #5 on: September 28, 2016, 11:48:51 am »
Did you try:
Code: [Select]
tell application "Microsoft Word"
  activate
  select bookmark "bkmAan" of active document
end tell
Does that jump the cursor to the bookmark?

Or even:
Code: [Select]
tell application "Microsoft Word"
  set content of text object of bookmark "bkmAan" of active document to "Aschwin van Loon"
end tell
(without jumping to the bookmark)

madref

  • Hero Member
  • *****
  • Posts: 953
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Searching for a bookmark in MS Word
« Reply #6 on: September 28, 2016, 01:04:04 pm »
I did test this in the applescript manager/ide and that works
But when i try this:
Code: Pascal  [Select][+][-]
  1. SysUtils.ExecuteProcess('osascript /Documenten/Lazarus/Ledenadministratie OSX/db_scriptmsw.applescript', '', []);
i get an error:
Projec ... raised exception class 'EOSError' with message:
Failed to execute "osascript /Documenten/Lazarus/Ledenadministratie OSX/db_scriptmsw.applescript", error code: 127
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

rvk

  • Hero Member
  • *****
  • Posts: 6171
Re: Searching for a bookmark in MS Word
« Reply #7 on: September 28, 2016, 01:15:30 pm »
Did you try it with fpsystem like the link showed you ?
http://wiki.lazarus.freepascal.org/Multiplatform_Programming_Guide#Making_do_without_Windows_COM_Automation

And if that still doesn't work did you could try
Code: Pascal  [Select][+][-]
  1. fpsystem('/Documenten/Lazarus/Ledenadministratie OSX/db_scriptmsw.applescript');
But in that case you need to give execution rights to db_scriptmsw.applescript with chmod.

madref

  • Hero Member
  • *****
  • Posts: 953
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Searching for a bookmark in MS Word
« Reply #8 on: September 28, 2016, 01:31:46 pm »
well it doesn't give an error but it also does nothing ;(
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

rvk

  • Hero Member
  • *****
  • Posts: 6171
Re: Searching for a bookmark in MS Word
« Reply #9 on: September 28, 2016, 01:32:55 pm »
well it doesn't give an error but it also does nothing ;(
Which of the two methods did you try?
(I gave you two options)

What is the resultcode of fpsystem()?
Code: Pascal  [Select][+][-]
  1. gReturncode := fpsystem(.......
(with both methods osascript and .applescript directly with the correct chmod-rights)
« Last Edit: September 28, 2016, 01:34:50 pm by rvk »

madref

  • Hero Member
  • *****
  • Posts: 953
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Searching for a bookmark in MS Word
« Reply #10 on: September 28, 2016, 01:42:32 pm »
Code: Pascal  [Select][+][-]
  1.    i := fpsystem ('osascript /Documenten/Lazarus/Ledenadministratie OSX/db_scriptmsw.applescript');
  2.  
Returns 256

Code: Pascal  [Select][+][-]
  1.    i := fpsystem ('/Documenten/Lazarus/Ledenadministratie OSX/db_scriptmsw.applescript');
  2.  
Returns 32512
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

rvk

  • Hero Member
  • *****
  • Posts: 6171
Re: Searching for a bookmark in MS Word
« Reply #11 on: September 28, 2016, 01:44:44 pm »
Code: Pascal  [Select][+][-]
  1.    i := fpsystem ('/Documenten/Lazarus/Ledenadministratie OSX/db_scriptmsw.applescript');
  2.  
Returns 32512
And what rights did you give the db_scriptmsw.applescript file ??

madref

  • Hero Member
  • *****
  • Posts: 953
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Searching for a bookmark in MS Word
« Reply #12 on: September 28, 2016, 01:48:18 pm »
How do i do that????

You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

madref

  • Hero Member
  • *****
  • Posts: 953
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Searching for a bookmark in MS Word
« Reply #13 on: September 28, 2016, 01:55:11 pm »
With this in a terminal window
Code: [Select]
chmod 777 "/Documenten/Lazarus/Ledenadministratie OSX/db_scriptmsw.applescript"I still get the same numbers :(
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

rvk

  • Hero Member
  • *****
  • Posts: 6171
Re: Searching for a bookmark in MS Word
« Reply #14 on: September 28, 2016, 02:00:57 pm »
And what do you get when you execute the same commands in the terminal window???

(that way you know which is at fault...)

So:
Code: [Select]
/Documenten/Lazarus/Ledenadministratie OSX/db_scriptmsw.applescriptand
Code: [Select]
osascript /Documenten/Lazarus/Ledenadministratie OSX/db_scriptmsw.applescript

 

TinyPortal © 2005-2018