Hello,
Since the Word activity for inserting a datatable is somewhat slow for my use case, i am trying to use invoke code to find the already opened document, pass the datatable to insert in argument and insert it.
However i have the following issues:
- invoke code C#
Microsoft.Office.Interop.Word.Application wordApp = Microsoft.VisualBasic.Interaction.GetObject(null, "Word.Application") as Microsoft.Office.Interop.Word.Application;
i get the error : Invoke Code: No compiled code to run
error CS0234: The type or namespace name ‘Interaction’ does not exist in the namespace ‘Microsoft.VisualBasic’ (are you missing an assembly reference?) At line 1
- invoke code VBNet :
Dim wordApp As Microsoft.Office.Interop.Word.Application
wordApp = CType(Microsoft.VisualBasic.Interaction.GetObject(Nothing, "Word.Application"), Microsoft.Office.Interop.Word.Application)
Dim wordDoc As Microsoft.Office.Interop.Word.Document
wordDoc = wordApp.ActiveDocument
wordApp is nothing, even if i open a word document (not make UiPath do it) still same thing.
I thought about using invoke VBA but this accepts only string arguments.
Any suggestions?
@koutaiba_alabd try this in invoke code:
Dim wordDoc As Microsoft.Office.Interop.Word.Document = wordApp.ActiveDocument
' Then add your table:
Dim dt As DataTable = InArgumentDt ' pass your DataTable here
Dim rows = dt.Rows.Count
Dim cols = dt.Columns.Count
Dim wordTable = wordDoc.Tables.Add(wordApp.Selection.Range, rows + 1, cols)
For c = 1 To cols
wordTable.Cell(1, c).Range.Text = dt.Columns(c - 1).ColumnName
Next
For r = 1 To rows
For c = 1 To cols
wordTable.Cell(r + 1, c).Range.Text = dt.Rows(r - 1)(c - 1).ToString()
Next
Next
wordTable.AutoFitBehavior(Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitContent)
Also make sure - You add both Microsoft.Office.Interop.Word and Microsoft.VisualBasic in Invoke Code references
Pass your DataTable as an InArgument name InArgumentDt
Hello @koutaiba_alabd You can instead use Marshal.GetActiveObject which doesn’t require VB
using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
Application wordApp = null;
try
{
wordApp = (Application)Marshal.GetActiveObject("Word.Application");
}
catch
{
wordApp = new Application();
}
Document wordDoc = wordApp.ActiveDocument;
And make sure your arguments are
wordApp direction Out and type Microsoft.Office.Interop.Word.Applicationand for value wordAppVar
wordDoc direction Out of type Microsoft.Office.Interop.Word.Document and value wordDocVar
Also
create variable as per this
wordAppVar of type Microsoft.Office.Interop.Word.Application
wordDocVar of type Microsoft.Office.Interop.Word.Document
and in your namespace or import panel import this
Microsoft.Office.Interop.Word
System.Runtime.InteropServices
System.Data
Cheers
Hello, thanks for you suggestion. However, the same problem persists.
I modified the code you provided for debugging, here it is :
Try
Dim wordApp As Microsoft.Office.Interop.Word.Application
wordApp = CType(Microsoft.VisualBasic.Interaction.GetObject(Nothing, "Word.Application"), Microsoft.Office.Interop.Word.Application)
Console.WriteLine("Got wordApp")
Dim wordDoc As Microsoft.Office.Interop.Word.Document = wordApp.ActiveDocument
Console.WriteLine("Got wordDoc")
' Then add your table:
Dim dt As System.Data.DataTable = InArgumentDt ' pass your DataTable here
Dim rows = dt.Rows.Count
Dim cols = dt.Columns.Count
Dim wordTable = wordDoc.Tables.Add(wordApp.Selection.Range, rows + 1, cols)
For c = 1 To cols
wordTable.Cell(1, c).Range.Text = dt.Columns(c - 1).ColumnName
Next
For r = 1 To rows
For c = 1 To cols
wordTable.Cell(r + 1, c).Range.Text = dt.Rows(r - 1)(c - 1).ToString()
Next
Next
wordTable.AutoFitBehavior(Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitContent)
Catch ex As Exception
Console.WriteLine("Error" & vbCrLf & ex.Message)
End Try
And here is what i get in the console:
Got wordApp
Error
Object reference not set to an instance of an object.
As you can see, Dim wordDoc As Microsoft.Office.Interop.Word.Document = wordApp.ActiveDocument fails because wordApp is Nothing (not set to an instance of an object)
Here is my test sequence
Hello, thanks for you reply.
Couple of things:
try
{
Application wordApp = null;
wordApp = new Application();
Console.WriteLine("wordApp created");
Document wordDoc = wordApp.ActiveDocument;
Console.WriteLine("ActiveDocument fetched");
}
catch (Exception e)
{
Console.WriteLine("Error message");
Console.WriteLine(e.Message);
Console.WriteLine("Error Source");
Console.WriteLine(e.Source);
Console.WriteLine("Error Stack trace");
Console.WriteLine(e.StackTrace);
throw;
}
And still i have the same problem, here is the console
wordApp created
Error message
This command is not available because no document is open.
Error Source
Microsoft Word
Error Stack trace
at Microsoft.Office.Interop.Word.ApplicationClass.get_ActiveDocument()
@koutaiba_alabd
Try this
Dim dt As System.Data.DataTable = InArgumentDt
Dim wordDoc As Microsoft.Office.Interop.Word.Document = doc
Dim rows = dt.Rows.Count
Dim cols = dt.Columns.Count
Dim wordTable = wordDoc.Tables.Add(wordDoc.Content.End-1, rows + 1, cols)
For c = 1 To cols
wordTable.Cell(1, c).Range.Text = dt.Columns(c - 1).ColumnName
Next
For r = 1 To rows
For c = 1 To cols
wordTable.Cell(r + 1, c).Range.Text = dt.Rows(r - 1)(c - 1).ToString()
Next
Next
wordTable.AutoFitBehavior(Microsoft.Office.Interop.Word.WdAutoFitBehavior.wdAutoFitContent)
I’m sorry but i think you didn’t provide any declarartion for ‘doc’ in your code. Which is the central element that i’m having trouble with, getting the word document.
Thanks for your consideration