Friday, January 29, 2016

Creating the Word Template for MS Dynamics NAV 2016 (or) Export to Word

Hi, All
Today one of my client wants to Export the data from NAV to MS Word, for that i prepared documentation, you can also use, its working fine.

Automation lets you use the capabilities and features of Microsoft Office products,
such as Microsoft Word or Microsoft Excel, in your Microsoft Dynamics NAV application.
In this demonstration, we will implement Word Automation from a Estimating Project card in the Microsoft Dynamics NAV Windows client.

The letter will include information about the Estimating Project, such as the Estimating Project's name and the address and so on...

-------------------------------
Creating the Word Template for Use by Automation

First, you create a Word template that you will use to create letters to Projects.
To create the template, you add mail merge fields for displaying data that is extracted from Microsoft Dynamics NAV that you want included in the Estimating Project letter, such as the Project's name, Address, City, State, Dodge No. and so on...
You will create and save the template on the computer running the Microsoft Dynamics NAV Windows client, because you will configure the automation object to run on the client.

To create the template
1. On the computer running Microsoft Dynamics NAV Windows client, open Word and create a new document.
2. Choose where you want to insert the fields. Then, on the Insert tab, in the Text group, choose Quick Parts, and then choose Field.
3. In the Categories list, select Mail Merge.
4. In the Field names list, select MergeField.
5. In the Field Name box under Field Properties, type ProjectName. This field will display the name of your Project at the Estimating as taken from the Estimating Project table.
6. Choose OK to add the field.
7. Repeat steps 2 through 6 to add the remaining fields.
8. Save the Word document as a template with the name Template.dotx at C:\Temp\Estimating or a folder of your choice.
------------------------
Creating the Codeunit and Declaring the Variables

To create the codeunit
1. In Object Designer, choose Codeunit, and then choose the New button to create a new codeunit.
2. On the View menu, choose Properties to open the Properties window of the codeunit.
3. In the TableNo field, choose the AssistEdit button to open the Table List window.
4. In the Table List window, select the Estimating Project table, and then choose OK.
5. Close the Properties window.

To declare the variables
1. Choose the OnRun Trigger and on the View menu, choose C/AL Locals, and then choose the Variables tab.
2. On a blank line, type wdApp in the Name field and set the Data Type field to Automation.
3. In the Subtype field, choose the AssistEdit button. The Automation Object List window is displayed.
4. In the Automation Server field, choose the AssistEdit button.
5. In the Automation Server List, select Microsoft Word 15.0 Object Library if you are running Word 2013,
or select Microsoft Word 14.0 Object Library if you are running Word 2010, and then choose OK.
6. From the list of classes in the Automation Object List, select the Application class, and then choose OK.
7. Repeat steps 2 through 6 to add the following two Automation variables:
Name Data type Subtype Class
wdDoc Automation Microsoft Word 14.0/15.0 Object Library Document
wdRange Automation Microsoft Word 14.0/15.0 Object Library Range
TemplateName Text
-------------------------------------------------------------------------------
Writing the C/AL Code
1. In the C/AL Editor, add the following lines of code to the OnRun section.
2. To create an instance of Word before using it, enter the following line of code.
CREATE(wdApp, FALSE, TRUE);
This statement creates the Automation object with the wdApp variable.
o The first Boolean parameter in the statement (FALSE) tells the CREATE function to try to reuse an already running instance of the Automation server that is referenced by Automation before creating a new instance. If you change this to TRUE, then the CREATE function always creates a new instance of the Automation server.
o The second Boolean parameter in the statement creates the Automation object on the client. This is necessary to use this codeunit on a page in the Microsoft Dynamics NAV Windows client.

3. Enter the following lines of code to add a new document to Word that uses the template that you designed earlier.

TemplateName := 'C:\TEMP\Estimating\Template.docx';
IF NOT EXISTS(TemplateName) THEN
  ERROR('File Not Found');

wdDoc := wdApp.Documents.Add(TemplateName);
wdApp.ActiveDocument.Fields.Update;
------------Transferring Data to Word -------------
wdRange := wdApp.ActiveDocument.Fields.Item(1).Result;
wdRange.Text := "Job Name";
wdRange.Bold := 0;

wdRange := wdApp.ActiveDocument.Fields.Item(2).Result;
wdRange.Text := FORMAT("Bid Date");
wdRange.Bold := 0;

wdRange := wdApp.ActiveDocument.Fields.Item(3).Result;
wdRange.Text := Address;
wdRange.Bold := 0;

wdRange := wdApp.ActiveDocument.Fields.Item(4).Result;
wdRange.Text := Estimator;
wdRange.Bold := 0;

wdRange := wdApp.ActiveDocument.Fields.Item(5).Result;
wdRange.Text := City + State;
wdRange.Bold := 0;

wdRange := wdApp.ActiveDocument.Fields.Item(6).Result;
wdRange.Text := "ZIP Code";
wdRange.Bold := 0;

wdRange := wdApp.ActiveDocument.Fields.Item(7).Result;
wdRange.Text := "No.";
wdRange.Bold := 0;

wdRange := wdApp.ActiveDocument.Fields.Item(8).Result;
wdRange.Text := "Dodge Plan No.";
wdRange.Bold := 0;
wdRange := wdApp.ActiveDocument.Fields.Item(9).Result;
wdRange.Text := "L/P Specification No.";
wdRange.Bold :=0;

wdRange := wdApp.ActiveDocument.Fields.Item(10).Result;
wdRange.Text := FORMAT("L/P Specification Date");
wdRange.Bold := 0;


wdApp.Visible := TRUE;
wdApp.ActiveDocument.Fields.Unlink;
----------------------------------------------------------
-----IF you want to insert any Image/Shape then add this code
wdApp.Selection.ParagraphFormat.Alignment := 1;
LinkToFile := FALSE;
SaveWithDocument := TRUE;
FileName := ItemSpec."Image URL";
IF FileName <> '' THEN
  wdDoc.InlineShapes.AddPicture(FileName,LinkToFile, SaveWithDocument);
----------------------------------------------------------

Calling the Codeunit from the Estimating Project Card 
________________________________________
The final task is to ensure that you can call the codeunit from the Estimating Project Card page in the Microsoft Dynamics NAV Windows client.
To call the codeunit from the Estimating Project card page in the Microsoft Dynamics NAV Windows client.
1. Open Object Designer, and then choose Page.
2. Select the Estimating Project Card page and then choose Design.
3. On the View menu, choose Page Actions.
4. To add a new action, locate the action container with the subtype set to ActionItems.
5. Right-click the next line after the ActionItems container, and then choose New.
6. In the Caption field of the new line, type Word Letter.
7. Set the Type field to Action.
8. With the new action selected, on the View menu, choose Properties.
9. In the RunObject field, type codeunit Export Estimating in Word.
10. Save and compile the Estimating Project Card page.
---------------------------------------------------
To run the Estimating Project Card and view the Word letter
1. In Object Designer, choose the Page button.
2. Select the Estimating Project Card page, and then choose Run.
3. In the ribbon, on the Actions tab, choose the "Export Estimating in Word" action.
The letter document opens in Word.
-------------------------------------------------

References: MSDN



Thanks & Best Wishes
Binesh Singh Rajput
(MCP, MS, MCTS)

Thursday, January 14, 2016

How You Can Encode And Decode Text

Hi all,
Today I posted how you can Encode And Decode Text.


//<<----------------Encoding Text--------------------->>
//------Define parameter-------->>
Var Name DataType Subtype Length
No string Text 50
//<<------------END----------------

//------Define Return Value-------->>
Name Pass
Return Type Text
Length 50
//<<------------END----------------

//------Define Variables-------->>
Name DataType Subtype Length
Ch Char
i Integer
//<<------------END----------------
//------Define Functions-------------->>
LOCAL EncodeTxt(string : Text[50]) Pass : Text[50]
//--------------START----------------->>
FOR i := 1 TO STRLEN(string) DO
BEGIN
  Ch := string[i];
  IF (Ch < 32) OR ((Ch > 127) AND (Ch < 192)) THEN
    ERROR('its not an alphanumeric.');
  Pass[i] := 255-Ch;
END;
EXIT(Pass);
//<<----------------END-------------------


//<<----------------Decoding Text--------------------->>
//------Define parameter-------->>
Var Name DataType Subtype Length
No string Text 50
//<<------------END----------------

//------Define Return Value-------->>
Name Pass
Return Type Text
Length 50
//<<------------END----------------

//------Define Variables---------->>
Name DataType Subtype Length
Ch Char
i Integer
//<<------------END----------------
//------Define Functions-------------->>
LOCAL DecodeTxt(string : Text[50]) Pass : Text[50]
FOR i := 1 TO STRLEN(String) DO
BEGIN
  Ch := String[i];
  Pass[i] := 255-Ch;
END;
EXIT(Pass);
//<<----------------END-------------------



Thanks & Best Wishes
Binesh Singh Rajput
(MCP, MS, MCTS)

Sunday, January 10, 2016

Create Code128 Bar Code in NAV 2009

Hi all,
Today I posted how you can Create Code128 Bar Code.

//------Define parameter-------->>
Var Name DataType Subtype Length
No CodeString Text 250
//<<------------END-------------------

//------Define Return Value-------->>
Name 128BCodeString
Return Type Text
Length 250
//<<------------END-------------------

//------Define Variables-------->>
Name DataType Subtype Length
Offset Integer
HighAscii Integer
Total Integer
iCounter Integer
Holder Integer
Check Integer
ASCIIValue Integer
CheckDigit Integer
Character Char
//<<------------END-------------------

//------Define Functions-------------->>
CreateCode128BFont(CodeString : Text[250]) "128BCodeString" : Text[250]
//--------------START----------------->>
CLEAR(Offset);
CLEAR(HighAscii);
CLEAR(Total);
CLEAR(iCounter);
CLEAR(Holder);
CLEAR(Check);
CLEAR(Character);
CLEAR(ASCIIValue);
CLEAR(CheckDigit);

Offset := 32;
HighAscii := 66;
"128BCodeString"[1] := Offset + HighAscii + 104;
Total := 104;

FOR iCounter := 1 TO STRLEN(CodeString) DO BEGIN
  Character := CodeString[iCounter];
  ASCIIValue := Character;
  CheckDigit := ((ASCIIValue - Offset) * (iCounter));
  Total += CheckDigit;
  "128BCodeString"[iCounter + 1] := ASCIIValue;
END;
Check := Total MOD 103;
Holder := 0;

IF (Check + Offset >= 127) THEN
  Holder := Check + Offset + HighAscii
ELSE
  Holder := Check + Offset;


"128BCodeString"[STRLEN("128BCodeString")+1] := Holder;
Holder := 106 + Offset + HighAscii;
"128BCodeString"[STRLEN("128BCodeString")+1] := Holder;

CLEAR(iCounter);
FOR iCounter := 1 TO STRLEN("128BCodeString") DO
  IF("128BCodeString"[iCounter] = 32) THEN
    "128BCodeString"[iCounter] := 177;

EXIT("128BCodeString");
//<<----------------END-------------------


Thanks & Best Wishes
Binesh Singh Rajput
(MCP, MS, MCTS)

Thursday, January 7, 2016

Get the days that are not holiday or weekend

Hi all,
Today I posted how you can Get the days that are not holiday or weekend.

// Get the days that are not holiday or weekend
//-----Define Local Parameters---------
{
Var Name DataType Subtype Length
No StartDate Date
No EndDate Date
//-----Define Return type---------
Name WorkDays
ReturnType Integer
//-----Define Local variabls---------
Name DataType Subtype Length
DateRec Record Date [where Table ID: 2000000007]
HolidayRec Record Shop Calendar Holiday     [where Table ID: 99000753]
}
//-----Define Function---------
GetWorkingDays(StartDate : Date;EndDate : Date) WorkDays : Integer
// Strat ---------------------->>
WorkDays := 0;
DateRec.RESET;
HolidayRec.RESET;

DateRec.SETRANGE("Period Type",DateRec."Period Type"::Date);
DateRec.SETRANGE("Period Start",StartDate,EndDate);
DateRec.SETRANGE("Period No.",1,5);  // Get only weekdays
IF DateRec.FINDFIRST THEN
  REPEAT
    IF NOT HolidayRec.GET('HLP',DateRec."Period Start") THEN
      WorkDays += 1;
  UNTIL DateRec.NEXT = 0;
EXIT(WorkDays);
// End <<------------------------


Thanks & Best Wishes
Binesh Singh Rajput
(MCP, MS, MCTS)


Popular Posts