cancel
Showing results for 
Search instead for 
Did you mean: 

How to write values in a cell of table which is within a table in MS Word ?

SwatiAgrawal
Level 5
Hi,

I want to write values in a cell of a table which is already in a table in MS Word.
For Eg: In the screenshot below, I want to write my values/text in the yellow highlighted box. My entire word doc has one table number, i.e - 2. This table has lot of fields which want me to write the values.
I also want to write details like Phone Number within Table. But If I read value of this table then it reads all the values including values of table.25974.pngLooking forward for your help..
Thanks in advance !!

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

Best Answers

Hi Swati,

As @ewilson mentioned, we can create a new action to extend the MS Word VBO to achieve the execution of this use case. Just to showcase how you can implement the same in a detailed step by step process, please follow the below steps:

Solution Implementation:

25880.png

You need to create a new action, in my case I named it as 'Set Inner Table Cell Value' by duplicating the pre-built action 'Set Table Cell Value' and I just added an additional input parameter called as 'Inner Table Number' (Number) which indicates the nested table index starting from 1.

Next, I modified the name of the code stage to 'SetCellForInnerTable' with the following input parameters and the code as Eric mentioned earlier:


25881.png
Code:

Dim doc as Object = GetDocument(handle,documentname)
doc.Tables.Item(TableNumber).Tables.Item(InnerTableNumber).Cell(celly,cellx).Range.Text = CellValue


The only two things that I would mention is that in both this custom action and the 'Set Table Cell Value' action, the data item 'Cell X Number' indicates the column number and the data item 'Cell Y Number' indicates the row number which is actually bit opposite of the convention that I've used till now in all my custom actions for this VBO as I generally assume X to be row and Y to be column. In case you want to follow the same, just swap the variables 'cellx' and 'celly' in the above code.

Second thing is what Eric rightly mentioned, this only works for nested table up to one level. In case you have more nested tables in your document you will need to keep referring to that specific nesting which we have also done by using the Item attribute of the class Tables in our code.


Test Results:

I created somewhat a similar structure of document as you showed above where in my case, the outer table has only one column and the inner table has three columns (this might differ in your case that you would need to check):

25882.png

The workflow that I have used can be divided into two parts, one where I am assigning values to outer table elements and one where I am assigning values for inner table elements indicated by two separate blocks:

25883.png

For updating outer table elements, I use pre-built 'Set Table Cell Value' action. Here since I have only one column in my table so I have to update the value along with the label (here, it is 'Address-'):

25884.png

For updating the values of inner table elements, I am using the custom action 'Set Inner Table Cell Value' that we built:

25885.png

Upon running the workflow, the output that I am getting is as follows:

25886.png

Let us know if this helps to resolve your query!



------------------------------
----------------------------------

Regards,
Devneet Mohanty
Intelligent Process Automation Consultant
Blue Prism 7x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------
------------------------------
----------------------------------
Hope it helps you out and if my solution resolves your query, then please provide a big thumbs up so that the others members in the community having similar problem statement can track the answer easily in future.

Regards,
Devneet Mohanty
Intelligent Process Automation Consultant | Technical Business Analyst,
WonderBotz India Pvt. Ltd.
Blue Prism Community MVP | Blue Prism 7x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------

View answer in original post

9 REPLIES 9

ewilson
Staff
Staff
Hi @Swati Agrawal,

Unfortunately, this is not possible with the current MS Word VBO as the Set Table Cell Value action only touches the top-level Tables collection within the document. To do what you want you would need to create a new action.​ You could copy the existing Set Table Cell Value (maybe call it Set Nested Table Cell Value) action and then extend it to support the nested table.

If you look at the Code stage within the existing action this is what you should see:
Dim doc as Object = GetDocument(handle,documentname)
doc.Tables.Item(TableNumber).Cell(celly,cellx).Range.Text = CellValue​


To support a nested table, you would want to change the above code to look something like this:

Dim doc as Object = GetDocument(handle,documentname)
doc.Tables(TableNumber).Tables.Item(NestedTableNumber).Cell(celly,cellx).Range.Text = CellValue


Note that TableNumber will now represent the parent table. You will need to define a new input data item (named NestedTableNumber in the code above) that represents the nested table within the parent. That should be it for your specific use case.

Be aware that this change will only support nested tables one-level deep. In other words, if you had another table nested within a cell of the first nested table you would have to change the code to include one more depth of Tables(XXXX) reference. Does that make sense?

Cheers,
 



------------------------------
Eric Wilson
Director, Integrations and Enablement
Blue Prism Digital Exchange
------------------------------

Hi Swati,

As @ewilson mentioned, we can create a new action to extend the MS Word VBO to achieve the execution of this use case. Just to showcase how you can implement the same in a detailed step by step process, please follow the below steps:

Solution Implementation:

25880.png

You need to create a new action, in my case I named it as 'Set Inner Table Cell Value' by duplicating the pre-built action 'Set Table Cell Value' and I just added an additional input parameter called as 'Inner Table Number' (Number) which indicates the nested table index starting from 1.

Next, I modified the name of the code stage to 'SetCellForInnerTable' with the following input parameters and the code as Eric mentioned earlier:


25881.png
Code:

Dim doc as Object = GetDocument(handle,documentname)
doc.Tables.Item(TableNumber).Tables.Item(InnerTableNumber).Cell(celly,cellx).Range.Text = CellValue


The only two things that I would mention is that in both this custom action and the 'Set Table Cell Value' action, the data item 'Cell X Number' indicates the column number and the data item 'Cell Y Number' indicates the row number which is actually bit opposite of the convention that I've used till now in all my custom actions for this VBO as I generally assume X to be row and Y to be column. In case you want to follow the same, just swap the variables 'cellx' and 'celly' in the above code.

Second thing is what Eric rightly mentioned, this only works for nested table up to one level. In case you have more nested tables in your document you will need to keep referring to that specific nesting which we have also done by using the Item attribute of the class Tables in our code.


Test Results:

I created somewhat a similar structure of document as you showed above where in my case, the outer table has only one column and the inner table has three columns (this might differ in your case that you would need to check):

25882.png

The workflow that I have used can be divided into two parts, one where I am assigning values to outer table elements and one where I am assigning values for inner table elements indicated by two separate blocks:

25883.png

For updating outer table elements, I use pre-built 'Set Table Cell Value' action. Here since I have only one column in my table so I have to update the value along with the label (here, it is 'Address-'):

25884.png

For updating the values of inner table elements, I am using the custom action 'Set Inner Table Cell Value' that we built:

25885.png

Upon running the workflow, the output that I am getting is as follows:

25886.png

Let us know if this helps to resolve your query!



------------------------------
----------------------------------

Regards,
Devneet Mohanty
Intelligent Process Automation Consultant
Blue Prism 7x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------
------------------------------
----------------------------------
Hope it helps you out and if my solution resolves your query, then please provide a big thumbs up so that the others members in the community having similar problem statement can track the answer easily in future.

Regards,
Devneet Mohanty
Intelligent Process Automation Consultant | Technical Business Analyst,
WonderBotz India Pvt. Ltd.
Blue Prism Community MVP | Blue Prism 7x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------

Thanks a lot Eric.
Your solution helped me.
Thank you !!

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

Hi Devneet,

Thanks a lot for your detailed answer. It was very helpful and I am able to write my values in the inner table now. 
However, when I am writing values in the outer table then all the text in that table including inner table gets erased and then my value is written.
When I do "Get Table cell value" for that particular table where I have to write then I am using these cell values. By doing the below action, the result is the entire text of that table including inner cell.
25889.png
Now when I "Set table cell values" using the same table number, x, y then it erases all values and just writes "Swati - RPA Dev"

25891.png
If we take an example - like in below table if I just want to write 1234567890 against the phone number then it will erase all the text including inner table and will just write 1234567890.
25892.pngHow can we resolve this ?
Looking forward to your help.

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

Hi Swati,

I think the issue is because of the table structure. Can you do one thing, try selecting the entire table and select the border option to enclose border around each cell so that we can get an idea about the proper table structure. Try to show us how that table looks like with all the borders around each cell so that we can get a clear idea.

------------------------------
----------------------------------
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 Process Automation Consultant
Blue Prism 7x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------
------------------------------
----------------------------------
Hope it helps you out and if my solution resolves your query, then please provide a big thumbs up so that the others members in the community having similar problem statement can track the answer easily in future.

Regards,
Devneet Mohanty
Intelligent Process Automation Consultant | Technical Business Analyst,
WonderBotz India Pvt. Ltd.
Blue Prism Community MVP | Blue Prism 7x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------

Hi Devneet,
This parent table is huge, it is 4 pages long. 
I believe, the text which is before and after inner table is not written in a column, they are written normally without being in columns and rows. 

25915.png

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

Yeah that is exactly what I was guessing all along. In your case, the table fields in the outer table is not being written into individual rows or columns but is written into one cell as a whole. In such a case, you need to write the content of that entire cell just once using 'Set Table Cell Value' action. You can create a data item consisting of all the details, for example:

Name - Swati
Address - Test
Phone Number - 20020220

Then pass this data item to the 'Set Table Cell Value' action with the cell index as X=1 and Y=1.

Now here comes the tricky part, since your inner table is going to be removed once you write the cell content into this table. You will need to again recreate this table for which first you need to find the text that you just inserted and then set the point after that point. Once you pointer is set properly, then you can create this table again programmatically and write all the data back to the inner table using the below approach.

For your inner table the fields are in multiple rows and columns so here you need to use the 'Set Inner Table Cell Value' action multiple times with different cell indices for each cell.

Like I said the above approach can be bit tedious. The only way to counter the above approach is to change the template such that all fields fall onto different rows but again that being said this is something you would need to check from you client or business end.

------------------------------
----------------------------------
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 Process Automation Consultant
Blue Prism 7x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------
------------------------------
----------------------------------
Hope it helps you out and if my solution resolves your query, then please provide a big thumbs up so that the others members in the community having similar problem statement can track the answer easily in future.

Regards,
Devneet Mohanty
Intelligent Process Automation Consultant | Technical Business Analyst,
WonderBotz India Pvt. Ltd.
Blue Prism Community MVP | Blue Prism 7x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------

This approach seems quite tedious. I have huge data in different different formats and placing this data in data item would be quite tedious.
I was trying to use "Find Text" and "Type text" in MS Word VBO. This works for me to write what I want at the desired location. 
I am first clearing clipboard, then I am finding that particular text using MS Word VBO - Find Text , lets say "Address:" then I use MS Word VBO - Type text" wherein in text I give value - "Address: Noida, India".
This pastes this value at the desired location.
Now, the only problem I am facing is- "Address:" was in Bold letters and when I type "Address: Noida, India". then the entire thing gets bold. However while typing I just want "Address:" to be bold and "Noida, India" as not bold.
Is that possible ?

25942.png

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

Hi Swati,

If you already know which text you want to make bold and which one not then you can create a custom action to simply change the font type of the given text. For implementing this you can can create a new action named 'Change Font Type' to extend the MS Word VBO with input parameters as:

- handle (Number): The dictionary key holding the current word document instance

- document name (Text): The name of the word document where the instance is pointing

- text (Text): The string to be searched whose font style needs to be changed

- make_bold (Flag): The flag value indicating whether the string to be searched needs to be made as bold. By default, if no value is passed then the value is False.

- make_italic (Flag): The flag value indicating whether the string to be searched needs to be made as italic. By default, if no value is passed then the value is False.

- make_underlined (Flag): The flag value indicating whether the string to be searched needs to be made as underlined. By default, if no value is passed then the value is False.

While creating the flag data items, set the initial value to False. The reason why we are doing this is if we pass no parameters to these flag values, then by default the value will be evaluated to False, which means don't make the defined font change like bold, italic or underlined.

25955.png

Once the workflow has been built, in the code stage add the following input parameters and the code:

25956.png

25957.png25958.png

Code:

Dim found As Boolean
Dim d As Object = GetDocument(handle,document_name)
Dim w As Object = d.Application
Dim s As Object = w.Selection
Dim f As Object = s.Find
 
Try
 
   f.ClearFormatting
   With f
      .Text = Text
      .Replacement.Text = ""
      .Forward = True
      .Wrap = 1 'wdFindContinue
      .Format = False
      .MatchCase = True
      .MatchWholeWord = True
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
   End With
 
found = f.Execute
 
If make_bold = True Then
   s.Font.Bold = True
Else
   s.Font.Bold = False
End If
 
If make_italic = True Then
   s.Font.Italic = True
Else
   s.Font.Italic = False
End If
 
If make_underlined = True Then
   s.Font.Underline = True
Else
   s.Font.Underline = False
End If
Finally
 
   d = Nothing
   w = Nothing
   s = Nothing
   f = Nothing
 
End try


Test Results:

The input file I have is as follows, where I need to make the word "bold" as Bold:

25959.png

After running the workflow:

25960.png


------------------------------
----------------------------------
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 Process Automation Consultant
Blue Prism 7x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------
------------------------------
----------------------------------
Hope it helps you out and if my solution resolves your query, then please provide a big thumbs up so that the others members in the community having similar problem statement can track the answer easily in future.

Regards,
Devneet Mohanty
Intelligent Process Automation Consultant | Technical Business Analyst,
WonderBotz India Pvt. Ltd.
Blue Prism Community MVP | Blue Prism 7x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------