Consulting

Results 1 to 8 of 8

Thread: Excel VBA : how to merge cells in winword table with for... next

  1. #1
    VBAX Regular
    Joined
    Feb 2017
    Location
    Hong Kong
    Posts
    33
    Location

    Excel VBA : how to merge cells in winword table with for... next

    Under Excel VBA operating, I have to merge the cells in winword table with For ... Next function.
    but system error code was shown '5941' when run "Rng.Cells.Merge", I cannot fixed the problem,
    Could anyone guide me to modify that, thank you very much.



    Error Message :

    Run-time error '5941':
    The requested member of the collection does not exist.

    Target : Cells(1,1 & 2,1).Merge, Cells(1,2 & 2,2).Merge, Cells(1,3 & 2,3).Merge &
    Cells(1,5 & 2,5).Merge, Cells(1,6 & 2,6).Merge

    Figure : Winword Table below
    1,1 1,2 1,3 1,4 1,5 1,6
    2,1 2,2 2,3 2,4 2,5 2,6


    Coding :

    Sub mergeCellinTable()

    Dim ObjWord
    Set ObjWord = CreateObject("Word.Application")

    Dim objDoc
    Set objDoc = ObjWord.Documents.Add

    ObjWord.Visible = True
    objDoc.Activate

    With ObjWord

    Dim Table1

    Set Table1 = objDoc.tables.Add(Range:=objDoc.ActiveWindow.Selection.Range, _
    NumRows:=6, _
    NumColumns:=6, _
    DefaultTableBehavior:=wdWord8TableBehavior)


    Dim Rng

    For I = 1 To 6
    If I <> 4 Then
    With objDoc.tables(1)
    Set Rng = .Cell(1, I).Range
    Rng.End = .Cell(2, I).Range.End
    Rng.Cells.Merge
    Rng.Text = "Merged Column Cells"
    End With
    End If
    Next I
    End With

    End Sub

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Sub mergeCellinTable()
    Dim ObjWord, objDoc, Table1
    
    Set ObjWord = CreateObject("Word.Application")
    Set objDoc = ObjWord.Documents.Add
    ObjWord.Visible = True
    Set Table1 = objDoc.tables.Add(Range:=objDoc.ActiveWindow.Selection.Range, NumRows:=6, NumColumns:=6, DefaultTableBehavior:=wdWord8TableBehavior)
    With Table1
      For I = 1 To 6
        If I <> 4 Then
          .Cell(1, I).Merge MergeTo:=.Cell(2, I)
          .Cell(1, I).Range.Text = "Merged column cells"
        End If
      Next I
    End With
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Regular
    Joined
    Feb 2017
    Location
    Hong Kong
    Posts
    33
    Location

    Run error code 424 Object Required

    Thanks for your help, your coding is work. but the error code : 424 was shown when I add them into my master Function.

    Error Coding : .cell(K, I).Merge MergeTo:=.cell(K + 1, I)
    Error Code : Run error code 424 Object Required


    Coding Summary:

    Dim objWord As Object, objDoc As Object, objSelection As Object
    Dim TableNo As Integer 'table number in Word
    Dim iRow As Long 'row index in Excel
    Dim iCol As Integer 'column index in Excel
    Dim Table1



    Set objWord = CreateObject("Word.Application")
    Set objDoc = objWord.Documents.Open("C:\ABC.doc")
    objWord.Visible = True
    Set objSelection = objWord.Selection



    With objDoc

    TableNo = objDoc.tables.Count

    If TableNo = 0 Then
    MsgBox "This document contains no tables", _
    vbExclamation, "Import Word Table"

    ElseIf TableNo > 1 Then

    TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
    "Enter table number of table to import", "Import Word Table", "1")

    End If

    With .tables(TableNo)

    For iRow = 0 To 10
    If iRow < 10 Then

    '========================================================================== ====
    ' Test Program : if TempData(1,4) = True ? if Yes, Cells(1,2).merge exclude Column 4
    '========================================================================== ====

    If TempData(iRow + 1, 4) = True Then

    With Table1
    For I = 1 To 6
    If I <> 4 Then
    K = (J - 1) * 2 + 1

    .cell(K, I).Merge MergeTo:=.cell(K + 1, I)
    .cell(K, I).Range.Text = "Merged column cells"
    End If
    Next I
    End With

    End If
    '========================================================================== ====
    ' End of Test Program Photo attached ?
    '========================================================================== ====
    '
    '========================================================================== ====
    ' Below Coding <ba ba ba ... > for
    ' General information and Provide one Row if TempData ( iRow, 4 ) = True ?
    ' Information : Photo on Cells (3, 2)
    '========================================================================== ====

    End If
    Next iRow
    End With
    End With

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
      With .tables(TableNo)
        For iRow = 0 To 10
          If iRow < 10 Then
            '========================================================================== ====
            ' Test Program : if TempData(1,4) = True ? if Yes, Cells(1,2).merge exclude Column 4
            '========================================================================== ====
            If TempData(iRow + 1, 4) = True Then
              For I = 1 To 6
                If I <> 4 Then
                  K = (J - 1) * 2 + 1
                  .cell(K, I).Merge MergeTo:=.cell(K + 1, I)
                  .cell(K, I).Range.Text = "Merged column cells"
                End If
              Next I
            End If
            '========================================================================== ====
            ' End of Test Program Photo attached ?
            '========================================================================== ====
            '
            '========================================================================== ====
            ' Below Coding <ba ba ba ... > for
            ' General information and Provide one Row if TempData ( iRow, 4 ) = True ?
            ' Information : Photo on Cells (3, 2)
            '========================================================================== ====
          End If
        Next iRow
      End With
    …but what's the value of J in K = (J - 1) * 2 + 1
    Last edited by p45cal; 02-24-2017 at 04:52 AM.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Regular
    Joined
    Feb 2017
    Location
    Hong Kong
    Posts
    33
    Location
    P45cal, J and K are integer (row number) and control the data copy to correct row in winword table.

    Target :
    Copy data(1) to first row
    If data(1,4) is false then
    Second row is no information
    End if

    Copy data(2) to third row
    If data(2,4) is true then
    Copy Data(2,4) to forth row
    Fifth row is no information
    End if

    Copy data(3) to sixth row
    Bababa

    I m not familiar to use the excel source to control winword but I need to learn and understand it, thanks
    Last edited by justuptou; 02-24-2017 at 05:30 AM.

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Yes, but what is the VALUE of J at that point?
    Step through the code with F8 on the keyboard, and when you get to that line, hover over J with your mouse; it may show its value (if it's a global variable) or, as I suspect, it may say =Empty or some such.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Did you solve this one?
    (I see you've posted a new different thread since I replied.)
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  8. #8
    VBAX Regular
    Joined
    Feb 2017
    Location
    Hong Kong
    Posts
    33
    Location
    Solved, thank you very much.

    The problem is variable of table name, I found it when I checked global variable in each step.

    thank you.

    P.S. but I m new bird, I don't know how to set "Solved" in this forum, thanks again.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •