Word integration with dotnet variables

ACAEACAE Member Posts: 52
edited 2013-02-01 in NAV Three Tier
Hello,

I'm updating our current word integration so the automations are replaced by dotnet variables (for NAV 2013). This is basically the normal word integration of NAV, but we added some extra functions to insert pictures, tables, ...

This is now a real issue to update due to the fact that all parameters must be passed, even those that are optional.
I already found a topic suggesting to create a 'missing class' (viewtopic.php?f=32&t=51875&hilit=word+interop+.net), but this doesn't work anymore in NAV 2013:
- if I use Missing.MissingParamter I get the error 'Internal Compiler Error. Error Code = 00000022'
- if I use Missing.MissingValue I get the error 'A variable was expected'.

I also tried to pass a variable that is defined but not filled up, but then I get a type mismatch error. It concerns the word function "WordRange.InlineShapes.AddPicture" where the 4th parameter is a 'word range' variable, but if you pass a word range you get a type mismatch error. So I am kind of stuck here.

Does anyone know a solution for this ? I also tried to create a C# class but I also can't pass the range variable (type mismatch).

Kind regards,
Andy

Comments

  • ACAEACAE Member Posts: 52
    Hello,

    I finally solved this by creating a C# class and passing the whole word document to insert a picture.

    Perhaps a tip for those who also struggle with this: if you add a reference in Visual studio to Microsoft.Office.Interop.Word (or Excel, Access, ...) set "Specific version" to false. This has cost me a few hours to find out but it solved my type mismatch error :thumbsup:
  • mdPartnerNLmdPartnerNL Member Posts: 802
    Would you mind sharing the source of this class + the calling c/al code. Just to understand how things work..

    Of course you can remove some parts which should be protected.
  • ACAEACAE Member Posts: 52
    No problem, this is the code in C#:

    public bool InsertPictureInWordDoc(Microsoft.Office.Interop.Word.Document pnetWordDoc, string ptMergeField, string ptFileName)
    {
    Microsoft.Office.Interop.Word.Range lnetRange = null;
    Microsoft.Office.Interop.Word.Range lnetRange2;
    Microsoft.Office.Interop.Word.InlineShape lnetShape;

    var r = pnetWordDoc.Content;
    lnetRange = pnetWordDoc.Content;
    ptFileName = ptFileName.Trim();
    if (ptFileName == "")
    lnetRange.Delete();
    else
    if (lnetRange.Find.Execute(ptMergeField))
    {
    lnetRange2 = lnetRange;
    lnetRange.Delete();
    lnetShape = pnetWordDoc.InlineShapes.AddPicture(ptFileName, Type.Missing, Type.Missing, lnetRange2);
    }
    }
    return true;
    }

    And in C/AL:

    lnetNaviTransOfficeHelper.InsertPictureInWordDoc( pnetWordDocument, precMergeFields."Merge Field", ltxtPicture);

    where pnetWordDocument is a dotnet variable of type Microsoft.Office.Interop.Word.Document.'Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'
  • mdPartnerNLmdPartnerNL Member Posts: 802
    Thanks.

    1.
    So you are creating a reference in c/al to a word document

    2.
    Transfer a reference for this object to a c# class

    3.
    do things with it

    4.
    and back.


    Note:
    It seems like you are transferring the whole object but actually you are only sending an address to an object (word). So it must be fast and will take up only the extra class in memory.
  • ACAEACAE Member Posts: 52
    Hi,

    Yes that is about it. I used the NAV wordhelper to open the document and merge it, but in this routine it isn't possible to add pictures or tables.
    There are just some things to keep in mind in C#
    - Microsoft.Office.Interop.Word reference with 'specific version' = false
    - Do not pass the word document as 'ref' (or 'out') as this causes a NAV crash

    Otherwise it is pretty simple

    Andy
  • mdPartnerNLmdPartnerNL Member Posts: 802
    This will only work in NAV2013 ?

    Did you needed to do some special tricks to get it working in NAV 2009 RTC ?
  • ACAEACAE Member Posts: 52
    I suppose it should work in 2009, but in that version we used automations. For 2013 we try to replace all these with dotnet variables, but some issues need to be solved in VS
Sign In or Register to comment.