[Solved] How to Open a PDF from a form

Discuss the database features
Post Reply
Marshal858
Posts: 6
Joined: Tue Apr 08, 2008 5:06 pm

[Solved] How to Open a PDF from a form

Post by Marshal858 »

Hi, I'm using Open Office 2.4 on a PC with Vista

I found a topic on opening email links from a form using a macro and was wondering if there is any way to open other files from my PC such as a button to open a pdf in the Pc's default pdf viewer.

If that is possible than is there anyway to make the button open a file based on the record selected from a grid in the form? This would really be useful for my current database project since it would allow me to have users open a file from a button rather than go look for it on the harddrive (currently a textbox tells you where to go to get the file but you have to look it up manually).

Any help on this would be appreciated, and if it can't be done letting me know that would also be appreciated :)
Last edited by Marshal858 on Tue Jun 03, 2008 2:19 pm, edited 1 time in total.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: How to Open a PDF from a form

Post by Villeroy »

Marshal858 refers to http://user.services.openoffice.org/en/ ... 92&p=14885 and http://user.services.openoffice.org/en/ ... 57&p=21667
I bundled 3 versions in a Python module.
The mailto-version adds protocol-prefix "mailto:" to the field's value.
The file-version converts a system path to URL with protocol-prefix "file:".
The url-version uses any URL which already includes the protocol in the field value.
The URL is assigned to a push-button. The push-button must be named "File_Button", "URL_Button" or "Mailto_Button" respectively. The tag (property "Additional Information") must specify the field name from where to get the URL. Don't mix up the field name with the name of a control. The field name is the name of one of the form's source columns.
Extract the attached zip-archive to <ooo_user_dir>/user/Scripts/python/ or save the following code without correct indendation and UNIX line feeds as <ooo_user_dir>/user/Scripts/python/DBForms.py
Assign the form's event "After Record Change" to one of the routines. Add a wrapper routine if you have to use more than one button in this manner.

Code: Select all

import uno

def mail_on_record_change(oEv):
    frm = oEv.Source
    cols = frm.getColumns()
    btn = frm.getByName("Mailto_Button")
    tag = btn.Tag
    field = cols.getByName(tag)
    btn.TargetURL = 'mailto:'+ field.getString()

def url_on_record_change(oEv):
    frm = oEv.Source
    cols = frm.getColumns()
    btn = frm.getByName("URL_Button")
    tag = btn.Tag
    field = cols.getByName(tag)
    btn.TargetURL = field.getString()

def file_on_record_change(oEv):
    frm = oEv.Source
    cols = frm.getColumns()
    btn = frm.getByName("File_Button")
    tag = btn.Tag
    field = cols.getByName(tag)
    btn.TargetURL = uno.systemPathToFileUrl(field.getString())

g_exportedScripts = mail_on_record_change, url_on_record_change, file_on_record_change
 Edit: 2008-05-21: The installation procedure is unclear. Thanks, mobio 
Redirection from another thread: http://user.services.openoffice.org/en/ ... =13&t=5937
mobio wrote:Problem is that I try to copy in one of the folders with python scripts (where I have Hello World, ...since I can't find the script specified in hyperlinked post <oo folder>/User/Script/phyton)
The "Hello World" stuff that comes with OOo is saved in the program's installation path, such as:
C:\Program Files\OOo.2.4\share\Script\python\
[sorry, no Windows at hand, but that's roughly the path]
On a decent operating system you need administration access in order to copy anything into this folder.
If you copied the macro to the system wide location, you find the macro below
"OpenOffice.org Macros">DBForms... where "DBForms" is derived from the file name "DBFroms.py"
In case you do not have any Python macros in Menu:Tools>Macros>Organize>Python... then you installed the office without support for Python macros.

Each office user has a profile folder for all his/her individual customizations, preferences, templates and macros. This is the path for Python macros:
C:\Documents And Settings\YourLogInName\OOo.2.4\user\Script\python\
(Add the "python" folder in "Script" when you copy your first Python-macro to this place)
If you copied to your user profile, you find the macro below
"My Macros">DBForms... where "DBForms" is derived from the file name "DBFroms.py"

I recommend to install the DBFroms.py contained the attached zip archive rather than pasting the above code into a text editor. It has the correct indentation and line-feeds.
If you want to edit Python macros, then you should use a decent text editor with two facts in mind:
- Leading white space (indentation) has a meaning in Python.
- OOo requires UNIX line feeds to handle Python macros.
- When you copy the above code by using the "Select All" hyperlink, all the code gets an extra indentation of 4 spaces. Select the code manually, paste into your editor and tell it to use Unix line feeds.
 Edit: 'nother edit because not every installation includes Python and Ubuntu messed up Python support for their version of OOo2.4 ... Anyway, this is the Basic version. 
Menu:Tools>Macros>Organize>OOo Basic... [Organizer...] some new module in some new library... replace module's default content with the following:

Code: Select all

REM  *****  BASIC  *****
sub mail_on_record_change(oEv)
    frm = oEv.Source
    cols = frm.getColumns()
    btn = frm.getByName("Mailto_Button")
    tag = btn.Tag
    field = cols.getByName(tag)
    btn.TargetURL = "mailto:"& field.getString()
end sub

sub url_on_record_change(oEv)
    frm = oEv.Source
    cols = frm.getColumns()
    btn = frm.getByName("URL_Button")
    tag = btn.Tag
    field = cols.getByName(tag)
    btn.TargetURL = field.getString()
end sub

sub file_on_record_change(oEv)
    frm = oEv.Source
    cols = frm.getColumns()
    btn = frm.getByName("File_Button")
    tag = btn.Tag
    field = cols.getByName(tag)
    btn.TargetURL = ConvertToURL(field.getString())
end sub
Attachments
DBForms.py.zip
RecordChange events to adjust URL-buttons.
(388 Bytes) Downloaded 662 times
Last edited by Villeroy on Tue Jun 03, 2008 2:55 pm, edited 6 times in total.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Marshal858
Posts: 6
Joined: Tue Apr 08, 2008 5:06 pm

Re: How to Open a PDF from a form

Post by Marshal858 »

Thanks a lot for the code I looked at it quickly and it makes sense. I don't have time to try it out just now but will try it as soon as I can. :) and now I know that it is possible so I feel much better :D
Marshal858
Posts: 6
Joined: Tue Apr 08, 2008 5:06 pm

Re: [Solved] How to Open a PDF from a form

Post by Marshal858 »

I've just got this working, thanks again Villeroy especially for your edits which clarified the installation to the point that even I could understand it. :D
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] How to Open a PDF from a form

Post by Villeroy »

Marshal858 wrote:I've just got this working, thanks again Villeroy especially for your edits which clarified the installation to the point that even I could understand it. :D
Thank you for the positive feedback. It's important to know that someone got it working actually.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
bandoro
Posts: 22
Joined: Tue Oct 18, 2011 8:24 am

Re: [Solved] How to Open a PDF from a form

Post by bandoro »

hai all
i'm newbie in openoffice base, i was a MS Access user before this.
so far i can already made database with embedded images like show at http://sheepdogguides.com/fdb/fdb1imagbp.htm.
now i want to know how to open the image with a button
so far i try this code

Code: Select all

sub openurl(oEv as object)
'
   dim oForm,oTextBox 
   oForm=oEv.source.model.parent
   oTextBox=oForm.getbyname("textpic") 'texpic is a text box that show field form table called imagepath to store image url link 
   if FileExists ( oTextBox.text) then
      oEv.source.model.targeturl=oTextBox.text
      openurl=true ' Do the programm when the file exists
      else
      openurl=false ' The file does not exist and do nothing
   end if
end sub
and nothing happen (no error and the picture is not opening)
then i try this code

Code: Select all

sub file_on_record_change(oEv)
    frm = oEv.Source
    cols = frm.getColumns()
    btn = frm.getByName("textpic")
    tag = btn.Tag
    field = cols.getByName(tag)
    btn.TargetURL = ConvertToURL(field.getString())
end sub
and i got error code
BASIC runtime error
property or method not found: getColumns
at

Code: Select all

cols = frm.getColumns()
What should i do ??
i had dwelling this problem since a week ago and i can't made any progress before this problem resolve.

Please help me. Don't let me back into MS access

Thanks
The greatest gift you can give is share your knowledge to others
Windows 7 HOME 64 BIT, Openoffice 3.3
bandoro
Posts: 22
Joined: Tue Oct 18, 2011 8:24 am

Re: [Solved] How to Open a PDF from a form

Post by bandoro »

I found the problem. Thanks guys..
i use this macro code and just add as variant at 2nd line and it work..

Code: Select all

sub openurl(oEv as object)
   dim oForm,oTextBox 
   oForm=oEv.source.model.parent
   oTextBox=oForm.getbyname("textpic") 'texpic is a text box that show field form table called imagepath to store image url link 
   if FileExists ( oTextBox.text) then
      oEv.source.model.targeturl=oTextBox.text
      openurl=true ' Do the programm when the file exists
      else
      openurl=false ' The file does not exist and do nothing
   end if
end sub
The greatest gift you can give is share your knowledge to others
Windows 7 HOME 64 BIT, Openoffice 3.3
Post Reply