如何解决定位word文件时的错误?

时间:2017-07-17 11:21:28

标签: vba excel-vba word-vba excel

我正在尝试打开word文件的位置,它会出现以下错误。调试时我在以下位置发现错误。我是VBA的新手可以有人帮助我

enter image description here

我的代码是

sub findBearingCopyFromExcel()
    Dim i As Integer
    Dim aCell As Range, rng As Range
    Dim SearchString As String
Set rng = Range("A750:A1790")
SearchString = "(248_R), 38,7 %"

For Each aCell In rng
    If InStr(1, aCell.Value, SearchString, vbTextCompare) Then
        ActiveSheet.Range(Cells(aCell.row + 4, 1), Cells(aCell.row + 9, 6)).Copy
Exit Sub
        Else: End If
    Next aCell
End Sub
Sub bearingDataFromExcelToWord()
Dim wrdApp As Word.Application
Dim wdoc As Word.Document
Set wdoc = wrdApp.Documents.Open("E:\ShareDrive_Ruehl\full-flexible-MBS-models_report\example-report\FullFlexibleGearbox - Copy.docx")

wrdApp.Visible = True
With wdoc
.Application.Selection.Find.Text = "(248_R), 38,7 %"
.Application.Selection.Find.Execute


.Application.Selection.MoveDown Unit:=wdLine, Count:=1
    .Application.Selection.EndKey Unit:=wdLine
    .Application.Selection.EndKey Unit:=wdLine
    .Application.Selection.MoveRight Unit:=wdCharacter, Count:=1
    .Application.Selection.EndKey Unit:=wdLine
    .Application.Selection.MoveDown Unit:=wdLine, Count:=6, Extend:=wdExtend
    .Application.Selection.MoveLeft Unit:=wdCharacter, Count:=5, Extend:=wdExtend
    .Application.Selection.Paste
    End With
End Sub

3 个答案:

答案 0 :(得分:0)

wrdApp未创建添加new到其定义或稍后将其设置为新的,但在使用之前。

答案 1 :(得分:0)

您需要创建wrdApp

的实例
Dim wrdApp As Word.Application
Set wrdApp = New Word.Application
    wrdApp.Visible = True

Dim wdoc as Word.Document
Set wdoc = wrdApp.Documents.Open(...)

答案 2 :(得分:0)

猜测它正在打开文档 - 你确定你有正确的路径和文件名吗? 移动

 wrdApp.Visible = True

到“设置Wdoc ...”行之前,检查文件是否已打开