vb.net 操作word问题 求大神
我利用Word_Workbook.Documents.Open打开了一个word并隐藏了它visible=0我现在想在不显示这个word的情况下读word中的表格数据,读...
我利用Word_Workbook.Documents.Open打开了一个word并隐藏了它visible=0
我现在想在不显示这个word的情况下读word中的表格数据,读到字符串、数组或者剪切板都可以,求问代码怎么写? 展开
我现在想在不显示这个word的情况下读word中的表格数据,读到字符串、数组或者剪切板都可以,求问代码怎么写? 展开
展开全部
在专业论坛上找了一个:
Sub Macro1()
Dim p$, f$, s$, a, arr, brr(1 To 6000, 1 To 20), d As Object, i&, l&, m&
Set d = CreateObject("scripting.dictionary")
a = Array("aaa", "身份证号码", "年龄", "姓名", "性别", "工作", "职业", "兴趣", "住址")
For i = 1 To UBound(a)
d(a(i)) = i
Next
p = ThisWorkbook.Path & "\"
With CreateObject("word.application")
.Visible = False
f = Dir(p & "*.doc")
Do While f <> ""
.Documents.Open p & f
For l = 1 To .ActiveDocument.Tables.Count
With .ActiveDocument.Tables(l)
For i = 1 To .Rows.Count
s = Replace(.Cell(i, 1).Range.Text, Chr(7), "")
s = Left(s, Len(s) - 1)
If d.Exists(s) Then brr(m + d(s), 2) = Replace(.Cell(i, 2).Range.Text, Chr(7), "")
Next
For i = 1 To 8
brr(m + i, 1) = a(i)
Next
End With
m = m + 9
Next
.ActiveDocument.Close
f = Dir
Loop
.Quit
End With
Set MyWord = Nothing
ActiveSheet.UsedRange.ClearContents
[a1].Resize(m, 2) = brr
End Sub
Sub Macro1()
Dim p$, f$, s$, a, arr, brr(1 To 6000, 1 To 20), d As Object, i&, l&, m&
Set d = CreateObject("scripting.dictionary")
a = Array("aaa", "身份证号码", "年龄", "姓名", "性别", "工作", "职业", "兴趣", "住址")
For i = 1 To UBound(a)
d(a(i)) = i
Next
p = ThisWorkbook.Path & "\"
With CreateObject("word.application")
.Visible = False
f = Dir(p & "*.doc")
Do While f <> ""
.Documents.Open p & f
For l = 1 To .ActiveDocument.Tables.Count
With .ActiveDocument.Tables(l)
For i = 1 To .Rows.Count
s = Replace(.Cell(i, 1).Range.Text, Chr(7), "")
s = Left(s, Len(s) - 1)
If d.Exists(s) Then brr(m + d(s), 2) = Replace(.Cell(i, 2).Range.Text, Chr(7), "")
Next
For i = 1 To 8
brr(m + i, 1) = a(i)
Next
End With
m = m + 9
Next
.ActiveDocument.Close
f = Dir
Loop
.Quit
End With
Set MyWord = Nothing
ActiveSheet.UsedRange.ClearContents
[a1].Resize(m, 2) = brr
End Sub
更多追问追答
追问
大哥,这不是vb.net代码啊,是VC的?
而且根本不符合要求啊
追答
代码很简单啊,是word下的VB代码,你用vb.net打开文档,把这段代码改一下就能用。
它的功能是把word里的一般表格里的数据存储到数组里。
确实搞不懂的话,建议里到word下做一下,先搞清楚它的机理再转到vb.net下就很容易了。word的宏代码是在vb的基础上扩展的,一般vb代码都能用,它主要是定义了些自身的对象和常用函数。
这里用到的字典类似于数组。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询