cancel
Showing results for 
Search instead for 
Did you mean: 

How to fill table in MS Word ?

SwatiAgrawal
Level 5
I want to fill various sections in a big table in MS Word like Name, Contact, Address, etc. Its a big document with lot of sections however what is seen below is a small part of it.

Name:

Contact:

     

Address:

     

Landmark:

     


I am using MS Word VBO for it. I am identifying cell value by hit and trial method using MS Word VBO - Get Table Cell Value. Then by using same cell value, I try to write the data in each cell by using MS Word VBO - Set Table cell value.
But when I write - For Eg: Tanush in "Name" box then it removes "Name" from that box and just writes "Tanush". However, I want "Tanush" to appear below "Name" in the same box as "Name"
Also, if I save "Tanush"  in a data item and then try to write using MS Word VBO - Set Table cell value then it just pastes "Data 1" in the Name box. Data 1 is the name of data item where I stored "Tanush".

Please help me to solve this.
Thanks in advance.

------------------------------
Swati Agrawal
------------------------------
1 BEST ANSWER

Helpful Answers

Hi Swati,

Apologies, I mistyped on my end a bit the code should have been doc.Tables.Item(TableNumber).Cell(Cell_X, Cell_Y).Range.Select  instead of doc.Tables.Item(TableNumber).Cell(Cell_X, Cell_Y).Select


Solution Walkthrough:

In order to achieve this, we can extend the "MS Word VBO" by creating a separate action within it and you can call it as "Align Cell Format In Table". This action will be having the following input parameters:

  1. handle (Number) : The data item having the handle value in the instance dictionary that needs to be queried. (This you can get from Create Instance action)

  2. document Name (Text) : The document name of the current workbook which the Blur Prism bot is operating on. (This you can get from Open Workbook action)

  3. Table Number (Number) : The table element index used in the current workbook to identify the table. (This value will be the same that you have been using as a part of the Set Table Cell Value action)

  4. Alignment Mode (Text) : The alignment mode according to which the table element needs to be aligned. (The possible values are: 'center', 'distribute', 'justify', 'justifyhi', 'justifylow', 'justifymed', 'left', 'right' or 'justifythai')

  5. Cell X (Number) : The row number of the cell where the formatting needs to be applied. (The index value starts from 1)

  6. Cell Y (Number) : The column number of the cell where the formatting needs to be applied. (The index value starts from 1)

Refer to the below workflow for more information:

14723.png
14724.png

Now, add a custom code stage with the following Input parameters and the code as shown below. No output parameters are required:

14726.png

14728.png14730.png
Code:

Const wdAlignParagraphLeft = 0
Const wdAlignParagraphCenter = 1
Const wdAlignParagraphRight = 2
Const wdAlignParagraphJustify = 3
Const wdAlignParagraphDistribute = 4
Const wdAlignParagraphJustifyMed = 5
Const wdAlignParagraphJustifyHi = 7
Const wdAlignParagraphJustifyLow = 8
Const wdAlignParagraphThaiJustify = 9

Dim currentMode As Integer = -1

Dim doc as Object = GetDocument(handle,documentname)

doc.Tables.Item(TableNumber).Cell(Cell_Y,Cell_X).Range.Select


Select Case AlignmentMode.Trim.ToLower

     Case "center"

         currentMode = wdAlignParagraphCenter

     Case "distribute"

         currentMode = wdAlignParagraphDistribute

     Case "justify"

         currentMode = wdAlignParagraphJustify

     Case "justifyhi"

         currentMode = wdAlignParagraphJustifyHi

     Case "justifylow"

         currentMode = wdAlignParagraphJustifyLow

     Case "justifymed"

         currentMode = wdAlignParagraphJustifyMed

     Case "left"

         currentMode = wdAlignParagraphLeft

     Case "right"

         currentMode = wdAlignParagraphRight

     Case "justifythai"

         currentMode = wdAlignParagraphThaiJustify

     Case Else

         Throw New Exception("Invalid alignment mode has been supplied")

End Select

doc.ActiveWindow.Selection.ParagraphFormat.Alignment = currentMode


You can publish this action and run the same from Process Studio to check your results.


Some of the test results are shown below:


Scenario: The table I have is aligned to the left and I want the first cell to be aligned to 'Center' only:

14731.png

14732.png

Result of the action is:

14733.png

Let us know if this helps you out!

------------------------------
----------------------------------
Hope it helps you and if it resolves you query please mark it as the best answer so that others having the same problem can track the answer easily

Regards,
Devneet Mohanty
Intelligent Automation Consultant
Blueprism 6x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------
------------------------------
---------------------------------------------------------------------------------------------------------------------------------------
Hope this helps you out and if so, please mark the current thread as the 'Answer', so others can refer to the same for reference in future.
Regards,
Devneet Mohanty,
SS&C Blueprism Community MVP 2024,
Automation Architect,
Wonderbotz India Pvt. Ltd.

View answer in original post

12 REPLIES 12

Hi Swati,

I would first suggest to exactly check the orientation of your table. In order to check this try applying border around the table of your interest once so see visually how it looks like.


Scenario 1: If the orientation is something like below:

14467.png

Name (X=1, Y=1)

Address(X=1, Y=2)

Contact(X=2, Y=1)

Landmark(X=2, Y=2)



Then while writing the name value, you will require an expression like: "Name" & NewLine() & [Name] , where [Name] data item is of text type holding the name we want to insert and here the X index will be 1 and Y index will also be 1. This expression needs to be provided in the 'Value' parameter of the 'Set Table Cell' action in 'MS Word VBO' as shown below:


Here I have a values collection having all the values that I need to set:

14468.png
14469.png


Scenario 2: In case your table orientation is like below:

14471.png

Name(X=1, Y=1)

Address(X=1, Y=2)

(X=2, Y=1)

(X=2, Y=2)

Contact(X=3, Y=1)

Landmark(X=3, Y=2)

(X=4, Y=1)

(X=4, Y=2)





Then you should directly use the data item in the cell index. For example, we can provide [Name] in the X index 2 and Y index 1 as shown below:

14472.png

Technically from what I guess is you are directly, setting the name data item in the scenario 1 type of table that I showed previously, hence it is overwriting the label 'Name' with your data stored 'Tarun'. Try appending the label with value while setting, it should work.

------------------------------
----------------------------------
Hope it helps you and if it resolves you query please mark it as the best answer so that others having the same problem can track the answer easily

Regards,
Devneet Mohanty
Intelligent Automation Consultant
Blueprism 6x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------
------------------------------
---------------------------------------------------------------------------------------------------------------------------------------
Hope this helps you out and if so, please mark the current thread as the 'Answer', so others can refer to the same for reference in future.
Regards,
Devneet Mohanty,
SS&C Blueprism Community MVP 2024,
Automation Architect,
Wonderbotz India Pvt. Ltd.

Hi Devneet,

Thanks for your help. It resolved my query.
I am also getting stuck at few more areas-

1. After running  'Set Table Cell' action in 'MS Word VBO',  the name appears as desired in the table in MS Word. But the name alignment shifts from center to left alignment. I want the name to again shift to center alignment. How can that be achieved ?

2. There are few checkboxes in the MS Word table. How can I check them Yes or No ? Y/N boxes are such that, as I will click on box of Yes or No, then it will get checked.

Does customer have passport?

☐Y     ☐ N

ABC

☐Y     ☐ N   ☐ N/A

Documents ?

☐Y     ☐ N

XYZ

☐Y     ☐ N


Thanks in advance.

------------------------------
Swati Agrawal
------------------------------

Hi Swati,

Glad your earlier issue got resolved. For the new issues, you are facing firstly the first issue seems to be a bit strange as on my machine I can see that if the allignment of the text inside the table cell was "Center" then upon using the above approach as well where we are appending the values, the appended cell value also is in the same allignment. So allignment technically should not change. Perhaps, you can check the allignment in your original document and revert back once on this post.

Coming to your second use case, for working with checkboxes first I will explain you a bit about the type of control that it is before proceeding with the solution. Checkboxes in Word documents are usually of the type Form Controls which you will be able to find from the "Developer" ribbon on your Word Application as shown below:

14478.png
First to automate any such use case where a lot of external components are included some standardization is required such as if we have a table then the table name ideally should be available otherwise we have to proceed with indices where the problem is that tomorrow if the position of the table changes then our index also won't work.

Similarly in case of checkboxes one needs to have them standardized as a best practice. For identifying checkboxes we have an identifier element called as "Tag" that is associated with them. You can check if your checkbox elements also have this tag value or not first by following the below steps:

    • First select the checkbox whose tag name you want to check, (like in my case I have selected the checkbox - "Yes") and then click on the "Properties" button under the Controls section of your Developer Ribbon on top.

      14479.png


  • Check if the tag name field is provided or not first in such a case as you can see from below:


14480.png

If the tag name is provided for all such controls it will be easier to implement the below solution otherwise you need to identify the correct sequence indices of all such checkboxes by hit and trial method using different index values in the solution I provide you.

This solution firstly works bot for either selecting or deselecting the checkboxes either by supplying the associated tag names or by supplying index values for such controls (index starts from 1, do remember that)


Solution Implementation:

In order to implement the solution, first extend the "MS Word VBO" by creating a separate action within it and you can call it as "Check Or Uncheck Checkbox". This action will be having the following input parameters:

  1. handle (Number) : The data item having the handle value in the instance dictionary that needs to be queried. (This you can get from Create Instance action)

  2. document Name (Text) : The document name of the current workbook which the Blur Prism bot is operating on. (This you can get from Open Workbook action)

  3. use Index (Flag) : The flag value indicating if the checkbox needs to be located using index or tag name. If the value is True, index based search will be performed and the flag is False, then tag name based search will be performed.

  4. index (Number) : The index value for the checkbox control on which the action needs to be performed. (The index values start from 1)
    NOTE: This value will be only used if the use Index data item value is True.

  5. tag Name (Text) : The tag name associated with the checkbox control on which the action needs to be performed.
    NOTE: This value will be only used if the use Index data item value is False.

  6. check Mode (Flag) : The flag value indicating if the selected checkbox needs to be checked or unchecked. If the flag value is True, then the checkbox will be marked as selected otherwise it will be deselected.


Refer to the below workflow for more information:

14481.png
14482.png

Now, add a custom code stage with the following Input parameters and the code as shown below. No output parameters are required:

14483.png


14484.png
Code:

Const wdContentControlCheckBox = 8
Dim d As Object = GetDocument(handle,document_name)
Dim success As Boolean = False

If use_index = False Then

    For Each ctrl In d.ContentControls

        If ctrl.Type = wdContentControlCheckBox And ctrl.Tag = tag_name Then

             ctrl.Checked = check_mode
             success = True

        End If

     Next

    If Not success Then

        Throw New Exception("No form control of checkbox type was found with the supplied tag name")

    End If

Else

        If d.ContentControls(index).Type = wdContentControlCheckBox Then

              d.ContentControls(index).Checked = check_mode

        Else

              Throw New Exception("The form control located at the supplied index is not of checkbox type")

        End If

End If


You can publish this action and run the same from Process Studio to check your results.


Some of the test results are shown below:

Scenario 1:  For the input file having both the checkboxes as unselected, using index based search for selecting "No" checkbox (index=2)

14485.png14486.png

Result of the action is:

14487.png

Scenario 2:  For the input file having  the "No" checkboxes as selected, using tag name based search for deselecting "No" checkbox (tag Name= "No")

14488.png

14489.png

Result of the action is:

14490.png

Let us know if it help and do mark the solution as the best answer if it resolves your query



------------------------------
----------------------------------
Hope it helps you and if it resolves you query please mark it as the best answer so that others having the same problem can track the answer easily

Regards,
Devneet Mohanty
Intelligent Automation Consultant
Blueprism 6x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------
------------------------------
---------------------------------------------------------------------------------------------------------------------------------------
Hope this helps you out and if so, please mark the current thread as the 'Answer', so others can refer to the same for reference in future.
Regards,
Devneet Mohanty,
SS&C Blueprism Community MVP 2024,
Automation Architect,
Wonderbotz India Pvt. Ltd.

Hi Devneet,

Thank you so much !! This worked for me.. 
In MS Word's properties, Tag was empty so I followed your steps and it worked.

I am just facing the left alignment problem. Just for one box, the alignment shifts from center to left however, for the remaining boxes alignment stays at center. I am not able to understand the reason behind.
Is their any way to resolve this ?
My collection looks as below -
14498.png
Just the first one,  Date:   14-12-2021  shifts to left however rest of the values like ABC, 50 remain at center. Seems strange to me.

14499.png
                              
Thanks in advance !!

------------------------------
Swati Agrawal
------------------------------

Hey Swati,

Glad to know your second issue got resolved 😊 Coming to your first issue, I suspect it might be some weird behavior with you internal table formatting for that word document while entering the value. However, one workaround that I can suggest is to change the formatting of the entire table itself to "Center" as in your case by invoking it explicitly using some code. You can implement the solution that I have stated below once you have set up all the cell values in your table.

Solution Implementation:

In order to achieve this, we can extend the "MS Word VBO" by creating a separate action within it and you can call it as "Align Table". This action will be having the following input parameters:

  1. handle (Number) : The data item having the handle value in the instance dictionary that needs to be queried. (This you can get from Create Instance action)

  2. document Name (Text) : The document name of the current workbook which the Blur Prism bot is operating on. (This you can get from Open Workbook action)

  3. Table Number (Number) : The table element index used in the current workbook to identify the table. (This value will be the same that you have been using as a part of the Set Table Cell Value action)

  4. Alignment Mode (Text) : The alignment mode according to which the table element needs to be aligned. (The possible values are: 'center', 'distribute', 'justify', 'justifyhi', 'justifylow', 'justifymed', 'left', 'right' or 'justifythai')

Refer to the below workflow for more information:

14521.png
14522.png

Now, add a custom code stage with the following Input parameters and the code as shown below. No output parameters are required:

14523.png
14524.png14525.png
Code:

Const wdAlignParagraphLeft = 0
Const wdAlignParagraphCenter = 1
Const wdAlignParagraphRight = 2
Const wdAlignParagraphJustify = 3
Const wdAlignParagraphDistribute = 4
Const wdAlignParagraphJustifyMed = 5
Const wdAlignParagraphJustifyHi = 7
Const wdAlignParagraphJustifyLow = 8
Const wdAlignParagraphThaiJustify = 9

Dim currentMode As Integer = -1

Dim doc as Object = GetDocument(handle,documentname)

doc.Tables.Item(TableNumber).Select


Select Case AlignmentMode.Trim.ToLower

     Case "center"

         currentMode = wdAlignParagraphCenter

     Case "distribute"

         currentMode = wdAlignParagraphDistribute

     Case "justify"

         currentMode = wdAlignParagraphJustify

     Case "justifyhi"

         currentMode = wdAlignParagraphJustifyHi

     Case "justifylow"

         currentMode = wdAlignParagraphJustifyLow

     Case "justifymed"

         currentMode = wdAlignParagraphJustifyMed

     Case "left"

         currentMode = wdAlignParagraphLeft

     Case "right"

         currentMode = wdAlignParagraphRight

     Case "justifythai"

         currentMode = wdAlignParagraphThaiJustify

     Case Else

         Throw New Exception("Invalid alignment mode has been supplied")

End Select

doc.ActiveWindow.Selection.ParagraphFormat.Alignment = currentMode


You can publish this action and run the same from Process Studio to check your results.


Some of the test results are shown below:


Scenario: The table I have is aligned to the left and I want the same to be aligned to the 'Center':

14526.png

14527.png

Result of the action is:

14528.png

Let us know if this helps you out!

------------------------------
----------------------------------
Hope it helps you and if it resolves you query please mark it as the best answer so that others having the same problem can track the answer easily

Regards,
Devneet Mohanty
Intelligent Automation Consultant
Blueprism 6x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------
------------------------------
---------------------------------------------------------------------------------------------------------------------------------------
Hope this helps you out and if so, please mark the current thread as the 'Answer', so others can refer to the same for reference in future.
Regards,
Devneet Mohanty,
SS&C Blueprism Community MVP 2024,
Automation Architect,
Wonderbotz India Pvt. Ltd.

Hi Devneet,

I did what you had mentioned.
What I noticed is that my entire word document and all the sections are in Table 2 with multiple cell x and cell y. This table also has some statements mentioned which are left aligned. So, when I run the action - "Align Table" then entire document shifts to center alignment. However, the statements in word need not be center aligned which are in different cells. 
So, I just want the cell which is shifting from center to left alignment, shifts back to center align.
Is that possible ?

Thanks in advance !!

------------------------------
Swati Agrawal
------------------------------

Hi Swati,

In this case, create additional two input parameters of type Number called as Cell X and Cell Y. And in the code stage as well map these data items.

In the code section you simply need to change this line: doc.Tables.Item(TableNumber).Select to doc.Tables.Item(TableNumber).Cell(Cell_X, Cell_Y).Select

This will only select the cell in that table which needs to be aligned as per the X and Y value you provide. 

Try it and let me know if this helps.

------------------------------
----------------------------------
Hope it helps you and if it resolves you query please mark it as the best answer so that others having the same problem can track the answer easily

Regards,
Devneet Mohanty
Intelligent Automation Consultant
Blueprism 6x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------
------------------------------
---------------------------------------------------------------------------------------------------------------------------------------
Hope this helps you out and if so, please mark the current thread as the 'Answer', so others can refer to the same for reference in future.
Regards,
Devneet Mohanty,
SS&C Blueprism Community MVP 2024,
Automation Architect,
Wonderbotz India Pvt. Ltd.

Hi Devneet,

I tried this but its giving me this exception - "Internal : Could not execute code stage because exception thrown by code stage: The requested member of the collection does not exist."

I will show you all steps with screenshots-
This is object -
14675.png
14677.png
14678.png
Now going to my process -

14679.png

Not sure if I am doing something wrong.

------------------------------
Swati Agrawal
------------------------------

Hi Swati,

Apologies, I mistyped on my end a bit the code should have been doc.Tables.Item(TableNumber).Cell(Cell_X, Cell_Y).Range.Select  instead of doc.Tables.Item(TableNumber).Cell(Cell_X, Cell_Y).Select


Solution Walkthrough:

In order to achieve this, we can extend the "MS Word VBO" by creating a separate action within it and you can call it as "Align Cell Format In Table". This action will be having the following input parameters:

  1. handle (Number) : The data item having the handle value in the instance dictionary that needs to be queried. (This you can get from Create Instance action)

  2. document Name (Text) : The document name of the current workbook which the Blur Prism bot is operating on. (This you can get from Open Workbook action)

  3. Table Number (Number) : The table element index used in the current workbook to identify the table. (This value will be the same that you have been using as a part of the Set Table Cell Value action)

  4. Alignment Mode (Text) : The alignment mode according to which the table element needs to be aligned. (The possible values are: 'center', 'distribute', 'justify', 'justifyhi', 'justifylow', 'justifymed', 'left', 'right' or 'justifythai')

  5. Cell X (Number) : The row number of the cell where the formatting needs to be applied. (The index value starts from 1)

  6. Cell Y (Number) : The column number of the cell where the formatting needs to be applied. (The index value starts from 1)

Refer to the below workflow for more information:

14723.png
14724.png

Now, add a custom code stage with the following Input parameters and the code as shown below. No output parameters are required:

14726.png

14728.png14730.png
Code:

Const wdAlignParagraphLeft = 0
Const wdAlignParagraphCenter = 1
Const wdAlignParagraphRight = 2
Const wdAlignParagraphJustify = 3
Const wdAlignParagraphDistribute = 4
Const wdAlignParagraphJustifyMed = 5
Const wdAlignParagraphJustifyHi = 7
Const wdAlignParagraphJustifyLow = 8
Const wdAlignParagraphThaiJustify = 9

Dim currentMode As Integer = -1

Dim doc as Object = GetDocument(handle,documentname)

doc.Tables.Item(TableNumber).Cell(Cell_Y,Cell_X).Range.Select


Select Case AlignmentMode.Trim.ToLower

     Case "center"

         currentMode = wdAlignParagraphCenter

     Case "distribute"

         currentMode = wdAlignParagraphDistribute

     Case "justify"

         currentMode = wdAlignParagraphJustify

     Case "justifyhi"

         currentMode = wdAlignParagraphJustifyHi

     Case "justifylow"

         currentMode = wdAlignParagraphJustifyLow

     Case "justifymed"

         currentMode = wdAlignParagraphJustifyMed

     Case "left"

         currentMode = wdAlignParagraphLeft

     Case "right"

         currentMode = wdAlignParagraphRight

     Case "justifythai"

         currentMode = wdAlignParagraphThaiJustify

     Case Else

         Throw New Exception("Invalid alignment mode has been supplied")

End Select

doc.ActiveWindow.Selection.ParagraphFormat.Alignment = currentMode


You can publish this action and run the same from Process Studio to check your results.


Some of the test results are shown below:


Scenario: The table I have is aligned to the left and I want the first cell to be aligned to 'Center' only:

14731.png

14732.png

Result of the action is:

14733.png

Let us know if this helps you out!

------------------------------
----------------------------------
Hope it helps you and if it resolves you query please mark it as the best answer so that others having the same problem can track the answer easily

Regards,
Devneet Mohanty
Intelligent Automation Consultant
Blueprism 6x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------
------------------------------
---------------------------------------------------------------------------------------------------------------------------------------
Hope this helps you out and if so, please mark the current thread as the 'Answer', so others can refer to the same for reference in future.
Regards,
Devneet Mohanty,
SS&C Blueprism Community MVP 2024,
Automation Architect,
Wonderbotz India Pvt. Ltd.