返回

excel-VBA从宏打开工作簿失败”(路径&文件名)找不到。检查拼写……”

发布时间:2022-04-24 23:42:42 448
# 数据库# 服务器# 数据# 服务器# 信息

我有以下代码,只需从主工作簿中打开一个工作簿,就可以粘贴一些信息,稍后将从数据库中检索这些信息。我收到了标题中的错误每一次我试着打开工作簿。我以前使用过类似的代码,完全没有问题。文件保存在远离桌面的服务器上,但每次路径都是正确的。我检查了一遍又一遍。罪魁祸首可能是什么?

Sub copyDealerDataToWorkbook(ByVal targetID As Integer)

    Dim mainWB      As Workbook    
    Dim directory   As String
    Dim fn          As String
    Dim aFile       As Excel.Application

    Set aFile = CreateObject("Excel.Application")

    Set mainWB = ActiveWorkbook
    directory = ActiveWorkbook.Path

    'append a "\"
    If InStrRev(directory, "\") < Len(directory) Then
        directory = directory & "\"
    End If

    fn = targetID & ".xls"

    aFile.Application.Visible = True
    aFile.Workbooks.Open directory & fn 'ERROR ON THIS LINE
    aFile.Parent.Windows(1).Visible = True
End Sub

我还尝试了以下代码。。。

Sub copyDealerDataToWorkbook(ByVal targetID As Integer)
    Dim foreignWB   As Workbook
    Dim mainWB      As Workbook
    Dim directory   As String
    Dim fn          As String

    Set mainWB = ActiveWorkbook
    directory = ActiveWorkbook.Path

    'append a "\"
    If InStrRev(directory, "\") < Len(directory) Then
        directory = directory & "\"
    End If

    fn = targetID & ".xls"

    Set foreignWB = Workbooks.Open(fileName:=directory & fn) 'ERROR ON THIS LINE
End Sub
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(2)
按点赞数排序
用户头像