VBA How to programmatically set objects name? - vba

In FactoryTalk View SE I’m trying to set an objects name in VBA based on another value.
This works:
Dim PumpSP As Object
Set PumpSP = ThisDisplay.PumpSetpoint1
PumpSP.Vaule = 10
This doesn’t work:
Dim PumpSP As Object
Set PumpSP = "ThisDisplay.PumpSetpoint" & "1"
PumpSP.Vaule = 10
How do I get it to take a concatenated string?
Thanks.

How do I get it to take a concatenated string?
You don't, and you can't. PumpSP is an Object, not a String. The only thing you can ever Set it to, is an object reference.
This is very similar to, say, how you would access the controls on a UserForm:
Set box = Me.TextBox1
The reason you can get that object with a string, is because a form has a Controls collection that lets you provide a string, works out which control has that name, and returns the Control object for it:
Set box = Me.Controls("TextBox" & i)
So in your case to go from this:
Set PumpSP = ThisDisplay.PumpSetpoint1
I don't know what ThisDisplay is, but maybe it has a similar collection:
Set PumpSP = ThisDisplay.Controls("PimpSetpoint" & i)
If there's no collection that lets you retrieve items by name with a string literal, you're out of luck.

That is the hard part as we have paid support with Rockwell but they will not help with any VBA questions. Ok fine but there VBA commands and class are not like 90% of the Excel or MS Office VBA you find online. Anyway I was able to find the correct class member.
So this work for me:
Set PumpSP = ThisDisplay.FindElement("PumpSetpoint" & “1”) Thanks for all the help.

Related

In ETAS INCA, how can I extract specific calibration values by accessing the COM-API via VBA?

I'm trying to construct a VBA script that opens INCA and extracts specific calibration values.
To start, I found a Youtube video by ETAS (https://www.youtube.com/watch?v=OQAvE0UT5xk), and I copied the code from there:
Private Function inca_com_api() As Variant
Dim Inca As Object
Dim DB As Object
Dim DBname As String
Dim devices() As Variant
Dim calValue As Variant
Set Inca = CreateObject("inca.inca")
Set DB = Inca.GetCurrentDataBase
DBname = DB.GetName
inca_com_api = DBname
Inca.DisconnectFromTool
End Function
The code opens INCA and runs as intended, returning the database name, but what I want is not the database name, but specific calibration values.
I was under the impression that I could just use the API functions listed in the User's Guide for INCA-MIP (https://www.etas.com/en/downloadcenter/18008.php), which is the INCA COM-API interface package for Matlab.
I don't have INCA-MIP, I just thought I could call those functions through VBA, by using the same function name or similar function names.
But when I try code like:
devices() = DB.IncaGetDevices
or
devices() = DB.GetDevices
or
devices() = Inca.IncaGetDevices
or
devices() = Inca.GetDevices
or the same variations of GetCalibrationValue, IncaGetCalibrationValue, I get:
Run-time error '438': Object doesn't support this property or method
I was planning to use IncaGetDevices to show me a device name, which I could then use as an input argument for IncaGetCalibrationValue, for each of my specific calibration names that I want returned.
Again, I don't have INCA-MIP, but I am wondering how I can return these calibration values using the VBA and the COM-API for INCA?
Also, I tried looking through the following posts for answers:
In ETAS INCA, is there a way to quickly retrieve an item from an arbitrary path in an ASAP2Project?
In ETAS INCA, what classes correspond to each type of database item?
but I don't really know what they're talking about.
Any help would be appreciated, thanks

Assigning object reference to a Variant/Long

I have sometime some error like this: "Required object" (translate from my french error.)
But I can't understand what is exactly an object in VBA (with Access, if it's change something).
For example, I have do an SQL SELECT with ADODB.recordset and I see in the spy of the Access IDE for VBA, the value is an "Variant/Long". Okay, I create an Long variable and I set the value of query in the long variable.
Set userId = RS.Fields(0).value
But it's doesn't work...
At the start, I have think: the object is like an array. But the array already exist in VBA.
Someone can help me ?
The Long data type can hold a 32-bit integer. It isn't an object reference, so don't use Set.
Dim userId As Long
userId = RS.Fields(0).value
For more on what is and isn't an object, see Data Type Summary for VBA.

Can you edit the Value property of a named excel object with VBA code?

ThisWorkbook.Names("numWorkersCompEntries").Value = ThisWorkbook.Names("numWorkersCompEntries").Value + 1
I would like to store an incrementing variable in the name manager rather than storing it somewhere in the spreadsheet so that I can manipulate it by name. The above code seems intuitive to me but I am getting a type mismatch. Any ideas? Thanks for any help you can provide.
--Drake
If you modify your code a bit, you'll see that the values of a single numeric are actually stored as a string, so you're trying to add a string, which Excel isn't happy about. You can try something like this:
Sub test()
Dim namedValue As Variant
namedValue = ThisWorkbook.Names("Foo").Value
ThisWorkbook.Names("Foo").Value = Mid(namedValue, 2, Len(namedValue)) + 1
Debug.Print ThisWorkbook.Names("Foo").Value
End Sub
Which seemed to have the expected results on my test sheet...just make sure you have a named range of "Foo" to test. Obviously tweak to suit your situation, but hopefully it will get you started in the right direction.

Object variable or With Block variable not set error when accessing members of BuildingBlockEntries collection

I am attemping to access members of a template's BuildingBlockEntries collection through a macro in Microsoft Word 2007. As it is a collection, I first thought a For Each loop would be the best way to to this:
For Each bBlock In NormalTemplate.BuildingBlockEntries
MessageBox.Show (bBlock.Name)
Next bBlock
However this attempt through the error: Object doesn't support property or method.
So I tried this method which was suggested here:
Templates.LoadBuildingBlocks
Dim iBB As Integer
iBB = NormalTemplate.BuildingBlockEntries.Count()
Dim bb As Word.BuildingBlock
Dim i As Integer
Dim objCounter As Object
If iBB > 0 Then
For i = 1 To iBB
objCounter = i
bb = NormalTemplate.BuildingBlockEntries.Item(objCounter)
MessageBox.Show (bb.Name)
Next
End If
However, this is resulting in the error shown in the title: Object variable or With Block variable not set.
I have tried just using an integer variable for the index, i specifically, but with now avail. How can I acheive the desired effect? What is wrong with my attempt?
Thank you for the help.
With your 2nd question the answer is that you need to use Set, as bb is an object:
Set bb = NormalTemplate.BuildingBlockEntries.Item(objCounter)
For more info on Set take a look at this SO question.
With your For/Next loop, it's not clear how you've declared bBlock. I guess it should be something like:
Dim bBlock as BuildingBlock
And perhaps the For line should reference BuildingBlocks instead of BuildingBlockEntries:
For Each bBlock In NormalTemplate.BuildingBlocks
I don't know for sure though, as I'm just looking at what pops up in Intellisense.

Outlook Redemption : GetNamesFromIDs

I'm trying to get all property names / values from an Outlook item. I have custom properties in addition to the default outlook item properties. I'm using redemption to get around the Outlook warnings but I'm having some problems with the GetNamesFromIDs method on a Redemption.RDOMail Item....
I'm using my redemption session to get the message and trying to use the message to get the names of all the properties.
Dim rMessage as Redemption.RDOMail = _RDOSession.GetMessageFromID(EntryID, getPublicStoreID())
Dim propertyList As Redemption.PropList = someMessage.GetPropList(Nothing)
For i As Integer = 1 To propertyList.Count + 1
Console.WriteLine(propertyList(i).ToString())
Console.WriteLine(someMessage.GetNamesFromIDs(________, propertyList(i)))
Next
I'm not totally sure what to pass in as the first parameter to getNamesFromIDs. The definition of GetNamesFromIDs is as follows:
GetNamesFromIDs(MAPIProp as Object, PropTag as Integer) As Redemption.NamedProperty
I'm not totally sure what should be passed in as the MAPIProp object. I don't see this property referenced in the documentation. http://www.dimastr.com/redemption/rdo/MAPIProp.htm#properties
Any help or insight would be greatly appreciated.
I think I figured it out. I have used VBA only, so you need to "think around" it's limitations, it will follow the same scheme in VB.NET.
The function signature is this:
Function GetNamesFromIDs(MAPIProp As Unknown, PropTag As Long) As NamedProperty
As the first parameter it requires an object which supports the IUnknown interface. Looking at the Redemption docs it became clear that there is an interface named _MAPIProp, from which many other RDO objects are derived (IRDOMail is among them). So this must be the very RDOMail you are trying to get data out of.
Knowing that, it needed only one other subtle hint from the docs to get it working:
Given a prop tag (>= 0x80000000),
returns the GUID and id of the named
property.
So property tag must be >= 0x80000000, this means it wont work for all properties, but just for the custom ones (I guess that is the distinction in this case, correct me if I'm wrong.) Passing in property tags not fulfilling this condition raises an error message (0x8000ffff "unexpected results").
Here is my code. It is VBA, so forgive me the Hex() blunder, as VBAs long integer overflows for numbers that big. I am sure you will get the picture.
Sub GetNamesFromIds()
Dim rSession As New Redemption.RDOSession
Dim rMessage As Redemption.RDOMail
Dim PropertyList As Redemption.PropList
Dim PropTag As Long
Dim EntryId As String
Dim i As Integer
rSession.MAPIOBJECT = Application.Session.MAPIOBJECT
' retrieve first random mail for this example '
EntryId = ActiveExplorer.CurrentFolder.Items(1).EntryId
Set rMessage = rSession.GetMessageFromID(EntryId)
Set PropertyList = rMessage.GetPropList(0)
For i = 1 To PropertyList.Count
PropTag = PropertyList(i)
If "0x" & Right("00000000" & Hex(PropTag), 8) > "0x80000000" Then
Debug.Print
If IsArray(rMessage.Fields(PropTag)) Then
Debug.Print Hex(PropTag), "(Array:", UBound(rMessage.Fields(PropTag)), "items)"
Else
Debug.Print Hex(PropTag), "(", rMessage.Fields(PropTag), ")"
End If
Debug.Print " GUID:", rMessage.GetNamesFromIds(rMessage, PropTag).GUID
Debug.Print " ID:", rMessage.GetNamesFromIds(rMessage, PropTag).ID
End If
Next
End Sub
First snippet from the output:
8041001E ( urn:content-classes:message )
GUID: {00020386-0000-0000-C000-000000000046}
ID: content-class
Well, for background info, the author suggests using something like OutlookSpy to see how Outlook stores the properties.
Looking at this exchange (make sure to read through all the follow-up responses), there isn't much more (in fact, I think at one point the Outlook MVP types GetNamesFromIDs when he means GetIDsFromNames).
What you might try is using GetIDsFromNames to see what that returns, and then use that to pass to GetNamesFromIDs.
I've used Redemption before, but not in this particular manner, so that's all I've got for you ...