Printing External files from the Item Card

MauddibMauddib Member Posts: 269
I want to be able to add a button to the item card that will cause files external to Navision to print that are related to that item. Imagine a scenario where all files are in:

d:\Pictures
d:\Specs

all files are .jpg in pictures and either .doc or .xls in specs.

The file name ALWAYS matches the code No. off the item card. So if you are on item FM1234 then the files are always FM1234.jpg and FM1234.doc and FM1234.xls.

What would the C/AL Code for the onpush() trigger for the button be to print these files out ?

Answers

  • krikikriki Member, Moderator Posts: 9,118
    Well, with HYPERLINK, you can open the files automatically, but then you have to manually print it from the application that has opened the document.
    If you want it automatically print, you will have to use automation for each type of document.
    For the different types, if you search the forum, you will find examples for them.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    I remember a similar thread: Print files with an associated application
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • MauddibMauddib Member Posts: 269
    Im getting there. This works for excel:

    IF EXISTS('c:\temp\testbook.xls') THEN BEGIN
    xlApp.Workbooks.Open('C:\temp\testbook.xls');
    xlWorkSheet := xlApp.ActiveSheet;
    xlWorkSheet.PrintOut;
    END;

    Where xlAPP and xlWorkSheet are global variances of type Automation for excel.

    HOWEVER if I try the same for Word:

    IF EXISTS('c:\temp\testdoc.doc') THEN BEGIN
    wdApp.Documents.Open('C:\Temp\testdoc.doc');
    wdDoc := wdApp.ActiveDocument;
    wdDoc.PrintOut;
    END;

    OR

    IF EXISTS('c:\temp\testdoc.doc') THEN BEGIN
    wdApp.Documents.Open FileName:="C:\Temp\testdoc.doc";
    wdDoc := wdApp.ActiveDocument;
    wdDoc.PrintOut;
    END;

    NEITHER works. Any idea what the correct Syntax is? wdApp and wdDoc are instances of Automation for word 9.0.
  • DenSterDenSter Member Posts: 8,307
    You can try this from the office VBA editor. From there you get the intellisense dropdown list that shows you the whole object model, and inside you can browse to the print methods.

    Despite what common sense would make you think, each office application has its own team, so they all have their own ways of doing common tasks. I heard that they are working on streamlining this in the next release of Office though. Big help that is huh :).
  • MauddibMauddib Member Posts: 269
    The print isnt the problem im having though, its the OPEN part thats not working. IF I get past this stage Im sure ill get the print part working.
  • DenSterDenSter Member Posts: 8,307
    You'll need a reference to the Word Application object for the open method to work. I am guessing that in your excel code there is already a CREATE(xlApp) statement, or it works because you happen to already have Excel running.

    How it works exactly I don't know, but there's a Word management codeunit that you could take a look at.
    IF ISCLEAR(wrdApp) THEN
      CREATE(wrdApp);
    
    wrdApp is an unknown automation server in my locals list, but I'm sure it's something like "Microsoft Word".Application.
  • MauddibMauddib Member Posts: 269
    Sorry fraid this isnt it either :( I already have:

    IF NOT CREATE(wdApp,TRUE) THEN
    ERROR(Text001);

    There seems to be something wrong with the syntax of the command which ever way I use it. The OPEN command that is.

    It tells me Im not using the right parametres when I do it as:

    wdApp.Documents.Open FileName:="C:\Temp\testdoc.doc";

    Then it tells me it was expecting a variable when I do it as:

    wdApp.Documents.Open('C:\Temp\testdoc.doc');

    but if I do it by creating a string variable := C:\Temp\testdoc.doc

    and then do:

    wdApp.Documents.Open(myVar);

    The form compiles perfectly, but when I run it and click the button this code is in, it crashes out with a rather long error message for C/AL programmers detailing that C/AL doesnt support this format.

    Im baffled and its driving me up the wall. It cant be this easy to get the Excel one to work and this hard for the Word one!!!
  • DenSterDenSter Member Posts: 8,307
    There should be some working example in standard Navision, in the wordmanagement codeunit. Have you looked at that at all? I have not worked with this myself, but that's where I would start looking.
  • MauddibMauddib Member Posts: 269
    Argh no, they dont have anything for simply opening a file.

    Ive used the documents.open command exactly like it is in the help file for C/Side reference. I cant see what im doing wrong.

    It cant be that hard to simply OPEN a word document :(
  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    In codeunit 5054 WordManagement, function OpenWordAttachment, I notice following line:
    wrdDoc := wrdApp.Documents.Open2000(FileName,ParamFalse,Attachment."Read Only");
    

    Hope this helps you any further.
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • MauddibMauddib Member Posts: 269
    Actually this is only serving to confuse me further as open2000 isnt a valid function. If you hit compile while looking at that code unit it wont compile. Weirder and weirder.
  • MauddibMauddib Member Posts: 269
    AGH ITS SIMPLE!!!!!!

    For some reason the excel one will let you pass any parametres into it. For example I can set a text variable and use that as the file name. I can use the file name directly. Anything.

    But for the WORD one you HAVE to pass all variables in as type text or boolean. You cant pass in FALSE. or 'C:\temp\test.doc'. You cant even set a text constant and use that. You HAVE to create a boolean AND a text variable, set them to what you want and pass THEM in.

    A little bit anal if you ask me to restrict it that way, when the excel team didnt, but at least I now have it working.

    Thanks all for your time on this!!!
  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    Great!

    Please update your Topic Title (Let it start with [SOLVED]) so people know you found a solution.
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • DenSterDenSter Member Posts: 8,307
    Actually I like them to be restrictive. I don't like it when an object model has 2 million ways to do one simple task. It is much easier for me to remember two specific types of variables.

    Thanks for posting the solution, that'll be helpful to a lot of folks.
  • MauddibMauddib Member Posts: 269
    Id have to disagree though, and im sure it would be an interesting debate. But I prefer when theres many ways to get to the same solution. Especially when there are subtle differences. When one solution doesnt work you tweak it. More control, more power, and above all more fun when you are a programmer and you find a new path around a problem to acheive what you want. Restrictive solutions takes half the joy out.
Sign In or Register to comment.