Options

Print files with an associated application

detlefdetlef Member Posts: 38
Hi
I am looking at writing a function that takes a filename as parameter. This function would then print the file as setup in the registry for that particular file type. (These types are setup under Folder options, File Types)
Is there an easy way of doing this?
Any help appreciated.


Cheers
Detlef

Comments

  • Options
    kinekine Member Posts: 12,562
    I think that it will be not so easy... because many application are not able to automatically print document... (because you have no way how to say them - print this file...)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    rsfairbanksrsfairbanks Member Posts: 107
    Taking one file type, how would you print a pdf file, for example one stored in a BLOB?
  • Options
    WaldoWaldo Member Posts: 3,412
    I'm struggling with the same problem.

    I have a blob field, which contains a file (extension can be a number of things). I would like to print this file to a pdf file and send through email.

    I think the solution is to make a print-procedure for every possible extension (.doc, .xls, .pdf, ...).

    any suggestions?

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • Options
    kinekine Member Posts: 12,562
    May be that you need something like http://www.softpedia.com/get/System/OS- ... ware.shtml


    I did not test it, I only found it on google... may be that there is some interface to this or some similar application...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    detlefdetlef Member Posts: 38
    Looks like the only way to do this is to read the registry, run the specified shell command and then optionally send dde messages (Office documents use dde). This must be, what Windows Explorer does, too (if you right-click and select print).
  • Options
    WaldoWaldo Member Posts: 3,412
    Just finished my app. What I did:

    - Export BLOB field to file name (extension is in attachment table).
    - validate extension:
    - if .doc: use word automation
    - if .xls: use excel automation
    - if .mpp: use project automation
    - ...
    - convert to pdf with amyuni
    - open mail message with pdf document

    may be not the best way to do it, but it works like a charm!

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • Options
    pdjpdj Member Posts: 643
    But you can drag almost any filetype to the printer window, and then it will print the file using the right appliction. There must be a way to do the same using some API...
    Regards
    Peter
  • Options
    WaldoWaldo Member Posts: 3,412
    Well, to be honest, I think it works internally different then what you think.

    You notice that when you right click on different files in explorer, sometimes you get the "print" functionality, sometimes you don't get it. So it depends on the filetype whether you can print the file or not.

    Eric Wauters
    MVP - Microsoft Dynamics NAV
    My blog
  • Options
    detlefdetlef Member Posts: 38
    Good news. I've found a way to do it!
    The following Function is using Automation Objects from 'Microsoft Shell Controls And Automation' (Shell32).

    PrintFile( FileName : Text[250])
    CREATE(objShell);
    IF NOT EXISTS(FileName) THEN
    EXIT;
    SplitDirFile(FileName,Dir,File);
    objFolder := objShell.NameSpace(Dir);
    objFolderItems := objFolder.Items;
    objFolderItem := objFolderItems.Item(File);
    objVerbs := objFolderItem.Verbs;
    i:=-1;
    REPEAT
    i+=1;
    IF i<objVerbs.Count THEN
    objVerb := objVerbs.Item(i);
    UNTIL (STRPOS(UPPERCASE(objVerb.Name),'PRINT')>0) OR (i >=objVerbs.Count);
    IF i<=objVerbs.Count THEN BEGIN
    objVerb.DoIt;
    END ELSE
    ERROR('Could not print file %1\Make sure you use a file extension you can print from Windows Explorer', FileName);


    This works fine. But now I have the next problem:
    What I want to do is print several files into one PDF file using Amyuni and then email the file.
    objVerb.DoIt; will do the printing but it does it asynchronus.
    I need to find a way to find out when the printing is finished so I can pick up the pdf file and attach it to an email.
    If I put a sleep(5000) command after all the PrintFile calls it actually delays the printing.

    The only way I can think of is using the timer and check if the datetime on the file has not changed in x seconds. I hate using the timer! it stuffs up debugging.
    Anybody got any ideas?

    Cheers
    Detlef
  • Options
    pdjpdj Member Posts: 643
    I would like to convert a html-file to pdf using Amyuni so we don't need yet another component.
    Did you find a solution to the problem?
    Regards
    Peter
  • Options
    DarrenBezzantDarrenBezzant Member Posts: 13
    I've tried your above code and it seems to work for everything but word documents - it will open the word Application, but doesnt even open the file.

    Anyone taken this any farther?
    Darren Bezzant NCPS, NCSD
    Bell Business Solutions
    Calgary, NB, CANADA
    darren.bezzant@bell.ca
  • Options
    Ashu_bhattAshu_bhatt Member Posts: 28
    Anybody wants to share the code of functions SplitDirFile function


    Thanks
  • Options
    MrWhoMrWho Member Posts: 59
    Thank u "Detlef", you're awesome, it worked like a charm still :)
  • Options
    jversusjjversusj Member Posts: 489
    thanks! this helped me tremendously in printing a PDF file!
    kind of fell into this...
  • Options
    SogSog Member Posts: 1,023
    Works great
    Unfortunatly the solution is not language independant (or even setup independant).
    So the only warning I could give is go to the file you want to print, right click it and see if you which command you can use to print it. Replace the 'PRINT' text in the code with the command specified.
    Nevertheless thank you!
    |Pressing F1 is so much faster than opening your browser|
    |To-Increase|
  • Options
    MBergerMBerger Member Posts: 413
    You could make an OCX or DLL exposing Windows' ShellExecute API function, then use that as an automation variable from NAV to call it with the 'print' option.
  • Options
    snehasneha Member Posts: 191
    What I am missing here? It's not working for me. I have added a below code to Report-On Post Report trigger. I want to print the Terms pdf file immediately to purchase order print. When I click on PO -> Print, I'm getting only the PO report page out, not the terms page.

    PrintThisFile := 'D:\Terms.pdf';
    IF ISCLEAR(objShell) THEN
    CREATE(objShell);

    SplitDirFile(PrintThisFile,Dir,FileName);

    objFolder := objShell.NameSpace(Dir);
    objFolderItems := objFolder.Items;
    objFolderItem := objFolderItems.Item(FileName);
    objFolderItem.InvokeVerb('PRINT');
    objVerbs := objFolderItem.Verbs;
  • Options
    gdkve9gdkve9 Member Posts: 161
    Below is the code I have used in the PrintFile function:
    FileName := 'E:\OrderDetails.pdf';
    Dir := 'E:\';
    File := 'OrderDetails.pdf';
    CREATE(objShell); 
    IF NOT EXISTS(FileName) THEN 
      EXIT;
    //SplitDirFile(FileName,Dir,File);
    objFolder := objShell.NameSpace(Dir); 
    objFolderItems := objFolder.Items; 
    objFolderItem := objFolderItems.Item(File); 
    objVerbs := objFolderItem.Verbs; 
    i:=-1; 
    REPEAT 
      i+=1; 
      IF i<objVerbs.Count THEN 
      objVerb := objVerbs.Item(i); 
    UNTIL (STRPOS(UPPERCASE(objVerb.Name),'PRINT')>0) OR (i >=objVerbs.Count); 
    IF i<=objVerbs.Count THEN BEGIN 
      objVerb.DoIt;
    END ELSE 
      ERROR('Could not print file %1\Make sure you use a file extension you can print from Windows Explorer', FileName);
    
    The function runs but their is no print initiated. Kindly suggest what could be the problem in my code? I am stuck ](*,)

    Awaiting for the responses. Thank u all in advance..
    Dilip
    Falling down is not a defeat..defeat is when you refuse to get up.
  • Options
    pdjpdj Member Posts: 643
    Your code won't work if PRINT is the last option. I'll suggest you replace
    IF i<=objVerbs.Count THEN BEGIN
    
    with
    IF STRPOS(UPPERCASE(objVerb.Name),'PRINT')>0 THEN BEGIN
    
    (Untested - it just seemed wrong)

    Edit: Not true - my bad...
    Regards
    Peter
  • Options
    BernardJBernardJ Member Posts: 57
    Try to debug with
    FileName := 'E:\OrderDetails.pdf';
    Dir := 'E:\';
    File := 'OrderDetails.pdf';
    CREATE(objShell); 
    IF NOT EXISTS(FileName) THEN 
      EXIT;
    //SplitDirFile(FileName,Dir,File);
    objFolder := objShell.NameSpace(Dir); 
    objFolderItems := objFolder.Items; 
    objFolderItem := objFolderItems.Item(File); 
    objVerbs := objFolderItem.Verbs; 
    i:=-1; 
    REPEAT 
      i+=1; 
      IF i<objVerbs.Count THEN BEGIN
        objVerb := objVerbs.Item(i);
        IF CONFIRM(objVerb.Name) THEN;  // debug purpose
      END;
    UNTIL (STRPOS(UPPERCASE(objVerb.Name),'PRINT')>0) OR (i >=objVerbs.Count); 
    IF i<=objVerbs.Count THEN BEGIN 
      objVerb.DoIt;
    END ELSE 
      ERROR('Could not print file %1\Make sure you use a file extension you can print from Windows Explorer', FileName);
    
  • Options
    gdkve9gdkve9 Member Posts: 161
    Hi Peter,
    pdj wrote:
    Your code won't work if PRINT is the last option. I'll suggest you replace
    IF i<=objVerbs.Count THEN BEGIN
    
    with
    IF STRPOS(UPPERCASE(objVerb.Name),'PRINT')>0 THEN BEGIN
    
    I tried working on your suggestion, but in both the cases the codeunit run outputs properties window as in the attachment instead intiating for the print.

    Any idea resolving the same would be very thankful..

    Awaiting for the reply.
    Dilip
    Falling down is not a defeat..defeat is when you refuse to get up.
  • Options
    pdjpdj Member Posts: 643
    Now I have tried your code, and I think I know your problem. First of all I got a compilation error with the variabel File, as it is a reservered word. That indicated you have translated your code to English for Mibuso 8)

    But that leads to the next problem. I guess you are not using an English PDF reader, and therefore the menu-item in the pdf-file context-menu-item does not contain "PRINT". In my case I had to change it to "UDSKRIV" which is Danish for PRINT.

    The reason for your code showing the Properties window, is that it is the last menu-item. My original suggestion should prevent that, but then you just get the error below :?

    I'll suggest you add this line to troubleshoot:
    MESSAGE('%1: %2',i,objVerb.Name);
    
    just before the UNTIL line...

    There doesn't seem to be a language independent solution to this problem, so you will need some way to determine which text to look for at the specifik users setup. It could be different for each file-extension for each user. ](*,)
    Regards
    Peter
Sign In or Register to comment.