vbscript Word宏迭代表中的每个单元格并将数据提取到文本文件。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vbscript Word宏迭代表中的每个单元格并将数据提取到文本文件。相关的知识,希望对你有一定的参考价值。

Private Sub Document_New()

End Sub

Sub RetrieveTableItems()

   Dim oRow As Row
   Dim oCell As Cell
   Dim sCellText As String
   Dim i As Integer
   Dim strLine As String
   Dim strLines As String
   
   Dim OutputFileNum As Integer
   ' Turn on error checking.
   On Error GoTo ErrorHandler

   ' Loop through each row in the table.
   i = 1
   For Each oRow In ActiveDocument.Tables(1).Rows

      ' Loop through each cell in the current row.
      If i > 2 Then
      For Each oCell In oRow.Cells
         
            sCellText = oCell.Range
            ' Remove table cell markers from the text.
            sCellText = Left$(sCellText, Len(sCellText) - 2)
            
            strLine = strLine + sCellText + "|"
      Next oCell
    End If
    strLines = strLines + strLine + "^"
    strLine = ""
    i = i + 1
   Next oRow
OutputFileNum = FreeFile
Open "C:\Ahold\ChangeLog.txt" For Output Lock Write As #OutputFileNum
Print #OutputFileNum, strLines
Close OutputFileNum
MsgBox "Complete "

ErrorHandler:
   If Err <> 0 Then
      Dim Msg As String
      Msg = "Error # " & Str(Err.Number) & Chr(13) & Err.Description _
         & Chr(13) & "Make sure there is a table in the current document."
         MsgBox Msg, , "Error"
   End If

End Sub


以上是关于vbscript Word宏迭代表中的每个单元格并将数据提取到文本文件。的主要内容,如果未能解决你的问题,请参考以下文章

重新排序 UITableView 单元格并跟踪索引

Word宏用于仅从某些表格单元格中删除颜色

选择特定范围的单元格并仅删除突出显示的单元格并向上移动的 VBA

比较MS Word表格列中的值,合并单元格并删除重复值

VBA - 复制单元格并粘贴列中的空行

读取 df,拆分每个单元格并附加到列表