运行时错误70权限被拒绝Excel VBA

新世界

我创建了一些代码,这些代码会将电子邮件移动到文件夹,添加唯一的ID,放入电子表格中,并且不会覆盖重复项。

当我做到这一点时,它起作用了,现在它带有运行时错误70权限被拒绝。一直在查看代码,无法弄清这在哪里或为什么发生。

你们能看到我想念的东西吗?

Option Explicit
Const fPath As String = "C:\Users\Emails" 'The path to save the messages

Sub Download_Outlook_Mail_To_Excel()
Dim olApp As Object
Dim olFolder As Object
Dim olNS As Object
Dim xlBook As Workbook
Dim xlSheet As Worksheet
Dim NextRow As Long
Dim i As Long
Dim olItem As Object
    Set xlBook = Workbooks.Add
    Set xlSheet = xlBook.Sheets(1)
    On Error Resume Next
    Set olApp = GetObject(, "Outlook.Application")
    If Err <> 0 Then
        Set olApp = CreateObject("Outlook.Application")
    End If
    On Error GoTo 0
    With xlSheet
        .Cells(1, 1) = "Sender"
        .Cells(1, 2) = "Subject"
        .Cells(1, 3) = "Date"
        '.Cells(1, 4) = "Size"
        .Cells(1, 5) = "EmailID"
        .Cells(1, 6) = "Body"
        CreateFolders fPath
        Set olNS = olApp.GetNamespace("MAPI")
        Set olFolder = olNS.PickFolder
        For Each olItem In olFolder.Items
            NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            If olItem.Class = 43 Then
                .Cells(NextRow, 1) = olItem.Sender
                .Cells(NextRow, 2) = olItem.Subject
                .Cells(NextRow, 3) = olItem.SentOn
                '.Cells(NextRow, 4) =
                .Cells(NextRow, 5) = SaveMessage(olItem)
                .Cells(NextRow, 6) = olItem.Body
            End If
        Next olItem
    End With
     MsgBox "Outlook Mails Extracted to Excel"
lbl_Exit:
    Set olApp = Nothing
    Set olFolder = Nothing
    Set olItem = Nothing
    Set xlBook = Nothing
    Set xlSheet = Nothing
    Exit Sub
End Sub

Function SaveMessage(olItem As Object) As String
Dim Fname As String
    Fname = Format(olItem.ReceivedTime, "yyyymmdd") & Chr(32) & _
            Format(olItem.ReceivedTime, "HH.MM") & Chr(32) & olItem.SenderName & " - " & olItem.Subject
    Fname = Replace(Fname, Chr(58) & Chr(41), "")
    Fname = Replace(Fname, Chr(58) & Chr(40), "")
    Fname = Replace(Fname, Chr(34), "-")
    Fname = Replace(Fname, Chr(42), "-")
    Fname = Replace(Fname, Chr(47), "-")
    Fname = Replace(Fname, Chr(58), "-")
    Fname = Replace(Fname, Chr(60), "-")
    Fname = Replace(Fname, Chr(62), "-")
    Fname = Replace(Fname, Chr(63), "-")
    Fname = Replace(Fname, Chr(124), "-")
    SaveMessage = SaveUnique(olItem, fPath, Fname)
lbl_Exit:
    Exit Function
End Function

Private Function SaveUnique(oItem As Object, _
                            strPath As String, _
                            strFileName As String) As String
Dim lngF As Long
Dim lngName As Long
    lngF = 1
    lngName = Len(strFileName)
    Do While FileExists(strPath & strFileName & ".msg") = True
        strFileName = Left(strFileName, lngName) & "(" & lngF & ")"
        lngF = lngF + 1
    Loop
    oItem.SaveAs strPath & strFileName & ".msg"
    SaveUnique = strPath & strFileName & ".msg"
lbl_Exit:
    Exit Function
End Function

Private Sub CreateFolders(strPath As String)
Dim strTempPath As String
Dim iPath As Long
Dim vPath As Variant
    vPath = Split(strPath, "\")
    strPath = vPath(0) & "\"
    For iPath = 1 To UBound(vPath)
        strPath = strPath & vPath(iPath) & "\"
        If Not FolderExists(strPath) Then MkDir strPath
    Next iPath
End Sub

Private Function FolderExists(ByVal PathName As String) As Boolean
   Dim nAttr As Long
   On Error GoTo NoFolder
   nAttr = GetAttr(PathName)
   If (nAttr And vbDirectory) = vbDirectory Then
      FolderExists = True
   End If
NoFolder:
End Function

Private Function FileExists(filespec) As Boolean
Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(filespec) Then
        FileExists = True
    Else
        FileExists = False
    End If
lbl_Exit:
    Exit Function
End Function
新世界

该问题现在已解决。这是由于电子邮件保存的文件夹具有特殊权限。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Excel VBA运行时错误“ 70”:权限被拒绝

来自分类Dev

VBA Excel SSO到SAP /运行时错误70“访问被拒绝”

来自分类Dev

Excel VBA运行时错误1004

来自分类Dev

Excel VBA运行时错误1004

来自分类Dev

运行时错误13 VBA Excel

来自分类Dev

VBA Excel 错误运行时错误“53”

来自分类Dev

读取图表标签值运行时错误(Excel VBA)

来自分类Dev

Excel VBA,Http.ResponseText运行时错误91

来自分类Dev

Excel VBA运行时错误'-2147319767(80028029)'

来自分类Dev

运行时错误462使用Excel访问VBA

来自分类Dev

VBA Excel做While循环运行时错误

来自分类Dev

VBA Excel-Access 3251运行时错误

来自分类Dev

Excel VBA Vlookup运行时错误1004

来自分类Dev

运行时错误13 VBA Excel 2007

来自分类Dev

Excel VBA-运行时错误1004

来自分类Dev

Excel VBA,Http.ResponseText运行时错误91

来自分类Dev

VBA Excel做While循环运行时错误

来自分类Dev

运行时错误91- Excel VBA

来自分类Dev

Excel VBA:运行时错误424,需要对象

来自分类Dev

Excel VBA查找函数获取运行时错误1004

来自分类Dev

Excel VBA运行时错误类型不匹配13

来自分类Dev

Excel VBA:.find函数返回运行时错误91

来自分类Dev

运行时错误“424” - Excel 中的 VBA

来自分类Dev

带有 .quit 的 vba excel 运行时错误 438

来自分类Dev

需要运行时错误“424”对象:Excel VBA

来自分类Dev

Excel VBA:运行时错误(对象“范围”的方法“值”失败),但仅在连续运行时

来自分类Dev

Excel VBA:运行时错误(对象“范围”的方法“值”失败),但仅在连续运行时

来自分类Dev

VBA组合框附加项并出现运行时错误70

来自分类Dev

Excel VBA减少运行时