Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

Need to close MS Word table which is created through X++

Posted on by 15

Hello All,

I have a requirement where I need to fetch all the objects(from shared project) names in table format and it should be in word document. With the object name I also want to fetch all the child nodes of objects.

For ex: I have Form1 in my project, form will contain methods, datasources as form's sub nodes, also method subnode will contains multiple methods. So all these methods I want in tabular format and in word document.

I have written code to create word document and print the list of all sub-sub nodes present under object. Problem which I am facing right now is, it is showing all object's sun node in one table. Ideally a table should get finish with the object's last sun node but it is not happening.

// Initialize Word object and document
    wordApplication = new COM("Word.Application");
    wordDocuments = wordApplication.documents();
    wordDocuments.add();
    wordDocument = wordDocuments.item(1);
 
    wordSelection = wordApplication.selection();
    wordRange = wordSelection.range();
 
    wordTables = wordSelection.tables();
 
    wordTable = wordTables.add(wordRange, 1, 2);


Does any one can guide me how to do end of a wordTable which is created in above code. I will need to create multiple tables in word doc, so it is necessary to close the earlier one otherwise it will create nested tables.

Thanks in advance!!!
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Need to close MS Word table which is created through X++

    This method adds a text to the end of a range. It's not related to tables.

    Here you can learn more about add(): Tables.Add method (Word).

  • AniketNimbekar Profile Picture
    AniketNimbekar 15 on at
    RE: Need to close MS Word table which is created through X++

    Sure, Please have a look

    wordRange.insertAfter(strFmt("%1", childComponents.AOTname()));

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Need to close MS Word table which is created through X++

    I'm sorry, but I have no idea what code you have in insertAfter(). You didn't show it to us.

  • AniketNimbekar Profile Picture
    AniketNimbekar 15 on at
    RE: Need to close MS Word table which is created through X++

    I am not clear how this first parameter of add() is defining the position in word. Can you please tell me what change should I made to change its position.

    Is this what you want to me to?

    wordRange1 = insertAfter(strFmt("%1", childComponent1.AOTname()));
    
    wordRange2 = insertAfter(strFmt("%1", childComponents2.AOTname()));

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Need to close MS Word table which is created through X++

    We're back at what I said in the first reply.

    The problem isn't that you use a loop in your code, but in the position in the document where you place your tables (that's at least my understanding of your problem). You seem to be putting them inside the first table while you want to put them below each other. Let me remind you that you define the position by the first argument of add().

  • AniketNimbekar Profile Picture
    AniketNimbekar 15 on at
    RE: Need to close MS Word table which is created through X++

    Ofcourse. Since I want to create new table for every subnode, I tried to put wordTable.add() inside while loop of childNodes. But now it is creating nested tables because I placed it in loop. I can not even declare wordTable or wordRange as array because it is COM class object. Can you please suggest any work around for this? Thanks in advance!!!

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Need to close MS Word table which is created through X++

    Your code does what you told it to do - you're indeed adding rows into a single table; you don't add any extra tables. If you want more tables, you must create them (remember wordTables.add() in your first code snippet).

    By the way, please make sure you use Insert > Insert Code to paste source code, as you did originally.

  • AniketNimbekar Profile Picture
    AniketNimbekar 15 on at
    RE: Need to close MS Word table which is created through X++

    My apoligies for not sharing the code. Please have a look on below code where I am trying to add child components into table:

    if (rootNode)//rootNode contains project name;

    {

    rootNodeIterator = rootNode.AOTIterator();

    childNodes = rootNodeIterator.next();

    while (childNodes)

    {

    childNodeIterator = childNodes .AOTIterator();

    childComponents = rootNodeIterator.next();

    while (childComponents)

    {

    wordRange = insertAfter(strFmt("%1", childComponents.AOTname()));

    childComponents = childNodeIterator.next()

    }

    childNodes = rootNodeIterator.next();

    }

    }

    This code is showing all child components of all child node in single row.

    But when I replace my code which is inside while (childComponents)  with below mentioned code, it is adding every child component in new row which solved my problem partially but again it is showing me all components in one table since I am not aware about the code which will close created wordTable. (for now just forget about that nested table statement which I mentioned in my earlier post)

    i++;

    wordTableCell = wordTable.cell(i, 1);

    wordTableRange = wordTableCell.range();

    wordTableCellRange.text(childComponents.AOTname());

    wordTableRows = wordTable.rows();

    wordTableRows.add();

  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    RE: Need to close MS Word table which is created through X++

    I don't understand what you mean by ending a table. Maybe your problem is in where you're adding your tables (the first argument of add()), e.g. putting all of them at the same place or at a place inside the first table.

    Unfortunately you haven't show us code relevant to your problem, i.e. where you're adding tables that end up being nested. Please do it if you need more help.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans