Reputation: 781
I am using the following namespace: Microsoft.Office.Interop
oDocs
is my used Word-Instance
intColumns
is the amount of columns (converting from datatable)
intRows
is the amount of rows in the DataTable (+1 for colName)
Dim rng As Word.Range
rng = oDocs.Application.Selection.Range
rng.Text = strTable
Dim WordTable As Word.Table = rng.ConvertToTable(NumRows:=intRows + 1, _
NumColumns:=intColumns, Separator:=Word.WdSeparatorType.wdSeparatorColon)
This code is working quite good for most cases, but not for all. The Problem: The separator does not seem to be unused in the Values. So sometimes the Table is not created correctly.
I know you can select an individual separator when using the converttotable method in Word itself. How can i do that programmatically instead of being forced to use one of the five Enums?
Edit
Got it Working! Cindy Meister brought me to the right path;
Dim WordTable As Word.Table = rng.ConvertToTable(NumRows:=intRows + 1, NumColumns:=intColumns, Separator:="$")
"Spearator:= " suggests you do put en enum, you can also simply put a String with the .length of 1 into there. I used '$'
because its probably not used in the DataTable Values.
Upvotes: 0
Views: 1106
Reputation: 25663
Mmm, yes, this is not straightforward, but recording a macro reveals the secret...
When you set a custom separator using "Other" this sets the Word Application's DefaultTableSeparator
. And there is an Enum for that:
Dim wdApp as Word.Application = oDocs.Application
Dim rng As Word.Range = wdApp.Selection.Range
wdApp.DefaultTableSeparator = "|"
rng.Text = strTable
Dim WordTable As Word.Table = rng.ConvertToTable(NumRows:=intRows + 1, _
NumColumns:=intColumns, _
Separator:=Word.WdTableFieldSeparator.wdSeparateByDefaultListSeparator)
Upvotes: 1