Welcome

Welcome to my blog. Here you can find few tips about Microsoft Dynamics NAV.

Thursday 16 February 2012

Read an Email in outlook through automation in Navision

we are all going to know about Reading an Email through automation in Navision

1) How to read an unread message  and filter based on sendername in the Inbox:
              Variables:
              1.outlookapplication-automation-'Microsoft Outlook 12.0 Object Library'.Application
              2.outlookemail-automation-''Microsoft Outlook 12.0 Object Library'.MailItem
              3.outlooknamespace-automation-Microsoft Outlook 12.0 Object Library'.NameSpace
              4.outlookitems-automation-Microsoft Outlook 12.0 Object Library'.Items
              5.outlookMAPIFolder-automation-Microsoft Outlook 12.0 Object Library'.MAPIFolder
              6.findcriteria-text-250
             7.endofloop-integer
             8. i - integer.
            9.Filenew-File
           10.Objout-Outstream

IF ISCLEAR(outlookapplication) THEN
  CREATE(outlookapplication);

outlooknamespace:=outlookapplication.GetNamespace('MAPI');
outlooknamespace.Logon('','',TRUE,FALSE);

outlookMAPIFolder:=outlooknamespace.GetDefaultFolder(6);
outlookitems:=outlookMAPIFolder.Items;
findcriteria:='[sendername] = ' 'Karthik Durairaj' ' ';
findcriteria:=findcriteria+' AND [Unread] = True';
outlookitems:=outlookMAPIFolder.Items.Restrict(findcriteria);


i:=1;
endofloop:=outlookitems.Count;
WHILE i<=endofloop DO
BEGIN
  outlookemail:=outlookitems.Item(i);
  IF CONFIRM(outlookemail.Subject) THEN
  BEGIN
   // will write the body of the email in a file
    Filenew.CREATE('d:\mailmessages1.txt');
    Filenew.CREATEOUTSTREAM(Objout);
    Objout.WRITETEXT(outlookemail.Body);
    Filenew.CLOSE;
     i:=i+1;
  END
END;

2)How to read an unread message in the subfolder:

outlooknamespace:=outlookapplication.GetNamespace('MAPI');
outlooknamespace.Logon('','',TRUE,FALSE);

outlookMAPIFolder:=outlooknamespace.GetDefaultFolder(6);
foldersnew:=outlookMAPIFolder.Folders; // where foldersnew=automation-Microsoft Outlook 12.0 Object Library'.Folders
foldercount:=foldersnew.Count;//where foldercount-integer
FOR i:=1 TO foldercount DO
BEGIN
subfolder:=foldersnew.Item(i);//subfolder-automation -Microsoft Outlook 12.0 Object Library'.MAPIFolder
IF subfolder.Name='Help Desk' THEN //"Help Desk' is my subfolder of Inbox
BEGIN
  outlookitems:=subfolder.Items;
  findcriteria:='[Unread] = True';
  outlookitems:=subfolder.Items.Restrict(findcriteria);
  j:=1;
  endofloop:=outlookitems.Count;
  WHILE j<=endofloop DO
  BEGIN
    outlookemail:=outlookitems.Item(j);
    IF CONFIRM(outlookemail.Subject) THEN
    BEGIN
      Filenew.CREATE('d:\mailmessages1.txt');
      Filenew.CREATEOUTSTREAM(Objout);
      Objout.WRITETEXT(outlookemail.Body);
      Filenew.CLOSE;
          j:=j+1;
    END;
  END;
END;
END;


Wednesday 15 February 2012

CreatePivotTable from Navision through Automation

We all know that to export to excel from Navision with the help of automation server. Now we are going to create pivot table for that excel with the help of automation:

1) We need to open then work book . Before that we need to Instantiate the automation server for the Excel application

     If isclear(Xlapp) then
       create(Xlapp)              // creating the instance where xlapp is the automation server of 'Microsoft Excel 12.0 Object Libray'.application

xlapp.Visible(TRUE);
xlbook:=xlapp.Workbooks.Open('path of your Excel sheet with extension');// where xlbook is the automation server of 'Microsoft Excel 12.0 Object Libray'.Workbook

XlPivotCache:=xlapp.ActiveWorkbook.PivotCaches.Add(1,'sheetname!range of cells');// where XlPivotcache is the automation server of 'Microsoft Excel 12.0 Object Libray'.Pivotcache


XlPivotCache.CreatePivotTable('','Pivottable6'); // this will create a pivottable with name as pivottable6 and create the pivot table in the new sheet.


//now we need to give the name for the sheet where the pivot table is created.
xlsheet := xlapp.ActiveSheet();
XlPivotTable := xlsheet.PivotTables('PivotTable6');
xlsheet.Name := 'Invoice Header Pivot'; 



XLPivotField := XlPivotTable.PivotFields(fieldname);
XLPivotField.Orientation:=1;// orientation is used to update the data fields in the RowLabels

XLPivotField := XlPivotTable.PivotFields(fieldname);
XLPivotField.Orientation:=1;
XLPivotField.Position:=2;// Position is used to update the data fields based on the orientation and in which position do we need that

if we need to update the columnLabels then the Orientation is 2. If we need update the Values then the orientation is 4.

Also in the Values column we have more function called sum, count,avg, etc.
we need to select that by using
XLPivotField."Function":=0(sum),1(count),.....