none
how can I move to the next line down in a section of a word document?

    Question

  • Hello fellows

    I am working on a small word macro that does the following

    1) select a table within the document

    2) vba adds a new section at the end of the document

    3) for each row, concatenate the contents of each cell in the row into a string

    4) enter the concatenated string for each row on a separate line in the new section

    when I execute my macro, it iterates trough each cell of every row of the table, then it writes the content of the string variable in one single line of the new section. When what I really want to do is: concatenate the contents of each cell in one cell, paste it in a line in the section then move down a line and write the string for the next row down of table, in the next line down of the section

    here is the code snippet if anyone is interested in helping a newbie out. thanks in advance guys

     

     

    'inserts a new page section at the end of the document
    ActiveDocument.sections.Add Start:=wdSectionNewPage
    
    For Each rr In Selection.Tables(1).Rows 'iterates through each row of the selected table
        For Each cc In rr.Cells 'iterates through each cell of each row of the table
          cellstring = cellstring & "..." & Left(cc.Range.text, Len(cc.Range.text) - 2) 'concatenates the contents of each row using the left
     funtion to remove the endofmarker mark
    Next cc
    ActiveDocument.sections(ActiveDocument.sections.count).Range.text = cellstring

     'writes what is in the string in the new section at the bottom of the document Next rr







    • Edited by flashy_lord Sunday, October 02, 2011 2:30 AM
    Sunday, October 02, 2011 2:12 AM

Answers

  • Use:

    Dim rngcell As Range
    Dim i As Long, j As Long
    ActiveDocument.Sections.Add Start:=wdSectionNewPage
    With Selection.Tables(1)
        For i = 1 To .Rows.Count
            For j = 1 To .Columns.Count
                Set rngcell = .Cell(i, j).Range
                ActiveDocument.Range.InsertAfter rngcell
            Next j
        Next i
    End With


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Sunday, October 02, 2011 7:56 AM