Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Dec 2003
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macro to cycle through table cells (Word XP SP2)

    Hi all,

    I need a macro which cycles through all cells of all tables in a document. In each cell, it is to ask me whether to copy the cell content (to my clipboard manager) or not. If I say YES, it copies the cell content and jumps to the next cell, If NO, it goes to the next cell without copying.

    Can somebody please help me to find a code?

    Many thanks in advance,

    Andreas

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to cycle through table cells (Word XP SP2)

    Hello Andreas,

    Try this macro:

    Sub PromptCopyCells()
    Dim tbl As Table
    Dim cel As Cell
    Dim strCell As String

    On Error GoTo ErrHandler

    For Each tbl In ActiveDocument.Tables
    For Each cel In tbl.Range.Cells
    strCell = cel.Range.Text
    strCell = Left(strCell, Len(strCell) - 2)
    Select Case MsgBox("Do you want to copy '" & strCell & _
    "' to the clipboard?", vbYesNoCancel + vbQuestion)
    Case vbYes
    cel.Range.Copy
    Case vbNo
    ' Do nothing
    Case vbCancel
    GoTo ExitHandler
    End Select
    Next cel
    Next tbl

    ExitHandler:
    Set cel = Nothing
    Set tbl = Nothing
    Exit Sub

    ErrHandler:
    MsgBox Err.Description, vbExclamation
    Resume ExitHandler
    End Sub

  3. #3
    New Lounger
    Join Date
    Dec 2003
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to cycle through table cells (Word XP SP2)

    Hallo Hans,

    Thank you very much for your prompt help with macro. It s really great.
    However, it copies the text together with the cell frame. Can it be modified a little bit in such away that it copies only the cell text without cell frame?

    Andreas

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Macro to cycle through table cells (Word XP SP2)

    Try this (I haven't really tested it): replace the line cel.Range.Copy by

    Dim rng As Range
    Set rng = cel.Range
    rng.MoveEnd Unit:=wdCharacter, Count:=-1
    rng.Copy

    This omits the cell marker from the copied range. You may have to test for the cell being empty, I suspect you may get an error message if you try to copy an empty cell.

  5. #5
    New Lounger
    Join Date
    Dec 2003
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Macro to cycle through table cells (Word XP SP2)

    Hello Hans,

    Thanks again for your work. The macro is perfect.
    The error message you suspected does not come.

    I only added this little change:
    Case vbNo
    Set rng = cel.Range
    rng.MoveEnd Unit:=wdCell

    Thanks again very much.

    Best regards,
    Andreas

Posting Permissions

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