|
本帖最后由 思乡之风 于 2017-10-21 08:42 编辑
Word文档格式都是固定的,请问怎样把代码改成可以输出下图的样子,万分感谢
代码如下:
- Sub Excel操控Word()
- Dim arrData(), n As Long, mypath$, myfile$
- Dim wdapp As Object, Docx As Object, strRng As Object
- mypath = ThisWorkbook.Path & "\需要提取的doc样表4个"
- myfile = Dir(mypath & "*.docx")
- Set wdapp = CreateObject("Word.Application")
- wdapp.Visible = False
- Do While myfile <> ""
- Set Docx = wdapp.Documents.Open(mypath & myfile, Visible:=False)
- Set strRng = Docx.Content
- With CreateObject("VBScript.Regexp")
- .Global = True
- .Pattern = "宗地基本信息表[(\(]一[)\)](?:.*?\x07){8}(.+?)\r\x07(?:.*?\x07){70}(.+?)\r\x07(?:.*?\x07){46}(.+?)\r\x07"
- ReDim Preserve arrData(1 To 3, 1 To n + 1)
- For Each RegMatch In .Execute(strRng)
- n = n + 1
- arrData(1, n) = RegMatch.submatches(0)
- arrData(2, n) = RegMatch.submatches(1)
- arrData(3, n) = RegMatch.submatches(2)
- Next
- End With
- Docx.Close
- myfile = Dir
- Loop
- wdapp.Quit
- Set wdapp = Nothing
- Set Docx = Nothing
- Cells.Clear
- Range("a1:c1") = Array("使用权", "宗地代码", "宗地面积" & vbCrLf & "(㎡)")
- Range("a2").Resize(n, 3) = Application.Transpose(arrData)
- End Sub
复制代码
测试.zip
(35.56 KB, 下载次数: 7)
|
|