Word: Issue when creating nested tables in Word2021 from VB.NET

Bredo Norhall 0 Reputation points
2023-02-09T16:52:53.2833333+00:00

I'm struggling with an issue when trying to create nested tables in word from Inventor iLogic 2022 (basically VB.NET)

What I'm trying to achieve is something like this:

Image1

Text, 1. level table and texts in 1. level cells are working as intended. Even inserting a 2. level table into a cell seems to work. The issue arises when I want to include a text and then a table in the same cell (line 16 and 17). No matter what I do I end up with the table on top of the cell and the text below. Like this:

Image2

I've posted a simple test code below. I don't quite understand the "Range" object and believe this is why I can't get this to work as I want.

The code needs to be easy to edit by a non-developer, and build the entire word document from "Sub CreateWordContent". The finished document will consist of many different versions of the table in the image above.

Any and all help would be appreciated. I've tried searching the forum, google and ChatGPT, but haven't found the missing piece yet.

Thanks in advance

Sub CreateWordContent()
	Debug("Debug", "CreateWordContent Start")
	
	InsertTextLf("Text above table 1")
	InsertPrimaryTable(2, 2, 30, 70)
	InsertTextInCell(1, 1, "This is in cell 1, 1")
	InsertTextInCell(1, 2, "This is in cell 1, 2")
	InsertTextInCell(2, 1, "This is in cell 2, 1")
	InsertTextLf("Text below table 1")
	InsertTextLf("")
	
	InsertTextLf("Text above table 2")
	InsertPrimaryTable(2, 2, 30, 70)
	InsertTextInCell(1, 1, "This is in cell 1, 1")
	InsertTextInCell(1, 2, "This is in cell 1, 2")
	InsertTextInCellLf(2, 2, "This is in cell 2, 2")
	InsertSecondaryTableInCell(2, 2, 5, 2, 30, 68)
	InsertTextLf("Text below table 2")

	Debug("Debug", "CreateWordContent End")
End Sub

Sub InsertTextLf(sText As String)
	
	oWordDoc.Range.InsertAfter(sText & vbCrLf)
	
End Sub

Sub InsertPrimaryTable(iRows As Integer, iColumns As Integer, iWidthColumn1 As Integer, iWidthColumn2 As Integer)
	Debug("Debug", "InsertPrimaryTable Start")
	
	Dim oRange As Microsoft.Office.Interop.Word.Range
	 
	oRange = oWordDoc.Range
	oRange.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd)
	
	oPrimaryTable = oWordDoc.Tables.Add(oRange, iRows, iColumns)
	
	Dim dPageWidth As Decimal = oWordDoc.PageSetup.PageWidth
	Dim dLeftMargin As Decimal = oWordDoc.PageSetup.LeftMargin
	Dim dRightMargin As Decimal = oWordDoc.PageSetup.RightMargin
	Dim dAvailableWidth As Decimal = dPageWidth - (dLeftMargin + dRightMargin)

	Debug("Debug", "AvailableWidth: " & dAvailableWidth)
	Dim iWidth1Points As Integer = dAvailableWidth * iWidthColumn1 / 100
	Dim iWidth2Points As Integer = dAvailableWidth * iWidthColumn2 / 100
	
	oPrimaryTable.Columns(1).SetWidth(iWidth1Points, WdRulerStyle.wdAdjustFirstColumn)
	oPrimaryTable.Columns(2).SetWidth(iWidth2Points, WdRulerStyle.wdAdjustFirstColumn)
	
	oPrimaryTable.Borders.Enable = True
	
	Debug("Debug", "InsertPrimaryTable End")
End Sub

Sub InsertTextInCellLf(iRows As Integer, iColumns As Integer, sText As String)
	
	oPrimaryTable.Cell(iRows, iColumns).Range.InsertAfter(sText & vbCrLf)

End Sub

Sub InsertTextInCell(iRows As Integer, iColumns As Integer, sText As String)
	
	oPrimaryTable.Cell(iRows, iColumns).Range.InsertAfter(sText)

End Sub

Sub InsertSecondaryTableInCell(iCellRow As Integer, iCellColumn As Integer, iRows As Integer, iColumns As Integer, iWidthColumn1 As Integer, iWidthColumn2 As Integer)
	Debug("Debug", "InsertSecondaryTable Start")
	
	Try
		Dim oRange2 As Microsoft.Office.Interop.Word.Range
		oRange2 = oPrimaryTable.Cell(iCellRow, iCellColumn).Range
		oRange2.Collapse(Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd)
		oRange2.MoveEnd(WdUnits.wdCell, -1)
		
		oSecondaryTable = oWordDoc.Tables.Add(oRange2, iRows, iColumns)
		
		Dim dCellWidth As Decimal = oPrimaryTable.Cell(iCellRow, iCellColumn).Width

		Debug("Debug", "CellWidth: " & dCellWidth)
		Dim iWidth1Points As Integer = dCellWidth * iWidthColumn1 / 100
		Dim iWidth2Points As Integer = dCellWidth * iWidthColumn2 / 100
		
		oSecondaryTable.Columns(1).SetWidth(iWidth1Points, WdRulerStyle.wdAdjustFirstColumn)
		oSecondaryTable.Columns(2).SetWidth(iWidth2Points, WdRulerStyle.wdAdjustFirstColumn)
		
		oSecondaryTable.Borders.Enable = True
		
	Catch ex As Exception
		Debug("Error", "Error inserting secondary table: " & ex.Message)
	End Try
	
	Debug("Debug", "InsertSecondaryTable End")
End Sub
Word
Word
A family of Microsoft word processing software products for creating web, email, and print documents.
883 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,738 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,995 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.