|
本帖最后由 gwfzh 于 2018-9-26 23:08 编辑
各位老师: 想将同文件夹下的word文档复制到本表中,可是程序运行到下面代码“SetMyRange = .Range(StartRange, EndRange) ”语句处时,出现运行时错误"48",加载DLL错误,怎么解决?是什么原因,请各位老师帮忙了!!!,谢谢!!!
wordtoexcel.rar
(549 KB, 下载次数: 3)
-
- Sub AlineInWord() '逐行读取复制109行=TarLines = .BuiltinDocumentProperties("Number of lines").Value
- ' Dim WD As New Word.Application 'word程序的父对象'前期绑定,好处是在对象后输入句点可以给出快速提示,因为需要先引用对象,所以容易出现版本兼容问题。
- ' Dim Doc As Word.Document '子对象、word具体上档文档对象
- ' 'Dim WD As Object '后期绑定,没有提示,根据运行代码机器上对象的版本创建对象,兼容性好。
- Dim WD, Doc, MyRange As Word.Range
- Dim word行数, 读行号%, 每次读行数%, Excel行号%
- Dim MyPath$, FN$, cPath$, cFile$, I%, arr()
- Dim TarLines As Long, StartRange As Long, EndRange As Long, Str, krr()
- Application.ScreenUpdating = False
- krr() = Array("姓名", "年龄", "出生日期", "详细住址", "联系人", "联系电话", "人口数", "家庭劳动力数", "人年收入", "人均收入)", "医疗费用", "交通费", "食宿费", "附加费用", "本人误工损失", "家属的误工损失")
- ' Set WD = CreateObject("Word.Application") '新建Word对象
-
- MyPath = ThisWorkbook.Path & ""
- Fp = ThisWorkbook.Path
- ReDim arr(1 To 10, 1 To 112)
- With CreateObject("word.application") '创建word进程
- .Visible = False '隐藏执行
- .AutomationSecurity = msoAutomationSecurityForceDisable
- .DisplayAlerts = False '''取消警报提醒
- FN = Dir(Fp & "" & "*.doc")
- sText = Right(FN, 7) ' = "调查表.doc"
- Do While Right(FN, 9) = "个案调查表.doc" '遍历WORD
-
- ' Set Doc = .Documents.Open(ThisWorkbook.Path & "" & FN)
- ' With Doc '.Documents.Open(Fp & "" & FN) '打开目标word文档
- With .Documents.Open(Fp & "" & FN) '打开目标word文档
- n0 = n0 + 1
- word行数 = .BuiltinDocumentProperties(wdPropertyLines).Value
- TarLines = .BuiltinDocumentProperties("Number of lines").Value 'DOC文档的行数
- lineStart = .GoTo(What:=wdGoToLine, Which:=wdGoToNext, Count:=1).End
- m = .Range.ComputeStatistics(wdStatisticLines)
- ReDim arr(1 To 10, 1 To TarLines)
- 每次读行数 = 1
- ' Excel行号 = 2
- ' StartRange = wdGoToLine
- For 读行号 = 3 To TarLines
- ' For 读行号 = 1 To LineCount
- If .Content.End <= 1 Then Exit Sub '如果没有文档内容则退出宏
- ' For 读行号 = 1 To word行数
- ' With WD.Selection
- ' .GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=读行号
- ' .MoveDown wdLine, 每次读行数, wdExtend
- ' If Val(.Text) > 0 Then
- ' Range("A" & Excel行号) = .Text
- ' Excel行号 = Excel行号 + 1
- ' End If
- 'Str0 = .wdLine(读行号).Text '方法和数据成员未找到错误
- StartRange = .GoTo(wdGoToLine, , 读行号).Start '指定行号的始点位置 '如果输入行号与DOC的总行数一致 , 则终点位置为文档末位置, 反之则为下一行的起点
- EndRange = VBA.IIf(读行号 = TarLines, .Content.End, .GoTo(wdGoToLine, , 读行号 + 1).Start)
- Set MyRange = .Range(StartRange, EndRange) '定义一个RANGE对象 '将开始到结束字符位置赋值给myRange对象
- ' End With
- ' Next
- If InStr(1, MyRange, "√") > 0 Then
- N = InStr(1, MyRange, "√") '对勾:ALT+小键盘41420 打叉:ALT+小键盘41409
- Str = Mid(MyRange, N, 4)
- arr(n0, I) = Str
- End If
- Sheets(2).Cells(读行号, 1) = Str0 ' MyRange
- Next
- .Close False '关闭word文档,不保存。
- Set Doc = Nothing
- End With
- FN = Dir() ''下一个WORD的处理
- Loop
- .DisplayAlerts = True '''显示警报
- .Visible = True '''可视
- .Quit '''退出WORD进程
- End With
- Application.ScreenUpdating = True
- End Sub
复制代码
|
|