且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有没有办法通过使用VBA的301重定向获取返回的URL?

更新时间:2022-10-19 21:50:13

这是在另一篇文章中回复到Reddit的Excel论坛。我需要的答案是.Option(1).Send(如果有人需要)。


I have a long list of our company's product numbers, and later in the code these numbers have to be converted to HTML and then linked to the product's corresponding page. The links to the pages have numerous 301 redirects to get from something messy / old / mistyped / etc. to the new SEO friendly URLs.

For example, something like this:

site.com/12345

Will get redirected to this page:

site.com/product-name-12345

I do not have a list to compare the product names against, as they are created and destroyed daily, and the site creates the URL on the fly (rather than drawing from some kind of database or something).

For a lot of complicated reasons, I only have access to Excel to complete this task (hooray!). I've written (bad) code to open Internet Explorer, go to the short URL, and then copy the new address into whatever was needed. This works, but it's incredibly slow (as you might expect). I'm wondering if there is a solution that doesn't rely on waiting for IE to open, however runs exclusively in VBA?

I have found a lot of ways to see if the redirect takes place, and I have found a lot of ways to get the headers, however I have yet to find a way to get the final URL that occurs. These mostly focus on WinHTTP.WinHTTPRequest.5.1, however it seems as though there isn't any kind of WinHTTPResponse to call (like HttpResponse in .NET) supported by Excel.

Any help would be greatly appreciated, thanks!

EDIT: This was answered on another post to Reddit's Excel forum. The answer I needed was .Option(1) after the .Send (if anyone else needs it).

Discussion can be read here

This was answered on another post to Reddit's Excel forum. The answer I needed was .Option(1) after the .Send (if anyone else needs it).