首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在尝试使用VBA访问OneDrive文件夹中的文件时,我遇到了OneDrive的问题。

在尝试使用VBA访问OneDrive文件夹中的文件时,我遇到了OneDrive的问题。
EN

Stack Overflow用户
提问于 2020-04-06 12:13:15
回答 1查看 4.7K关注 0票数 0

我有一个OneDrive Excel工作簿,我已经打开在我的本地电脑。我有一个宏,它产生一个发票,并将它以pdf格式保存到我的OneDrive上的一个子目录。到目前为止没问题。但是,当我使用另一个宏附加先前保存的文件时,宏将找不到该文件。我已经建立了子目录存在于

代码语言:javascript
运行
复制
Function URLExists(url As String) As Boolean
    Dim Request As Object
    Dim ff As Integer
    Dim rc As Variant

    On Error GoTo EndNow
    Set Request = CreateObject("WinHttp.WinHttpRequest.5.1")

    With Request
      .Open "GET", url, False
      .Send
      rc = .StatusText
    End With
    Set Request = Nothing
    If rc = "OK" Then URLExists = True

    Exit Function
EndNow:
End Function

但使用

代码语言:javascript
运行
复制
Function FileExists(filename)
    On Error Resume Next
    FileExists = (Dir(filename) <> "")
End Function

给我一个文件找不到错误

这是我正在使用的完整的“SendEmail”潜艇。

代码语言:javascript
运行
复制
Sub SendMail()
    Const cdoSendUsingPickup = 1                        '*** Send message using the local SMTP service pickup directory.
    Const cdoSendUsingPort = 2                          '*** Send the message using the network (SMTP over the network).
    Const cdoAnonymous = 0                              '*** Do not authenticate
    Const cdoBasic = 1                                  '*** Basic (clear-text) authentication
    Const cdoNTLM = 2                                   '*** NTLM
    mySubject = Trim(Cells(16, 3))
    myTestEmail = Trim(Cells(12, 12))
    myAttachment1 = myFileName & ".pdf"
    myAttachment1 = "Testing.xlsx"
    '*******************************************
    '*** This bit checks the pdf file exists ***
    '*******************************************
    If Not FileExists(myProgramPath & myAttachment1) Then '--- This gives TRUE i.e. file does not exist
        MsgBox "Attachment file does not exist"
        Exit Sub
    End If

    '--- This is the format of the url "https://d.docs.live.net/xxxxx/xxxxxxxxxxx/xxxxx/"
    '--- I've tried changing the direction of the "/" with myProgramPath = Replace(myProgramPath, "\", "/")
    '--- but makes no difference which way they face

    If Not URLExists(myProgramPath) Then '--- This is FALSE i.e. folder does exist
        MsgBox "Folder does not exist"
        Exit Sub
    End If
    Set objMessage = CreateObject("CDO.Message")
    Set objConf = CreateObject("CDO.Configuration")
    objMessage.AddAttachment myProgramPath & myAttachment1 '--- This is where the error occurs
    objMessage.CreateMHTMLBody "file://" & myProgramPath & "StatementBody.html" '*** This is the html file that creates the body of the email
    myTo = Trim(Cells(22, 5))
    myFrom = Chr(34) & "xxxxxxxxxxxxxxxx" & Chr(34) & "<" & "xxxxxxxxxxxxxx@xxxxxxxxxxxx" & ">"
    myFrom = "xxxxxxxxxxxxxxxxxxxx <" & "xxxxxxxxxxxxxxx@xxxxxxxxxxxx" & ">"
    myBcc = Trim(Sheets("Lookups & Validation").Cells(13, 9))
    objMessage.Subject = mySubject
    objMessage.From = myFrom
    objMessage.To = myTo
    objMessage.bcc = myBcc
    '*** This section provides the configuration information for the remote SMTP server.
    objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.ionos.co.uk"                     '*** Name or IP of Remote SMTP Server
    objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic                         '*** Type of authentication, NONE, Basic (Base64 encoded), NTLM
    objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "xxxxx@xxxxxxxxxxxxxxx"              '*** Your UserID on the SMTP server
    objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Dulceetdec0rumest%"                 '*** Your password on the SMTP server
    objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 587                                      '*** Server port (typically 25)
    objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False                                       '*** Use SSL for the connection (False or True)
    objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60                               '*** Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
    objMessage.Configuration.Fields.Update
    '*** End remote SMTP server configuration section
    objMessage.Send
    Set objMessage = Nothing
    Set objConf = Nothing
End Sub
EN

Stack Overflow用户

发布于 2020-06-29 11:34:24

可能的解决办法是:

启动OneDrive

  • create
  1. --为您的Excel工作簿创建一个单独的文件夹,并使用宏
  2. 右键单击该文件夹,然后选择“始终保持在此设备上”

这样,您的vba代码应该引用本地文件,而不是引用它的云位置

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61059590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档