VBA提取word表格内容到excel

这是一段提取word表格中部分内容的vb代码。

vbscript 复制代码
Sub 提取word表格()
mypath = ThisWorkbook.Path & "\"

myname = Dir(mypath & "*.doc*")

n = 4 ' index of rows


Range("A1:F1") = Array("课程代码", "课程名称", "专业", "备用1", "备用2", "备用3")

While myname <> ""

Set mydoc = GetObject(mypath & myname)

m = 4 ' index of rows of a table in word

With mydoc

With .Tables(1)


maxRows = .Rows.Count

'Debug.Print .Cell(2, 10).Range.text

While maxRows - m >= 2

Cells(n, 1) = .Cell(m, 3).Range.text
Cells(n, 1) = Left(Cells(n, 1), Len(Cells(n, 1)) - 1) '去除末尾小黑点,小黑点貌似代表换行符
Cells(n, 2) = .Cell(m, 4).Range.text
Cells(n, 2) = Left(Cells(n, 2), Len(Cells(n, 2)) - 1)

Cells(n, 3) = myname
m = m + 1
n = n + 1

'Cells(3, 1) = .Cell(3, 16).Range.text
'Cells(4, 1) = maxRows

Wend

End With

.Close False

End With

myname = Dir '再次调用,获取下一个文件名

Wend

MsgBox "COMPLETE"

End Sub

上述代码,可以把下面表格中课程代码和课程名称,提取到excel中。

相关推荐
Codiggerworld7 小时前
Vim 实战:在 VS Code、JetBrains、终端里玩转 Vim
编辑器·vim·excel
开开心心_Every9 小时前
多连接方式的屏幕共享工具推荐
运维·服务器·pdf·电脑·excel·tornado·dash
likerhood13 小时前
Word 两列文档中插入横跨两列的表格
word
、我是男生。16 小时前
给word添加mathtype插件的一些关键步骤!
word
Jun6261 天前
QT(4)-EXCEL操作
开发语言·qt·excel
草丛中的蝈蝈2 天前
word表格中当单元格内容过长时,分页后表格显示不连续。
word
dshudsnb2 天前
PDF转Word的3种方法(无乱码/批量/高精度教程)
pdf·word
2501_907136822 天前
Excel文件强效安全修复工具-清理安全风险 可修复WPS编辑后Excel无法打开的文件
excel·wps
AI刀刀2 天前
文心粘贴到 word 格式混乱,AI 导出鸭智能转文档零失真
人工智能·c#·word·ai导出鸭
一位代码2 天前
Excel 文本连接符 & :基础用法与进阶技巧详解
excel