NAV 2013/Word Interop - "Item is not a property" error

tomddlctomddlc Member Posts: 8
edited 2014-08-30 in NAV Three Tier
Hi all,

I'm hoping there is a .Net Interop expert in here, who can help me :-)

I have a problem using .Net Interop in NAV 2013. I'm trying to loop through all members of MailMergeField in a MailMergeFields collection.

My code looks like this:

WordDocument := WordHelper.CallOpen(WordApplication,TemplateFile,FALSE,FALSE);
WordDocument.Fields.Update;
WordMailMerge:=WordDocument.MailMerge;
WordMailMergeFields := WordMailMerge.Fields;
NoOfFields := WordMailMergeFields.Count;
FOR I := 1 TO NoOfFields DO BEGIN
WordMailMergeField := WordMailMergeFields.Item(I);

"Do something"
END;

I use the WordHelper class to open the document, and native Word.Interop for the rest. The code compiles nicely, but when I run it, I get an error saying that "Item is not a property" - when I try to call WordMailMergeFields.Item(I).

The variable "NoOfFields" is being set, so I my WordMailMergeFields is initialized correctly.

I would love to hear any suggestions.

Comments

  • mmperis8mmperis8 Member Posts: 19
    I have exactly the same problema than you.

    Item property has been removed from the Fields type after Office 2003 this is the cause of the error. In newer versions teorically we have to use the assignation like this:

    WordMailMergeField := WordMailMergeFields;

    I tested this in a .NET application and it works, but unfortunately NAV don't understand this because is expecting an array variable and you can not compile. I put a request to Microsoft with the problem, if they answer me I will keep you informed!!!

    In the meantime if anybody else has found a solution.... :whistle:
  • mmperis8mmperis8 Member Posts: 19
    Hello,

    I have a response from Microsoft, We have to pay for support in this incidence if we wan't they help us. Luckily, yesterday I was trying to advance in this feature in I found a solution.

    I made the develop in a .NET Class Library in C#, basically the code is the same as in NAV, but I can use a foreach instruction to make the loop. Once deployed the dll, paste the dll on Service and RoleTailoredClient Add-Ins folder. Now in NAV I can declare a DoNet variable and call the functions in my library from NAV.

    Hope it helps you!! :D
  • sailorsailor Member Posts: 15
    Use the Microsoft Office Interop in combination with the NAV Helper class.

    Variables:
    Name DataType Subtype
    wdApp DotNet Microsoft.Office.Interop.Word.ApplicationClass.'Microsoft.Office.Interop.Word
    wdDoc DotNet Microsoft.Office.Interop.Word.Document.'Microsoft.Office.Interop.Word
    wdFields DotNet Microsoft.Office.Interop.Word.Fields.'Microsoft.Office.Interop.Word
    wdRange DotNet Microsoft.Office.Interop.Word.Range.'Microsoft.Office.Interop.Word
    WordHelper DotNet Microsoft.Dynamics.Nav.Integration.Office.Word.WordHelper.'Microsoft.Dynamics.Nav.Integration.Office
    FileName Text 1024

    Don't forget to set the property "RunOnClient" to Yes for all DotNet variables.

    Code:
    [...]
    Add code to download file locally...
    FileName := 'C:\MyTemplateFile.dot';
    wdApp := wdApp.ApplicationClass;
    wdDoc := WordHelper.CallOpen(wdApp,FileName,FALSE,FALSE);
    wdDoc.Saved := TRUE;
    wdDoc.Fields.Update;
    wdFields := wdDoc.Fields;

    wdRange := wdFields.Item(1).Result;
    wdRange.Text := 'Hello World';
    [...]
  • navuser1navuser1 Member Posts: 1,329
    hi,
    This will work on NAS (JOB QUEUE) in NAV 2016 ?
    Now or Never
Sign In or Register to comment.