是否可以通过 VBA 检查共享点站点上是否存在文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13493756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to check via VBA if file exist on a sharepoint site?
提问by Kuba
I'm trying to write an Excel (2010) macro that at some point would have to confirm existence of a certain file (doc/pdf) on a corporate sharepoint site. The file is accessible through Internet Explorer (all rights are granted to the user). I have a direct link to that file. I don't need to open it, just check if it's there.
我正在尝试编写一个 Excel (2010) 宏,该宏在某些时候必须确认公司共享站点上某个文件 (doc/pdf) 的存在。该文件可通过 Internet Explorer 访问(授予用户所有权限)。我有该文件的直接链接。我不需要打开它,只需检查它是否在那里。
If this was a local file, I'd use Dir() to check if the file exists. However, this won't work with URIs.
如果这是一个本地文件,我会使用 Dir() 来检查文件是否存在。但是,这不适用于 URI。
I tried a method based on GET via objHttp but the only response I recieve is a wepage stating that "I am not authorized to view this page" [in tag].
我尝试了一种基于通过 objHttp 获取的方法,但我收到的唯一响应是一个网页,声明“我无权查看此页面”[在标签中]。
Is this doable in any way?
这以任何方式可行吗?
采纳答案by Kevin Pope
Give this a shot:
试一试:
Function checkFile(URLStr As String) As Boolean
Dim oHttpRequest As Object
Set oHttpRequest = New MSXML2.XMLHTTP60
With oHttpRequest
.Open "GET", URLStr, False, [Username], [Password]
.setRequestHeader "Cache-Control", "no-cache"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send
End With
If oHttpRequest.Status = 200 Then
checkFile = True
Else
checkFile = False
End If
End Function
URLStr
should be something like "http://sharepoint/site/user.xlsx". Enter your Username/Password in the .Open
line to pass them to the site, and this should work for any URI (I was testing it against .xlsx files for example). I should point out that on my internal SharePoint sites, I don't need to pass the UN/PW in order to run this function, so if that ends up being the case for you, just remove those parameters from the .Open
call. Also, all the header stuff probably isn't necessary, but I always have them in my requests so I left them in.
URLStr
应该类似于“http://sharepoint/site/user.xlsx”。.Open
在行中输入您的用户名/密码以将它们传递到站点,这应该适用于任何 URI(例如,我正在针对 .xlsx 文件对其进行测试)。我应该指出,在我的内部 SharePoint 网站上,我不需要传递 UN/PW 来运行此功能,因此如果您最终遇到这种情况,只需从.Open
调用中删除这些参数即可。此外,所有的标题内容可能都不是必需的,但我总是在我的请求中包含它们,所以我把它们留在了。
回答by Sigar Dave
I Dont know may this help you or not? But I am sharing my thoughts to you. You can use http web request for that. as given below example:
我不知道这对你有帮助吗?但我想和你分享我的想法。您可以为此使用 http Web 请求。如下例所示:
public void CheckWebFileExist()
{
try
{
WebClient client = new WebClient();
client.Credentials = CredentialCache.DefaultCredentials;
// Create a request for the URL.
WebRequest request = WebRequest.Create("myAddress");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//check response status
if (string.Compare(response.StatusDescription, "OK", true) == 0)
{
//URL exists so that means file exists
}
else
{
//URL does not exist so that means file does not exist
}
}
catch (Exception error)
{
//error catching
}
}
Let me know if this help you or not?
让我知道这是否对您有帮助?