首页 > 解决方案 > VBA word macro to select indexed table cells

问题描述

I have one Word document containing 1 table. The table has 1 column and 190 cells, containing text, photos and mathtypes equations. I have a list of cell indexes in the current table, which I want to select via Visual basic for applications (VBA macro) and copy them to another document. For example, the list of indices is (1,3,7,9). The new document will contain:

the contents of cell 1, then move on to the next line

the contents of cell 3, then move on to the next line

the contents of cell 7, and so on

Is it possible to number the lines above as in the example below?

1) the contents of cell 1,

2) the contents of cell 3,

3) the contents of cell 7, ...

I found on the internet an example that, without experience in VBA, I could not develop:

Sub copyit()
 Dim docOld As Document
 Dim rngDoc As Range
 Dim tblDoc As Table

 If ActiveDocument.Tables.Count >= 1 Then
  Set docOld = ActiveDocument
  Set rngDoc = Documents.Add.Range(Start:=0, End:=0)
  For Each tblDoc In docOld.Tables
   tblDoc.Range.Copy
   With rngDoc
    .Paste
    .Collapse Direction:=wdCollapseEnd
    .InsertParagraphAfter
    .Collapse Direction:=wdCollapseEnd
   End With
  Next
 End If
End Sub

The "tblDoc.Range.Copy" statement needs to be replaced with cell selection, but I don't know how to proceed ... or how to number them!

标签: vbams-word

解决方案


推荐阅读