Excel VBA copy the value of a label from Internet Explorer - excel

I'm trying to retrieve the value "CONGE STATUTAIRE" from the following html code
<span class="DescriptionLabel" id="lblProjectDescription">CONGE STATUTAIRE</span>
I've tried this
nom_proj = IE.Document.getElementsByClassName("DescriptionLabel")(0).innerText
The code pass this line without problem but the the value of nom_proj is " ", and I would have hope to get "CONGE STATUTAIRE" for result.
Could anyone tells me what's wrong with it? The rest of my code is working i.e. I can retrieve value by using the getelementbyID method.
Any help will be welcomed.

I would use the getElementById() method, to make sure it can return only one HTML element and not a collection of objects:
nom_proj = IE.Document.getElementById("lblProjectDescription").innerText
However, the reason why you get "" is most probably that the collection returned by getElementsByClassName() has more than one element (often, when retrieving object by class names).
Which means: in the Document of your browser there will be most probably more elements that are styled with the CSS class DescriptionLabel; for example, something like this:
<div name="emptyRow" class = "DescriptionLabel"></div>
You can test if there are more than one element by:
1) either, adding a watcher to IE.Document.getElementsByClassName("DescriptionLabel");
2) or, printing all the elements inside, I bet my hat you'll find inside more than one:
For Each obj In IE.Document.getElementsByClassName("DescriptionLabel")
Debug.Print obj.InnerText
Next obj
GENERAL SUGGESTION: if an HTML object has an id, use the getElementByID; that method returns a single object, not a collection, so even if you would be sure that the collection will contain a unique element, it would anyway be less clean and efficient in terms of coding.

Related

Calling methods dynamically, but not from a custom class

I'm trying to create a simple loop that prints out all available values of dir("Hello") side by side with the help("Hello".xxx) for each dir returned.
I've seen a number of stackoverflow threads on calling functions dynamically from a custom class, but it's not so clear how I can loop through built-in methods.
Taking this as an example:
for dr in dir("Hello"):
Using 'format' converts the "Hello.%d" % dr into a string of "hello.upper", but the print(help('hello.upper')) fails, because the help function expects "hello".upper, not "hello.upper":
for dr in dir("Hello"):
print(dr)
print(help("Hello.%d" % dr))
I've tried researching getattr, but the help function is not a method of the string, so getattr("Hello", "help")("upper") isn't working either.
expected results are:
dir value (followed by:)
dir help output
help doesn't return a string, it opens an interactive viewer for reading the help page.
To view each of these pages for some object (warning: there are going to be a lot of these pages), you can use getattr to get the attribute of the object given its name
obj = "Hello"
for attr_name in dir(obj):
help(getattr(obj, attr_name))

obj.Attributes(0) returns "ID", obj.getAttribute("ID") returns nothing

This is my first attempt to use MSXML2.DOMDocument within VBA, and I'm having a "huh?" moment right off the start. My document looks like this...
<Locations>
<Location ID="23456">
<Properties>
<Property ID="12345">
etc.
I want to make a report with all the Location IDs, so I:
Set locs= XDoc.SelectNodes("//Location")
For Each loc In locs
Debug.Print loc.Attributes(0).Text
Next
and I got 23456. Yay! But of course, those attributes might move around, so let's fix that...
Debug.Print loc.getAttribute("ID").Text
That returns Object required. Looking in the debugger, I can see that loc has one attribute and it's name is ID. That seems right. I can also see that loc.getAttribute("ID") returns null. That seems wrong.
So what's going on here?
I don't know VBA at all but my guess would be that the .attributes[] property returns an attribute object (XmlAttribute?), which would let you access its identifier and value, whereas the getAttribute() function is the getter for the text value of the attribute with that identifier.

Exposing the current combo selection index for the CGridCellCombo class

For several years I have been using the CGridCellCombo class. It is designed to be used with the CGridCtrl.
Several years ago I did make a request in the comments section for an enhancement but I got no replies.
The basic concept of the CGridCellCombo is that it works with the text value of the cell. Thus, when you present the drop list it will have that value selected. Under normal circumstances this is fine.
But I have places where I am using the combo as a droplist. In some situations it is perfectly fine to continue to use the text value as the go-between.
But is some situations it would have been ideal to know the actual selected index of the combo. When I have a droplist and it is translated into 30 languages, and I need to know the index, I have no choice but to load the possible options for that translation and then examine the cell value and based on the value found in the array I know the index.
It works, but is not very elegant. I did spend a bit of time trying to keep track of the selected index by adding a variable to CInPlaceList and setting it but. I then added a wrapper method to the CGridCellCombo to return that value. But it didn't work.
I wondered if anyone here has a good understanding of the CGridCellCombo class and might be able to advise me in exposing the CComboCell::GetCurSel value.
I know that the CGridCtrl is very old but I am not away of another flexible grid control that is designed for MFC.
The value that is transfered back to the CGridCtrl is choosen in CInPlaceList::EndEdit. The internal message GVN_ENDLABELEDIT is used, and this message always use a text to set it into the grid.
The value is taken here via GetWindowText from the control. Feel free to overwrite this behaviour.
The handler CGridCtrl::OnEndInPlaceEdit again calls OnEndEditCell. All take a string send from GVN_ENDLABELEDIT.
When you want to make a difference between the internal value and the selected value you have to manage this via rewriting the Drawing and selecting. The value in the grid is the GetCurSel value and you have to show something different... There isn't much handling about this in the current code to change.
More information
The key is CInPlaceList::EndEdit(). There is a call to GetWindowText (CInPlaceList is derived from CComboBox), just get the index here. Also in CGridCellCombo::EndEdit you have access to the m_pEditWnd, that is the CInPlaceList object and derived from CComboBox, so you have access here too.
I have found this to be the simplest solution:
int CGridCellCombo::GetSelectedIndex()
{
int iSelectedIndex = CB_ERR;
CString strText = GetText();
for (int iOption = 0; iOption < m_Strings.GetSize(); iOption++)
{
if (strText.CollateNoCase(m_Strings[iOption]) == 0) // Match
{
iSelectedIndex = iOption;
break;
}
}
return iSelectedIndex;
}

sharepoint - add custom column to list via object model

I'm having trouble figuring out how to add a custom column type to a list with the object model.
SPFieldCollection.Add() has a parameter SPFieldType, but that must be one of the enumerated values in the Microsoft.SharePoint.SPFieldType enumeration, thus it cannot be used to create columns of a custom type.
I next attempted using SPFieldCollection.CreateNewField() but when I call SPField.Update() on the returned value I get an exception: "ArgumentException was unhandled. Value does not fall within the expected range.".
I see a reference to SPFieldCollection.AddFieldAsXml() here: How do I add custom column to existing WSS list template but there's hardly any info and I'm not sure that's the right track to take.
UPDATE: I found a post on AddFieldAsXml: http://weblogs.asp.net/bsimser/archive/2005/07/21/420147.aspx and it turns out it's very easy and worked well for me. Posting anyway in hopes it will help someone else.
SPFieldCollection.AddFieldAsXml() is the way to go as far as I can tell. See here for an example: http://weblogs.asp.net/bsimser/archive/2005/07/21/420147.aspx
Try with:
SPField newField = null;
newField= web.Fields.CreateNewField("MyFieldTypeName", fieldName);
web.Fields.Add(newField);
newField = web.Fields[fieldName];
// set some properties
newField.ShowInDisplayForm = false;
newField.ShowInViewForms = true;
newField.Update();

Groovy paginate problem

I have an application written in groovy and I am having problems with the pagination of a resulting set.
I have a Controller called ReportingController. This controller has two methods called
listdoiTln and listdoiEv. Both methods are similar and at the end both have to render a list of reports. The last lines of both are as follows:
params.max = Math.min(params.max ? params.max.toInteger() : 15, 100)
render (view: 'list', model:[reportingInstanceList: reportingInstanceList, reportingInstanceTotal: i])
The list view is rendered as expected. At the footer of the list.gsp file I have:
<div class="paginateButtons">
<g:paginate controller="reporting" total="${reportingInstanceTotal}" max="25"/></div>
</div>
The list is working, the buttons for the pagination are there but it is always displayed the whole collection. Notice that I do not have files callled listdoiTln.gsp or listdoiEv.gsp. I am using list.gsp with different data models.
Surely I am doing something wrong.
Any hint?
Thanks in advance.
Luis
I had trouble with this, too, for quite a while. Try this:
Evaluate param.offset in the controller:
params.offset = params?.offset?.toInteger() ?: 0
Include the params in the model:
render (view: 'list',
model:[reportingInstanceList: reportingInstanceList,
reportingInstanceTotal: i,
params: params])
Check whether the value of reportingInstanceTotal is the value that you expect. That tripped me up for a while.
If it still doesn't work, let me know, or try looking at one of the list.gsp pages and its associated controller that are generated by the grails generate-all command.
The paginate buttons are quite cool, but there is little documentation and it takes longer than I expected to set them up.

Resources