VBA MSXML2 Open member POST - excel

In this code snipet example...
Dim pullSite As String
Dim pullXMLHTTP As MSXML2.XMLHTTP
Set pullXMLHTTP = New MSXML2.XMLHTTP
pullSite = "http://www.ThisIsASite.com/Documents/whatever........xml"
pullXMLHTTP.Open "POST", strXMLSite, False
...I am confronted with the word Open in the code which I reckon is a method since it's not being equaled to anything (in the OB screen at the bottom left once picked it says it's a Sub, but I rest my case on OB naming methods as Sub and Function- I guess it wants to say this is a class browser so we state the method as Sub or Function just like when we "custom" make them in a class module but I am not sure of that). If I go to the Object Browser in the MSXML2 Library I will find in XMLHTTP the Open listed as a Member.
Well if I hit F1 VBA's help has 3 choices. First choice (VBA Library) one tells me it is the Open Statement which clearly isn't.
The Second choice (Excel Library) has an RecentFile.Open Method without parameters so it doesn't buy that either.
The Third choice (Office Library) gives me the Developer Reference main menu.
So F1 VBA's Auto help has nothing for the MSXML2 Library. However in the OB's lower left bottom if I choose from the MSXML2.XMLHTTP Library the Open Member it states
Sub open(bstrMethod As String, bstrUrl As String, [varAsync], [bstrUser], [bstrPassword])
Member of MSXML2.XMLHTTP
Open HTTP connection
So my question is twofold
Is there a solid help which documents the members of the MSXML2 Library in detail/or a way to add that on the VBA' s Autohelp? (BTW Microsoft's Development Center on MSXML MSXLM is vast and to vague. I also believe that it points to experienced innuendos...
Why do I see in relevant examples where the MSXML2.HTTP Open member is used, the first String parameter always populated with the word "POST" in Cap letters? It's mind boggling to me-can't they use another String value?
After #codeape 's submitted answer:
Dear codeape so according to you answer, we have the
open Method (IXMLHTTPRequest)
and the parameters in an existing VBA example (which MS lacks) all-together form the IXMLHTTPRequest so:
Open methods Paramenters --> IXMLHTTPRequest
Please confirm if this is true so that I will edit this post in a more user-friendly all confirmed instructions for others to use.
Also I can't help my curiosity why do we have an I in the IXMLHTTPRequest naming? Just wondering...
In regards to the POST Question thanks to your answer I googled HTTP Method and found this neat link in W3Schools which to quite an extent explains to me. Really why not use GET? It looks more fast-forward... instead of submitting data to be processed by a specified source (with POST) just request the data from the specified source in one stroke it looks more swift and simple...
Oooups I almost forgot in the open Method link you gave me as well as in the bottom of OB we see bstrMethod As String, bstrUrl As String etc. Could you tell me what that bstr means? - Is the BSTR means Basic String or Binary String? please confirm

The documentation for XMLHTTP is here. Note that the documentation page is for the interface IXMLHTTPRequest. MSXML.XMLHTTP implements this interface.
The Open method's first argument is the HTTP method, i.e. GET or POST (or PUT, DELETE, etc.).
Sadly it looks as if MS has forgotten about VBA when it comes to documenting various COM libraries. Most/all documentation is for C++ and javascript.
Comments on other things mentioned in the question:
The "I" in IXMLHTTPRequest is a naming convention in COM used for naming interfaces.
Regarding HTTP methods (GET, POST etc.):
The server-side code decides how to interpret and respond to the HTTP requests.
When you type a URL into you browser's URL bar, the browser sends a GET request to the server. So if you want to fetch a web page using MSXML.XMLHTTP you do: obj.Open "GET", "http://www.google.com"
When you submit a HTML form from your browser, the browser can either send a GET or a POST request depending on method attribute of the <form> tag. <form method="GET"> (the default) or <form method="POST">.
Javascript on the page can send AJAX requests using XMLHTTPRequest with any HTTP method.
Regarding argument names bstrMethod etc.: This is hungarian notation, where an argument's expected type is reflected in the argument's name to make things easier for the developer. The bstr means that this argument is a COM "basic string". A basic string is what COM typically uses for unicode strings. In VBA, you can use normal VBA strings where a method expects a bstr.

Related

Pulling Data from HTML Tables

So I understand how to pull data from a single weblink looking at tables. I cannot find not 1 tutorial anywhere on the web about how to do so getting it from Div elements and no one talks about it at all. Can someone please give me an example or something? Either Excel or Google Spreadsheets.
Im trying to teach myself doing so but using this website https://newworldstatus.com/regions/us-east for a small project I want to do.
Thank you in advance.
This is not a comprehensive answer, just intended to show you how some very basic concepts work. Second, an answer for Sheets, but let me preface all of this by saying that while your test URL seems simple enough, you will not be able to do any of this for that specific URL. They are either actively trying to stop scraping or they just have it set up in a way that makes it difficult to scrape by accident. If you directly make a web request to that URL, you will get back the JS code that actually handles the data load-in and not the data itself, so any kind of parsing you try to do will fail because what you see in the page isn't what is actually coming back on the initial page request. All the html that will be in the page is enough to show this:
You would need to either try to read through the code and figure out what they're doing, or do some tinkering in the javascript console, and probably some fairly high-level tinkering. So for a first project, or just to learn some basics, I think I would pick a different test case.
First, in VBA. It's both complicated and not all that complicated at the same time. If you know how web technologies work non-language specifically, then it all works pretty much the same way in VBA. First, you'll need to make a web request. You can do that with the winHTTP library or the msXML library. I usually use winHTTP, but unless what you're doing is complex, either one is fine.
WEB REQUEST:
You'll need to instantiate a request object. You can do that by either adding a reference to the library (tools->references-> and pick the library out of the list) or you can use late binding. I prefer to add the reference, because you get intellisense that way. Here are both:
Dim req As New WinHttp.WinHttpRequest
or
Set req = CreateObject("WinHttp.WinHttpRequest.5.1")
Then you open the request. I'm going to assume this is a straight GET. POST requests get a little more complicated:
req.Open "GET", url, TRUE
If you have the reference added and created the req with Dim, then you'll get the intellisense and as you type that the arguments will pop up and you can use that to refer to the documentation if you have questions. TRUE here is to send it asynchronously, which I would do. If you don't, it will block up the interface. This the Open method, which you can find in the documentation.
https://learn.microsoft.com/en-us/windows/win32/winhttp/iwinhttprequest-interface
Then use
req.send
req.WaitForResponse
source = req.responseText
to send the request. WaitForResponse is needed only if you send the request asynchronously. The last part is to get the responseText into a variable.
PARSING:
Then you'll need to do some stuff with the MSHTML library, so add a reference to that. You can also late bind, but I would not, because it will be very helpful to you to have the prompts in intellisense.
First, set up a document
https://learn.microsoft.com/en-us/dotnet/api/mshtml.htmldocument?view=powershellsdk-1.1.0
and write the source you just fetched to it:
Dim doc as new MSHTML.HTMLdocument
doc.write source
Now you have a document object you can manipulate. The trick is to get a reference to the element you want. There are two methods that will return an element:
getElementById
querySelector
If you are lucky, the element you are looking for will have a unique ID and you can just get it. If not so lucky, you can use a selector that identifies it uniquely. In either case, you will set up an IHTMLElement to return to:
Dim el as MSHTML.IHTMLElement
set el = doc.getElementById("uniqueID") 'whatever the unique ID is
Once you have that, you can use the methods and properties of the element to return information about it:
https://learn.microsoft.com/en-us/dotnet/api/mshtml.ihtmlelement?view=powershellsdk-1.1.0
There are more specific interfaces, like
https://developer.mozilla.org/en-US/docs/Web/API/HTMLAnchorElement
You can use the generic IHTMLElement, but sometimes there are advantages to using a specific element type, for instance, the properties that are available to it.
Sometimes you will have to set up an IHTMLElementCollection:
https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa703928(v=vs.85)
and iterate it to find the specific element you are looking for. There are four methods that return collections:
getElementsByName
getElementsByTagName
getElementsByClassName
querySelectorAll
getElementsByClassName is sometimes problematic, so forewarned is forearmed.
If you need to do that, set up and IHTMLElementCollection and return the list to that:
dim els as MSHTML.IHTMLElementCollection
set els = doc.getElementsByTagName("tagName") 'for instance a for anchors, div for divs
That is about it. There is obviously more to it, but a comprehensive answer would be very long. This is mostly intended to point you in the right direction and give you more stuff to google.
I will say that you should test out some of these methods in the browser first. They exist in many languages, and all major browsers have developer tools. For Chrome, for instance, press Ctrl+Shift+I to bring up the dev tools, and then in the console window type something like:
document.getElementById("uniqueID")
and you should get the node. or
document.getElementsByClassName(".test") 'where test is the name of the class
document.querySelectorAll("div") ' where you pass a valid CSS selector
and you will get the node list.
It will be quicker to experiment there than to try to set it up and debug in VBA. once you have a good handle on how it works, try to transfer that knowledge to a VBA implementation.
Here is a basic overview of .querySelector to get you started on understanding how those work, although they can get very complicated. In fact, querySelector is my go to method for finding elements.
https://www.w3schools.com/jsref/met_document_queryselector.asp
Now, Google Sheets:
You don't really want to use IMPORTHTML, even though it seems counterintuitive. That function (AFAIK) only supports tables and lists, and it's index based, too, which means you give it a number n and it returns the nth table or list in the page. That means that if they ever change the layout, or the layouts are dynamic in any way, then you won't be able to rely and an index to accurately identify what you want. Also, as you noted people don't really use tables much anymore, and when they say list I'm pretty sure they mean on and elements, which is also not going to be that useful to you. Here's the docs:
https://support.google.com/docs/table/25273?hl=en&visit_id=637732757707317357-1855795725&rd=2
But you can use IMPORTXML. Even though it says XML, you can still use it to parse HTML (for reasons and with limitations that are out of scope for this answer). IMPORTXML takes a URL and an xpath selector. In this way it's similar to the document.querySelector and querySelectorAll methods. Here is some information on xpath in tutorial from from w3schools.
https://www.w3schools.com/xml/xpath_intro.asp
And if you want to test selectors in Chrome you can use $x("selector") in the javascript console in the dev tools. I believe Firefox also supports this, but I am not sure if other browsers do. If not, you can use document.evaluate:
https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate
Even though you can't actually use this in sheets against the URL you've given, let's take a look at a couple of xpath selectors in that context. Hit Ctrl+Shift+I to bring up the dev tools (hopefully you are using Chrome), and then go to the elements tab. If you don't have the javascript console showing in the bottom pane, hit Esc. You should see something like this:
Use the arrow icon in the top left of the dev tools to search the elements, and just click on the first row in the table:
so that you can see the structure of the elements, and figure out how to parse out what you want from it. You'll notice that the cell that's highlighted is contained in a div with a role of "row" and an attribute of row-id. I think that's where I would start. So an xpath to that container would look something like this:
//div[#row-id=1]
where we are fetching all elements (//) that match div and have an attribute (#) of row-id = 1.
If you want to get the children of that container, you just add another level to the path
//div[#row-id=1]/div
where we want to get all children (/) that are divs.
And I notice that they all have a col-id attribute, so if you wanted to fetch the "set" information you'd just specify divs that have an attribute of col-id = 'set':
//div[#row-id=1]/div[#col-id='set']
and to get the text out of that:
//div[#row-id=1]/div[#col-id='set']/text()[1]
since it looks like the second node is the one that has the team name in it. Again, you can see how this WOULD work in the dev tools, but you won't actually be able to use this for your URL.
I'm not going to spend a lot of time here. As already stated, you won't be able to use this method on your specific URL. If you can figure out the actual URL that your URL wraps around, then perhaps. Also, since there's only one argument, the selector, then there's not much more to expound on. If you needed something more complex, like the ability to iterate over a set of matching nodes, you could probably do it in Scripts, but I would probably just switch to Excel if it started getting that complicated. The only exception would be if the data was JSON formatted, in which case Scripts will be able to handle that better than VBA, although I would probably switch to a different language entirely in that case.
Since your URL is probably not good for testing, I'm going to point you to this tutorial from Geckoboard, which has a few different examples from sites like Wikipedia and Pinterest.
https://www.geckoboard.com/blog/use-google-sheets-importxml-function-to-display-data/
So google around, experiment, and let me know if you need any help. And this was all off the top of my head, so let me know if any of this stuff throws errors so I can edit the answer.
Also, be aware that Excel is not always the right tool for dealing with this. Very often, while the page might have the elements you are looking for, they will be loaded in with JSON and both php and javascript can natively handle JSON objects, while VBA doesn't. If the data is JSON formatted, it is much easier to parse it out of that than trying to parse it out of the DOM structure (DOM = document object model, another thing to google). Also, in many cases, if the data is loaded in with AJAX, it won't be returned with your winHTTP call, because that doesn't execute any javascript that might be in the page.
Further, in many cases you will need to set headers or cookies in the winHTTP call to get the data (calls without the right setings might return an error or a redirect). That is also not addressed in my answer, although you can set headers and cookies in winHTTP. You would need to sniff the calls, either with Fiddler or similar or with the network tab in dev tools, to find out the right combination of information to pass with your request.

Python Win32Com First page Header/Footer

Some Context
I am trying to scrape information from a large number of .doc files, so I've written a python program to do the heavy lifting for me. Word has this nifty ability to make the header and footer of the first page different. This is generally useful, but I am running into a problem which I'm not finding a good solution for.
This is how I am accessing headers and footers:
import win32com
word_app = win32com.client.Distpatch('Word.Application')
doc = word_app.Documents.Open('path/to/my/word/file.docx')
first_footer = doc.Sections(1).Footers(1).Range.Text
print(first_footer)
There is a catch, though: the first page contains header/footers which are common throughout the document, but also some things which are unique to the first page. The code above does not capture this unique information: it only shows the header/footer information from the first page which is common throughout the document.
When the first page has a unique content in its header and footer, how do I access it using python's win32com?
After some digging, I have found an answer.
It turns out you need to use a constant called "wdHeaderFooterFirstPage" within the constants bit of the module to access the first page header and footer, like so:
doc.Sections(1).Headers(win32com.client.constants.wdHeaderFooterFirstPage).Range.Text
This returns a string which you can manipulate like normal. Documentation for win32com is hard to find, and translating it from VBA documentation is not as obvious as I would like it to be.

Office JS API for OneNote - Missing APIs?

Office JS API for OneNote... Love it, but I am missing some critical things. Can someone comment?
I got a paragraph, type is RichText. But, I could not find in the API the style of the rich text. In my case, I want to know if it is a Header 1,2,3... or Quote, etc.
Same-page linking: In OneNote desktop I can right click any text and copy link to that specific paragraph. Clicking that link later will take me directly to that paragraph. However, I did not find an API that can navigate directly to a paragraph, the only one I could find navigates to a page: navigateToPage(page: Page) and navigateToPageWithClientUrl(url: string)
If that even possible? Also, I noticed these links don't work at all in the web version of OneNote, but that's a different story I guess.
I am building (a free!) TOC add-in, you can put at the top of your page and will potentially show all headers with links to the header in the page. However, the lack of the above capabilities make it impossible for such a simple add-in to work (or, at least I thought it is a very basic and simple one...)
Any help will be greatly appreciated!!! Like I said, if I get these 2 issues resolved - the add-in will be available for free.
https://dev.office.com/reference/add-ins/onenote/paragraph?product=onenote
Sounds like a cool add-in!
You can use the getHtml method on richText to get the style. There is an example in this answer.
OneNote Add in: Getting HTML content
As for creating links to a specific paragraph, OneNote add ins do not expose the capability of doing that. You can add a request in our uservoice. The only supported capability is to navigate to a page.
https://onenote.uservoice.com/forums/245490-onenote-developer-apis
As for links that work in OneNote online, the "webUrl" property in a page will contain a link that works in OneNote online.
https://github.com/OfficeDev/office-js-docs/blob/master/reference/onenote/page.md
Thanks for feedback. We will update the documentations.
There is currently no way to scroll to any region in the page.

How to get data from custom properties in UITestPropertyProvider

According to the code sample on MSDN (http://msdn.microsoft.com/en-us/library/hh552522.aspx) any custom property data that you need to get out of your control should be in a semicolon delimited string in the AccessibleObject's Description property. This does not seem right to me at all. This seems like just a quick and dirty trick to get it working. What is the correct way to get the value of properties from custom controls? And if this actually is how you're supposed to do it, then how are you supposed to set those properties using the SetPropertyValue method? The example in the link above just throws a NotImplementedException in SetPropertyValue.
Since the IAccessible interface has only a limited number of properties the best solution is to cram any extra information into the Description property (that's what they do at the company I work at, and our developers don't work quick and dirty :) ). To modify the return value of this property you have to implement the Iaccessible interface on your control. Or, since you only want to modify the Description property you only need to modify that property and leave the rest to the proxy (I'm not sure how this works exactly but there are tutorials for it on MSDN).
The SetPropertyValue method in the UITestPropertyProvider is for the UItestControls. By overwriting it you can modify the way CUIT interacts with the control during playback. For example, if you overwrite SetPropertyValue for the Text property you can change the way CUIT types strings into the control.

Client id of elements changes in sharepoint page? Why? When?

Client id of every element from the sharepoint page changes sometimes.
Can anybody please tell me why and on which instance it changes???
jQuery is fantastic! It makes client-side development faster and
countless plug-ins are available for just about every need. Using
jQuery with Asp.NET Web-Forms gets aggravating when dealing with
nested server controls. ClientID’s get appended when using ASP.NET
Master Pages. Objects in JavaScript tend to look like this:
ctl00_m_g_aaf13d41_fc78_40be_81d5_2f40e534844f_txtName
The difficulty of the issue above is that, in order to get the element txtName, It’s necessary to know the full “path”. It’s quite
aggravating to refer to get the object using the method below:
document.getElementByID('ctl00_m_g_aaf13d41_fc78_40be_81d5_2f40e534844f_txtName');
This becomes a big problem when developing server controls or web parts that may be used in a typical ASP.NET application or SharePoint.
You cannot hard-code the path above if you don’t know the full path of
the control.
Fortunately, there are a few ways to get around this. There are three, in particular, I will mention. The first is the jQuery
equivalent to the standard JavaScript method:
document.getElementById("<%=txtName.ClinetID%>");");
This can be done in jQuery by using:
$("#'<%=txtName.ClinetID%>");");
The second jQuery method does not require server tags. This method searches through all tags and looks for an element ending with the
specified text. The jQuery code for this method is shown below:
$("[id$='_txtName']");
There are, of course, drawbacks to both methods above. The first is fast, but requires server tags. It’s fast, but it just looks messy.
Also, it will not work with external script files. The second
alternative is clean, but it can be slow. As I said earlier, there are
several other alternatives, but these two are the ones I find myself
using the most.
The third registering Javascript in C# code behind.
Page.ClientScript.RegisterStartupScript(GetType(), "saveScript",
String.Format("function EnableSave( isDisabled )"+
"{{ var saveButton = document.getElementById(\"{0}\");"+
"saveButton.disabled=isDisabled;}}", btnSave.ClientID), true);
Do not forget to call this script after controls have been loaded, I mean after Controls.Add(); in CreateChildControls method while
developing webparts.
$('input[title="Name"]')
Look at the page source and get the value of the title property - works every time.
ListBox1.Attributes.Add("onmouseup",
"document.getElementById('" + base.ClientID + "_" + lbnFilter.ClientID + "').style.display='block';");

Resources