How to Print a WORD DOCUMENT in Navision

Hi, I would like to know if it is possible for me to print a word document through a command button on a form. The path of that word document can be found in a table. Here is the information: Table Item Spec Item No. Item Name Document (Code) (Text 30) (Text 100) T1 Testing 1 C:\WINDOWS\Desktop\T1.doc Thank you in advance.

With the Stream commands. Define variable OFile as datatype file and OStrean as datatype : Outstream. OFile.WriteMode(True); OFile.TextMode(True); Ofile.QueryReplace(True); OFile.Creat(‘C:\WINDOWS\Desktop\T1.doc’); OFile.CreateOutStream(Ostream); OStream.WriteText(“Item Spec”.“Item No.”); OFile.CLOSE; You need to add some looping to scroll tthrough the required tables and so on.

Ahem, gco, I think yummi was asking how to send a Word doc to the printer, not how to create it. In addition, I think that writing a .doc file in TextMode using stream commands will not yield a valid Word document… [B)] Yummi, you could use Automation for this task. The Application.PrintOut method of the Word Application object will accept the file name as parameter and send it to the printer. If you want the users to select a printer, you could open the printer dialog for the automation object. As an alternative, you could use C/AL’s SHELL function to invoke a command line printing command, if such a command is available on your system.

Xorph, I looked at the help section on “automation”, and I sort of understand what it is. At this point, I don’t really understand how to define its arguments. After I define the automation function, do I still need to use the “shell” function. If so, how do I use it? Thanks.

Hi Yummi, The SHELL function and the Automation stuff are alternatives - you do not need to use both. The Automation stuff works like defining C/AL Automation variables for Word.Application and Word.Document and using its methods - there are several tutorials available here that contain examples and guidelines. If you need any assistance, just ask [8D]

Xorph, Okay, I finally got some codes, although it compiled, it is not printing the word document. Here is what I got on the form’s “print document” onpush command trigger: IF “Doc. Specification Source” <> ‘’ THEN BEGIN Testing := ‘c:\Program Files\Microsoft Office\Microsoft Word.lnk’; SHELL(Testing); CREATE(MSWordApplication,FALSE); MSWordApplication.PrintOut(“Doc. Specification Source”); END; Could you tell me what I have done wrong, thank you.

Hi Yummi, There are several errors in this piece of code [:D] First, if you use Automation, you do not need to call SHELL. Second, a SHELL call to a .lnk file is not likely to start anything - did you see Word starting? Third, you can not simply omit leading parameters when calling a method via Automation as you can do in VB. You may, however, omit trailing parameters. I would try the following: CREATE(WordApp, TRUE/FALSE); // whatever you like more [;)] WordDoc := WordApp.Documents.Open(Doc.“Specification Source”); WordDoc.PrintOut; //… etc. WordDoc.Close; WordApp.Quit; (Disclaimer: This is off the top of my head, I haven’t tested this.) By using a document object, you need not mess around with PrintOut parameters, as the document to print is already specified by the object upon which this method is called. Hope this helps (if not, let me know, but I’ll only be able to answer tomorrow due to time zones difference [8D]),

Xorph, I tried your codes, and they gave me a C/AL programmer’s error saying that I couldn’t assign a_document to document. Afterwards, I modified the codes, and I could see the printer icon showed up on the right hand corner (looked like the document was being printed). I then went to the printer and nothing was printed, but an empty piece of paper. Anyway, here is the modified codes: IF “Doc. Specification Source” <> ‘’ THEN BEGIN CLEAR(MSWordApplication); CLEAR(MSWordDocument); CREATE(MSWordApplication,TRUE); CREATE(MSWordDocument,TRUE); MSWordApplication.Documents.Open(“Doc. Specification Source”); MSWordDocument.PrintOut; MSWordDocument.Close; MSWordApplication.Quit; END; I looked at the help documents, and tried other functions that made sense to the situation, but still I couldn’t get it working right. What should I look into in order to get this working? Thanks.

Yummi, You should not CREATE the Document object, instead it will be returned by the call to Documents.Open and MUST be assigned to the appropriate variable. The Automation stuff is a bit unstable when it comes to “nested” object references like a.b.c [:(] You should try instead IF Doc.“Specification Source” <> ‘’ THEN BEGIN CREATE(MSWordApplication, TRUE); MSWordDocuments := MSWordApplication.Documents; Dummy := FALSE; ReadOnly := TRUE; MSWordDocument := MSWordDocuments.Open(Doc.“Specification Source”, Dummy, ReadOnly); MSWordDocument.PrintOut; // etc.

Automation in general. I remember when doing the Development course, it only worked on 20% of the computers. The other 80% got error messages all the time. The teacher said it was because VB was not installed on the PC. Do you really need a VB installation to have all the necessary dll’s and all that ? I can’t imagine each client has to purchase a VB license if you want to use automation ?

gco, In general, you should not need to have VB, VBA or any other specific language installed. COM/Automation is Microsoft’s binary standard for storing reusable class/object definitions. This standard is independent of any programming language or environment. Thus, Word/Excel/whatever objects are just a bunch of binary code which complies to this COM standard. You can create and use these objects in any language that supports COM. A non-MS / non-VB example would be Adobe Acrobat, which also supports COM and therefore can be “remote-controlled” by COM-client applications (like Navision). There is much more to Automation than these few statements, but these are the basic principles (or did I miss anything signifcant?)

Xorph, I am still getting that error message that I can’t assign document to document with your new codes. I don’t know if I didn’t define the global variables correctly or what. Here is what I have: MSWordDoc - automation - msword document MSWordDocs - automation - msword documents MSWordApp - automation - msword application Dummy - boolean Readonly - boolean Thanks.

My curiosity of this question made me put up a test for it… The code frome xorph works :o) This is what i did: On the button i made i put in as locals (on the OnPush()): Name DataType Subtype Length WordApp Automation ‘Microsoft Word 9.0 Object Library’.Application Dummy Boolean WordDoc Automation ‘Microsoft Word 9.0 Object Library’.Document ReadOnly Boolean WordDocs Automation ‘Microsoft Word 9.0 Object Lib’.Documents Fname Text 30 And at the OnPush() i typed in this code: Fname := ‘c:\Automation.doc’; CREATE(WordApp, TRUE); WordDoc := WordApp.Documents.Open(Fname); WordDoc.PrintOut; WordDoc.Close; WordApp.Quit; And when i push the button i made… i walk towards the printer with some butterflys inside my stomach… and what do i see… the pefect word document i just made as an test for this… [:p] I use Microsoft Word 2000 (9.0.2720) and Navision Attain NO 3.01.B Good luck further yummi… [:)]

Hmm… now i see i have “killed” some of the code xorph made… caz of the error u write… hehe… but i worked without the code. That was the whole meaning of writing my thread…hehe… sorry… But i dont know how importent the code: MSWordDocuments := MSWordApplication.Documents; is for the application … but it works fine without it [;)] Maybe Xorph can explain if why if it is needed [:I]

Hello Frode, nice to hear from you again! [:)] Did you solve the problems with your Excel application that you mentioned ~ 2 weeks ago? Thanks for testing my code, you saved me from doing this myself [:D] FYI: The line MSWordDocuments := MSWordApplication.Documents; is just for making the code more stable - as I mentioned before, from time to time the Automation stuff barfs when cascading “.” operators are used. My previous experiences have shown me that it is good practice to use intermediate objects. If it works fine for you without it, great [8D] Sylvia, besides the possibly incorrect declaration of your Automation variables, I can’t imagine why your code still doesn’t work [:0] I have used the very same code that I posted here in our own system to open and show a Word document, and Frode has verified that printing works fine, too. Could you post your complete code, along with variable declarations? PS: Reading this posting in the forum, it came to my mind that I should try and control my use of the various smileys [:p]

Hi, everybody! After I posted my message, I realized that I didn’t declare those variables as locals for the Onpush trigger, and then after I read all of your messages, I fixed that. But still I am getting this error: “This message is for C/AL programmers. Invalid assignment. It is not possible to assign a_Document to a Document.” I had all the right variables, except the fact that I am using Microsoft word 8.0 object library, and also Navision Financials 2.60b. The last thing is I have a field called “Doc. Specification Source” where the path to the word document is stored. Since I just called it from the field, I didn’t make a text variable for that. Also, the locations of all the word document paths aren’t located on the same directory as my Navision Financial and also the Microsoft Word, so could all these contribute my codes getting that error message? sigh I really don’t understand what is the problem here. Please give me some suggestions, folks. Thanks a lot.

Sorry folks! I almost forgot to post my codes and variables, and here they are: OnPush() // Print out product specifications Nomin.GET(“Entry No.”); IF Nomin.“Doc. Specification Source” <> ‘’ THEN BEGIN CLEAR(MSWordApp); CREATE(MSWordApp,TRUE); MSWordDocs := MSWordApp.Documents; Dummy := FALSE; Readonly := TRUE; MSWordDoc := MSWordDocs.Open(Nomin.“Doc. Specification Source”,Dummy,Readonly); MSWordDoc.PrintOut; MSWordDoc.Close; MSWordApp.Quit; END; Local Variables: Name DataType Subtype Length MSWordApp Automation ‘Microsoft Word 8.0 Object Library’.Application MSWordDoc Automation ‘Microsoft Word 8.0 Object Library’.Document MSWordDocs Automation ‘Microsoft Word 8.0 Object Library’.Documents Dummy Boolean Readonly Boolean Nomin Record Nomination Thank you.


Originally posted by yummi MSWordDocs := MSWordApp.Documents;

Just try to remove that line and see if it works then… just for fun [:p] And xorph… yes, now it works perfect [8D] Just had to cut some of the quit and visible(fals) code… Noticed that less code less error [;)] Thnx for that help… owe u a big one there.

Hi, Frodio! Nope, I tried that also: both full version and simple version. None of them works on my machine. There could be something wrong with the word application on my machine. Anyway, I tried this piece of codes and it worked: Nomin.GET(“Entry No.”); IF Nomin.“Doc. Specification Source” <> ‘’ THEN BEGIN CLEAR(MSWordApp); CREATE(MSWordApp,TRUE); MSWordApp.Documents.Open(Nomin.“Doc. Specification Source”); MSWordApp.PrintOut; END; Do you have any idea why it works? The strange thing about this piece of codes is that while printing, it will give me the printer icon, and when you clicked on it, you couldn’t see the name of the file that you are printing. Finally, the word document got printed. I just don’t understand it at all. Something obviously isn’t working right.