对象变量或未设置块变量

时间:2015-07-24 09:59:19

标签: vba ms-access

我有这个VBA代码,用于检查文件夹中的所有文件。如果文件不是pdf,则打开它。然后它循环文本中的每个段落,如果段落包含一些固定符号,它会将段落文本和值导出到Access DB。

代码80%的时间都有效,但有时候,我收到错误说:

  

对象变量或With block变量未设置"错误。

这是我的代码:

Sub retrieve()
Dim wfile, strinsert, fileorigine, parastringa, parastring, myOutput, price, quantity, filename As String
Dim Myrange As Range
Dim objPara
Dim accessdb As Access.Application
Dim para As Paragraph
Dim date_created, date_last_modified As Date
Dim currfile As Document
Application.DisplayAlerts = False
Set accessdb = CreateObject("Access.Application")
With accessdb
.OpenCurrentDatabase ("C:\Users\myuser\Desktop\macro\DataBaseFr.accdb")
.Visible = True
.DoCmd.SetWarnings False


For i = 0 To 100 'This will have to be updated with a counter

If i = 0 Then ' For the first call to dir
    wfile = Dir("C:\Users\myuser\Desktop\macro\doc\")
Else
    wfile = Dir 'all calls to Dir after the first
End If

'if .pdf then skip
If Right(wfile, Len(wfile) - InStrRev(wfile, ".")) = "pdf" Then 
    Else
    Set currfile = Documents.Open("C:\Users\myuser\Desktop\macro\doc\" & wfile)
    'for each paragraph
    fileorigine = Replace(Left(currfile.Name, (InStrRev(currfile.Name, ".", -1, vbTextCompare) - 1)), "'", "")
    date_last_modified = Format(FileDateTime(currfile.FullName), "d/m/yy h:n ampm")
    date_created = currfile.BuiltInDocumentProperties("Creation Date")

    For Each para In currfile.Paragraphs
        Set objPara = para.Range
        objPara.Find.Text = "€"
        objPara.Find.Execute 
        'if the line contains €
        If objPara.Find.Found Then 
            If Left(para.Range.Text, 7) = "Sum" Then 
            'if the line contains € AND Sum
            parastringa = para.Previous.Range.Text
            parastring = parastringa
            quantity = Trim(Left(para.Range.Text, (InStrRev(para.Range.Text, "€") - 1)))
            price = Trim(Right(para.Range.Text, ((Len(para.Range.Text) - InStrRev(para.Range.Text, "€")))))
            Else 'if it is not a sum line
                parastringa = para.Range.Text
                parastring = Trim(Left(parastringa, (InStrRev(parastringa, "€") - 1)))
                price = Trim(Right(parastringa, ((Len(parastringa) - InStrRev(parastringa, "€")))))
                quantity = ""
            End If
                strinsert = " INSERT INTO Base " & "([origin], [Description],[date_created],[Datelast],[quantity], [price]) VALUES ('" & fileorigine & "', '" & parastring & "', '" & date_created & "' , '" & date_last_modified & "', '" & quantity & "' , '" & price & "');"
                CurrentDb.Execute strinsert, dbFailOnError
        Else
        End If
    Next para

    currfile.Close SaveChanges:=False
End If
i = i + 1
Next
CurrentDb.Close
End With
Application.DisplayAlerts = True

End Sub`

可能会发生什么以及如何解决?

1 个答案:

答案 0 :(得分:1)

从其他应用程序引用对象,属性等时要小心,在这种情况下,从Word VBA中访问“CurrentDb”对象。

由于您正在使用With accessdb块,因此只需更改

即可
CurrentDb.Execute strinsert, dbFailOnError

.CurrentDb.Execute strinsert, dbFailOnError

CurrentDb.Close

.CurrentDb.Close

一旦你到达它,也将最后的.DisplayAlerts = True拉到With块。