Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im writing a WORD procedure that fills in fields of a Word Template. In the Template document, after the header, there are 2 Textcolumns. Column 1 contains a Table, with a column break after the Table, and Column 2 is plain text. My question is: How do I set a Range variable to the 2nd Textcolumn?

I know I am doing something wrong but do not know what.

(A solution in C# is acceptable but VB.NET is preferred)

What I have tried:

Doing a Insertafter the last row of the Table in Column 1 and trying to get to column 2

Tried
Dim txtCol As Word.TextColumn
txtCol = oDoc.Range.PageSetup.TextColumns(2)
Posted
Updated 11-Feb-21 7:09am
v3

1 solution

If i understand you well...

I'd refer to range occupied by table, then select range till the end of document range, then loop through the collection of paragraphs. But(!) i have no idea how to get information about column number, which is not in table ;(

VB
Option Explicit

Sub SelectTextDownToTable1()
    Dim rng As Range
    Dim p As Paragraph
    
    Set rng = ThisDocument.Tables(1).Range
    rng.Start = rng.End + 1
    rng.End = ThisDocument.Range.End
    For Each p In rng.Paragraphs
        p.Range.Select
        Debug.Print Selection.Information(wdSelectionMode)
    Next p
End Sub


See:
Range.Tables property (Word) | Microsoft Docs[^]
Range.GoTo method (Word) | Microsoft Docs[^]
WdGoToItem enumeration (Word) | Microsoft Docs[^]
Range.Information property (Word) | Microsoft Docs[^]
WdInformation Enum (Microsoft.Office.Interop.Word) | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
fluffy3 12-Feb-21 5:17am    
Thanks. I assume 'go to end of range' is Collapse. I am not sure about go to next paragraph.
fluffy3 12-Feb-21 7:14am    
Thanks for your help. I found a solution that was so simple. Since I have control of the Template, I put a bookmark in column 2. I then used the following code:

Dim BookmarkName As String = "Column2"
Dim rngBookmark As Word.Range

rngBookmark = oDoc.Bookmarks(BookmarkName).Range
rngBookmark.Text = "This is Column 2"

I will use your solution if I do not have control of the template.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900