简体   繁体   English

VBA中的QueryTables.Add会添加相同的文件,而不管文件路径如何

[英]QueryTables.Add in VBA Adds Same File Regardless Of Filepath

Thanks in advance for the help! 先谢谢您的帮助!

In VBA, I'm using the following code to pull in data from an IQY file stored in Sharepoint (Workbooks.Open was not a great option, due to the "Import Data" prompt): 在VBA中,我使用以下代码从存储在Sharepoint中的IQY文件中提取数据(由于“导入数据”提示,Workbooks.Open并不是一个很好的选择):

Function getIQYWorkbook(ByVal filename As String) As Workbook
Dim wb As Workbook
Set wb = Workbooks.Add
Dim IQYFile As String
IQYFile = ThisWorkbook.Path & "/" & filename

With wb.Sheets("Sheet1").QueryTables.Add(Connection:= _
   "FINDER;" & IQYFile, Destination:=Range("A1"))

    .BackgroundQuery = True
    .TablesOnlyFromHTML = True
    .Refresh BackgroundQuery:=False
    .SaveData = True
End With

Set getIQYWorkbook = wb

End Function

What is so bizzare, though, is that the data that gets pulled in does not correspond to the actual file I pass in. No matter what I pass in, one particular IQY file in the Sharepoint folder is downloaded. 但是,如此奇怪的是,导入的数据与我传入的实际文件对应。无论传入什么,都将下载Sharepoint文件夹中的一个特定IQY文件。 Which makes it frustrating, as this function is called in a loop that passes in different filenames. 令人沮丧的是,此函数在传递不同文件名的循环中被调用。

For instance, when I use the following code (notice me setting the filepath to the word "dogs", which is ridiculous), that same sharepoint file is still pulled in. 例如,当我使用以下代码(请注意,将文件路径设置为单词“ dogs”,这很荒谬)时,该共享点文件仍被拉入。

Function getIQYWorkbook(ByVal filename As String) As Workbook
Dim wb As Workbook
Set wb = Workbooks.Add
Dim IQYFile As String
IQYFile = "dogs"

With wb.Sheets("Sheet1").QueryTables.Add(Connection:= _
   "FINDER;" & IQYFile, Destination:=Range("A1"))

    .BackgroundQuery = True
    .TablesOnlyFromHTML = True
    .Refresh BackgroundQuery:=False
    .SaveData = True
End With

Set getIQYWorkbook = wb

End Function

This has persisted across reboots/closing Excel/etc.... it's as if QueryTables.Add is "stuck" in pulling the same file regardless of which filepath I pass in. Any ideas of what's going on? 这在重新启动/关闭Excel /等过程中一直存在。...好像QueryTables.Add被“卡住”在拉同一个文件中,而不管我传递的是哪个文件路径。这是怎么回事?

Thank you so much! 非常感谢!

I realize this questions is pretty old by now but I've not seen a solution to this problem anywhere. 我知道这个问题现在已经很老了,但是我在任何地方都没有找到解决这个问题的方法。 I only seen 2 reference total about this problem. 我只看到2条关于该问题的参考书。

I had the same problem occur. 我曾经发生过同样的问题。 2 other examples that I have work fine and I test a third that worked fine too. 我能正常工作的另外两个示例,我也测试了另一个能正常工作的示例。

My conclusion is the SharePoint list is generating a bad or corrupt .IQY file. 我的结论是SharePoint列表正在生成错误或损坏的.IQY文件。 I've tried various Exports form my SharePoint list and they all fail. 我尝试了各种从SharePoint列表中导出的操作,但都失败了。 So I don't have a solution for it! 因此,我没有解决方案! My workaround plan to is Power Query in O365 to access the data from an Excel file. 我的解决方法是在O365中使用Power Query来访问Excel文件中的数据。 This avoids the popups the .IQY file has. 这样可以避免.IQY文件具有的弹出窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM