Exception Thrown: 'System.IndexOutOfRangeException' in EPPlus.dll - indexoutofrangeexception

Using Visual Studio Community 2017 And AdvancedHMI to create a PC based HMI application. I have several running with no issue so moving onto something new.
On each line I have 7 array's (500 real numbers each array) and would like to capture this data daily and save to Excel.
Using EPPlus and AdvancedHMI I have the following code.
Private Sub DataSubscriber1_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged
If e.ErrorId = 0 AndAlso e.Values.Count > 0 AndAlso e.Values(0) = "True" Then
Console.WriteLine("About to read the data")
Dim MyValues() As String = EthernetIPforCLXCom1.Read("VCell_1A_FES_Cycle_Average[0]", 500)
Console.WriteLine(MyValues.Length & "elements read.")
'* Transfer the values to Excel
Using ExcelPackage As New OfficeOpenXml.ExcelPackage(New System.IO.FileInfo("c:\Data.xlsx"))
For I = 0 To MyValues.Length - 1
Console.WriteLine("Element " & I & "=" & MyValues(I))
ExcelPackage.Workbook.Worksheets(1).Cells(1, I + 1).Value = MyValues(I)
Next
End Using
End If
End Sub
Running this and triggering my tag vale to execute the datascriber I get the following.
About to read the data
500Elements read.
Element 0=87.945
Exception Thrown: 'System.IndexOutOfRangeException' in EPPlus.dll
Everything looks like it should work but I am very new to VB or any type of coding for that matter. My forte' is PLC ladder logic.
Thanks.

Here is what I have working. The Datascriber has a trigger tag in the PLC. Once it's triggered it collects the array data, all 500 elements. Creates a new Excel file and writes the data to Column A, Cells 1-500
Private Sub DataSubscriber1_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged
If e.ErrorId = 0 AndAlso e.Values.Count > 0 AndAlso e.Values(0) = "True" Then
Console.WriteLine("About to read the data")
Dim MyValues() As String = EthernetIPforCLXCom1.Read("VCell_1A_FES_Cycle_Average[0]", 500)
Console.WriteLine(MyValues.Length & "elements read.")
Transfer the values to Excel
Using ExcelPackage As New OfficeOpenXml.ExcelPackage(New IO.FileInfo("C:\Data.xlsx"))
ExcelPackage.Workbook.Worksheets.Add("test")
For Index = 0 To MyValues.Length - 1
Console.WriteLine("Element " & Index & "=" & MyValues(Index))
Dim ws As OfficeOpenXml.ExcelWorksheet = ExcelPackage.Workbook.Worksheets(1)
Console.WriteLine("Worksheet OK")
Dim CellNum As String = "A" & (Index + 1)
ws.Cells(CellNum).Value = MyValues(Index)
Next
ExcelPackage.Save()
End Using
End If
End Sub

Related

simplest Unostructure that supports he getByName

In LibreOffice Basic sub I use a bunch of uno properties in an array. Which is the simplest Unostructure or UnoService that I must "embed" them, in order to use the getByName "function"?
Example:
dim props(1) as new com.sun.star.beans.PropertyValue
props(0).Name = "blahblah1"
props(0).Value = "blahblah1Value"
props(1).Name = "blahblah2"
props(1).Name = 3000
I want to be able to use something like:
b = props.getByName("blahblah2").Value
or something like (assuming I "assigned" them in a structure-like-object called "somestruct") :
b = somestruct.getprops.getByName("blahblah2").Value
As I understand that this can be done by creating a "UnoService" which supports the getByName and then, somehow, assigning these props to this service
Which is the "lightest" such service?
(I mean the service that uses less resources)
Thanks in advance.
Really supporting the interface XNameAccess is not as easy. The services which implement this interface are supposed using this interface for existing named properties, not for own created ones.
But you can use the service EnumerableMap to achieve what you probably want.
Example:
sub testEnumerableMap
serviceEnumerableMap = com.sun.star.container.EnumerableMap
oEnumerableMap = serviceEnumerableMap.create("string", "any")
oEnumerableMap.put("blahblah1", "blahblah1Value")
oEnumerableMap.put("blahblah2", 3000)
oEnumerableMap.put("blahblah3", 1234.67)
msgbox oEnumerableMap.get("blahblah1")
msgbox oEnumerableMap.get("blahblah2")
msgbox oEnumerableMap.get("blahblah3")
'msgbox oEnumerableMap.get("blahblah4") 'will throw error
msgbox oEnumerableMap.containsKey("blahblah2")
msgbox oEnumerableMap.containsValue(3000)
if oEnumerableMap.containsKey("blahblah4") then
msgbox oEnumerableMap.get("blahblah4")
end if
end sub
But starbasic with option Compatible is also able supporting Class programming like VBA does.
Example:
Create a module named myPropertySet. Therein put the following code:
option Compatible
option ClassModule
private aPropertyValues() as com.sun.star.beans.PropertyValue
public sub setProperty(oProp as com.sun.star.beans.PropertyValue)
bUpdated = false
for each oPropPresent in aPropertyValues
if oPropPresent.Name = oProp.Name then
oPropPresent.Value = oProp.Value
bUpdated = true
exit for
end if
next
if not bUpdated then
iIndex = ubound(aPropertyValues) + 1
redim preserve aPropertyValues(iIndex)
aPropertyValues(iIndex) = oProp
end if
end sub
public function getPropertyValue(sName as string) as variant
getPropertyValue = "N/A"
for each oProp in aPropertyValues
if oProp.Name = sName then
getPropertyValue = oProp.Value
exit for
end if
next
end function
Then within a standard module:
sub testClass
oPropertySet = new myPropertySet
dim prop as new com.sun.star.beans.PropertyValue
prop.Name = "blahblah1"
prop.Value = "blahblah1Value"
oPropertySet.setProperty(prop)
prop.Name = "blahblah2"
prop.Value = 3000
oPropertySet.setProperty(prop)
prop.Name = "blahblah3"
prop.Value = 1234.56
oPropertySet.setProperty(prop)
prop.Name = "blahblah2"
prop.Value = 8888
oPropertySet.setProperty(prop)
msgbox oPropertySet.getPropertyValue("blahblah1")
msgbox oPropertySet.getPropertyValue("blahblah2")
msgbox oPropertySet.getPropertyValue("blahblah3")
msgbox oPropertySet.getPropertyValue("blahblah4")
end sub
LibreOffice Basic supports the vb6 Collection type.
Dim coll As New Collection
coll.Add("blahblah1Value", "blahblah1")
coll.Add(3000, "blahblah2")
MsgBox(coll("blahblah1"))
Arrays of property values are the only thing that will work for certain UNO interfaces such as dispatcher calls. If you simply need a better way to deal with arrays of property values, then use a helper function.
Sub DisplayMyPropertyValue
Dim props(0 To 1) As New com.sun.star.beans.PropertyValue
props(0).Name = "blahblah1"
props(0).Value = "blahblah1Value"
props(1).Name = "blahblah2"
props(1).Name = 3000
MsgBox(GetPropertyByName(props, "blahblah1"))
End Sub
Function GetPropertyByName(props As Array, propname As String)
For Each prop In props
If prop.Name = propname Then
GetPropertyByName = prop.Value
Exit Function
End If
Next
GetPropertyByName = ""
End Function
XNameAccess is used for UNO containers such as Calc sheets. Normally these containers are obtained from the UNO interface, not created.
oSheet = ThisComponent.Sheets.getByName("Sheet1")
May UNO objects support the XPropertySet interface. Normally these are also obtained from the UNO interface, not created.
paraStyleName = cellcursor.getPropertyValue("ParaStyleName")
It may be possible to create a new class in Java that implements XPropertySet. However, Basic uses helper functions instead of class methods.
I think the serviceEnumerableMap is the answer (so far). Creating the values and searching them was much faster then creating props in a dynamic array and searching them with a for loop in basic.
(I do not "dare" to use "option Compatible", although I was a big fun of VB6 and VBA, because of the problems in code that maybe arise).
I used this code to test time in a form:
SUB testlala(Event)
TESTPROPS(Event)
' TESTENUM(Event)
MSGBOX "END OF TEST"
END SUB
SUB TESTENUM(Event)
DIM xcounter AS LONG
'b = now()
serviceEnumerableMap = com.sun.star.container.EnumerableMap
oEnumerableMap = serviceEnumerableMap.create("string", "any")
FOR xcounter= 0 TO 10000
oEnumerableMap.put("pr" & FORMAT(xcounter,"0000"), xcounter -10000)
NEXT
'b=now()-b
b = now()
FOR xcounter = 1 TO 5000
lala = Int((9000 * Rnd) +1)
g =oEnumerableMap.get("pr" & FORMAT(lala,"0000"))
'MSGBOX GetValueFromName(props,"pr" & FORMAT(xcounter,"0000"))
NEXT
b=now()-b
MSGBOX b*100000
END SUB
SUB TESTPROPS(Event)
DIM props()
DIM xcounter AS LONG
'b = now()
FOR xcounter= 0 TO 10000
AppendProperty(props,"pr" & FORMAT(xcounter,"0000"), xcounter -10000)
NEXT
'b=now()-b
b = now()
FOR xcounter = 1 TO 5000
lala = Int((9000 * Rnd) +1)
g = GetValueFromName(props,"pr" & FORMAT(lala,"0000"))
'MSGBOX GetValueFromName(props,"pr" & FORMAT(xcounter,"0000"))
NEXT
b=now()-b
MSGBOX b*100000
END SUB
REM FROM Andrew Pitonyak's OpenOffice Macro Information ------------------
Sub AppendToArray(oData(), ByVal x)
Dim iUB As Integer 'The upper bound of the array.
Dim iLB As Integer 'The lower bound of the array.
iUB = UBound(oData()) + 1
iLB = LBound(oData())
ReDim Preserve oData(iLB To iUB)
oData(iUB) = x
End Sub
Function CreateProperty(sName$, oValue) As com.sun.star.beans.PropertyValue
Dim oProperty As New com.sun.star.beans.PropertyValue
oProperty.Name = sName
oProperty.Value = oValue
CreateProperty() = oProperty
End Function
Sub AppendProperty(oProperties(), sName As String, ByVal oValue)
AppendToArray(oProperties(), CreateProperty(sName, oValue))
End Sub

VBA Excel make array of sequential numbers

I am working in VBA for Excel at the moment but am really only versed in Matlab. It's important for my work to stay in the memory of vba (not on the worksheets of excel) for time purposes.
What I need to do is make an array of sequential integers, say 4000 through 5000.
In matlab this is really easy, I would just do... i = 4000:5000, or i=4000:1:5000. With the 1 in the second case being my 'step.'
I was wondering what is the best way to achieve this result in vba?
Thanks
Without looping - Just seen Rory's same answer above after posting
Sub MakeArray()
Dim x As Long, y As Long, arr As Variant
x = 4000: y = 5000
arr = Evaluate("Row(" & x & ":" & y & ")")
'Show result
Sheets(1).Range("A1").Resize(y - x + 1) = arr
End Sub
The following is an example of creating and then displaying a set of sequential numbers:
Sub seqnum()
Dim firstnum As Long, secnum As Long
firstnum = 7
secnum = 23
ReDim ary(1 To secnum - firstnum + 1) As Long
For i = 1 To UBound(ary)
ary(i) = firstnum + (i - 1)
Next i
msg = ""
For i = 1 To UBound(ary)
msg = msg & i & vbTab & ary(i) & vbCrLf
Next i
MsgBox msg
End Sub
I Us "Fill" - "Series":
Write in first cell number ex. 400 and in the "Series" window insert increment step and in "Stop Value" last value. ex. 420
Or with Macro
Range("I1").Select
ActiveCell.FormulaR1C1 = "4000"
Range("I1").Select
Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
Step:=1, Stop:=4020, Trend:=False

Excel VBA Listbox - Only Format non blanks as Dates

This one has me stumped. I am populating a Listbox with a range and then formatting column 4 as d/mm/yyyy. This works fine if all cells in column 4 have a date. As some cells that are populated into the Listbox are in fact blank the sub crashes when it hits these cells. I have tried various if and else statements to skip the activecell if blank with no luck.
Grateful for any assistance.
Alex V
Sub populate_listbox_1()
Dim I As Long
Dim I2 As Long
Dim list_count As Long
Dim MyData As Range
Dim r As Long
With edit_report_input.compliments_listbox
.ColumnCount = 17
.ColumnWidths = "70;300;75;90;80;80;100;0;0;0;0;0;0;0;0;20;0"
.RowSource = ""
Set MyData = ActiveSheet.Range("A4:Q498") 'Adjust the range accordingly
.List = MyData.Cells.Value
For r = .ListCount - 1 To 0 Step -1
If .List(r, 1) = "" Then
.RemoveItem r
End If
Next r
End With
For I = 0 To edit_report_input.compliments_listbox.ListCount - 1
edit_report_input.compliments_listbox.List(I, 4) = Format(DateValue(edit_report_input.compliments_listbox.List(I, 4)), "d/mm/yyyy")
Next I
date_rec_compliment = Format(date_rec_compliment, "d/mm/yyyy")
End Sub
you can always check before changing the format. See if below snippet helps
For I = 0 To edit_report_input.compliments_listbox.ListCount - 1
if edit_report_input.compliments_listbox.List(I, 4) <> "" Then
edit_report_input.compliments_listbox.List(I, 4) = Format(DateValue(edit_report_input.compliments_listbox.List(I, 4)), "d/mm/yyyy")
End If
Next I

Compare 2 listboxes and show nonmatching values in 3rd listbox

I've been working on this for 2 days. Basically I have 2 ListBoxes and I want a command button to compare the values and show the non-matching values (those that appear in the first listbox but not in the 2nd) and list them in the 3rd listbox. I'm not sure if this is the best way to go about it but here's my code. It errors on the line with the message:
Wrong number of arguments or invalid property assignment
My listboxes are named CompList1, CompList2 and CompList3.
Dim BoolAdd As Boolean, I As Long, j As Long
'Set initial Flag
BoolAdd = True
'If CompList2 is empty then abort operation
If CompList2.ListCount = 0 Then
MsgBox "Nothing to compare"
Exit Sub
'If CompList1 is empty then copy entire CompList2 to CompList3
ElseIf CompList1.ListCount = 0 Then
For I = 0 To CompList2.ListCount
CompList3.AddItem CompList2.Value
Next I
Else
For I = CompList2.ListCount - 1 To 0 Step -1
For j = 0 To CompList1.ListCount
If CompList2.ListCount(I) = CompList1.ListCount(j) Then
'If match found then abort
BoolAdd = False
Exit For
End If
DoEvents
Next j
'If not found then add to CompList3
If BoolAdd = True Then CompList3.AddItem CompList2.Value
DoEvents
Next I
End If
Some notes:
Dim tdf1 As TableDef
Dim tdf2 As TableDef
Dim db As Database
Set db = CurrentDb
Set tdf1 = db.TableDefs(Me.CompList1.RowSource)
For Each fld In tdf1.Fields
sFields = sFields & ";" & fld.Name
Next
sFields = sFields & ";"
Set tdf2 = db.TableDefs(Me.CompList2.RowSource)
For Each fld In tdf2.Fields
sf = ";" & fld.Name & ";"
sFields = Replace(sFields, sf, ";")
Next
Me.CompList3.RowSource = Mid(sFields,2)
Edit:

Can images be read from an iPhone programmatically using CreateFile in Windows?

When an iPhone is connected to a Win7 computer, the images can be viewed using Explorer (and the open file dialog of my app). However, the file location does not contain a drive letter.
For example Computer\Apple iPhone\Internal Storage\DCIM\800AAAAA\IMG_0008.JPG instead of E:\DCIM\800AAAAA\IMG_0008.JPG which is common of sdcards, usb drives, etc...
I've tried using CreateFileW to read images from an iPhone but it fails with '(Error Code: 3) The system cannot find the path specified.' I've also tried accessing them with Chrome and it fails too.
Any suggestions?
The folder is actually what is referred to as a 'Virtual Folder' and does not have a full path on the file system. You will need to use the shell item returned from the open dialog to get the content of the file rather than using CreateFile.
The data should be accessible, but you should follow the instructions from the MSDN documentation. I'm sure there are probably better examples (as this only gives guidelines).
edit the rough process is to get the IShellItem from IFileOpenDialog, then to bind to the stream and then read the stream (assuming reading only) - bear in mind that this code is pretty much without error handling or checking or safety:
if (pitem->GetDisplayName(SIGDN_NORMALDISPLAY, &destName) == S_OK) {
std::cout << destName << std::endl;
CoTaskMemFree(destName);
}
IStream *pistream;
if (pitem->BindToHandler(0, BHID_Stream, IID_PPV_ARGS(&pistream)) == S_OK) {
char input[1024];
long to_read = 1024;
unsigned long read;
while (S_OK == pistream->Read(input, to_read, &read)) {
std::cout << input << std::endl;
}
pistream->Release();
}
pitem->Release();
Most often such a device is inserted in the Windows Explorer as a Shell Namespace Extension and not like an USB stick with drive letter. Most of the normal file commands like CopyFile(..), FindFirst() or GetFileInfo(..) can not be used directly in such a Shell Namespace extension. Only the CopyHere(..) is working.
I needed long time to figure out how to enumerate the files on a digicam and now also on an Android device with an vb.net program and to copy my pictures to my Windows PC:
Public Const MyComputer As Integer = &H11&
Sub EnumMyComputer()
Dim oItem As Object
Dim res As Integer
For Each oItem In DirectCast(CreateObject("Shell.Application").Namespace(MyComputer).Items, System.Collections.IEnumerable)
Debug.Print(oItem.Type.ToString)
if oItem.Type.ToString="Tragbares Medienwiedergabegerät" then '<- check, adopt!
res = EnumNamespaceItems(oItem, "", oItem.Name.ToString, 0)
End If
Next oItem
End Sub
Function EnumNamespaceItems(oItem As Object, SrcCPath As String, SrcDPath As String, folderLevel As Integer) As Integer
Dim y As Object
Dim tempFullFileName As String
Debug.Print(StrDup(folderLevel, " ") & "\" & oItem.Name.ToString & " (" & oItem.Path.ToString & ")")
For Each y In DirectCast(oItem.GetFolder.items, System.Collections.IEnumerable)
'Debug.Print(StrDup(folderLevel, " ") & SrcDPath & y.Name.ToString)
If y.IsFolder = True Then
Dim n1 As Integer
n1 = EnumNamespaceItems(y, SrcCPath & y.Path.ToString & "\", SrcDPath & y.Name.ToString & "\", 1 + folderLevel)
If n1 < 0 Then 'failure: Cancel
EnumNamespaceItems = n1
Exit Function
End If
Else 'it's a file:
Debug.Print(StrDup(folderLevel, " ") & " " & y.Name.ToString)
tempFullFileName = System.IO.Path.GetTempPath() & y.Name.ToString
' CopyFile is not possible here if SrcCPath is like "::{…}…":
' My.Computer.FileSystem.CopyFile(SrcCPath & y.Name.ToString , fFile.FullName)
Dim suc As Integer = CopyHereFileWait(y, My.Computer.FileSystem.SpecialDirectories.Temp)
If suc >= 0 Then 'now we can do things like this:
Dim MyFileInfo As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(tempFullFileName)
Dim fileDate As Date = MyFileInfo.LastWriteTime
End If 'suc
End If 'else y.IsFolder
Next y
EnumNamespaceItems = 0
End Function
Function CopyHereFileWait(sourceNamespaceObject As Object, targetFolder As String) As Integer
Dim fsMyStream As System.IO.FileStream
Dim n1 As Integer
Dim taregetFullFileName As String
n1 = Len(targetFolder)
If Mid(targetFolder, n1, 1) = "\" Then
targetFolder = Microsoft.VisualBasic.Left(targetFolder, n1 - 1)
End If
taregetFullFileName = targetFolder & "\" & sourceNamespaceObject.Name.ToString
Dim oNsTargetFolder As Object
oNsTargetFolder = CreateObject("Shell.Application").Namespace(CStr(targetFolder))
oNsTargetFolder.copyHere(sourceNamespaceObject)
'returns immediately and is doing the work in the background
n1 = 0
Do
Threading.Thread.Sleep(50) 'ms
Try
fsMyStream = System.IO.File.Open(taregetFullFileName, IO.FileMode.Open, IO.FileAccess.ReadWrite)
fsMyStream.Close()
CopyHereFileWait = n1
Exit Function
Catch ex As Exception
Debug.Print(ex.Message)
End Try
n1 = n1 + 1
Loop While n1 < 400 'timeout 400*50ms = 20s
CopyHereFileWait = -n1
End Function
You may add to check for folders with y.Name.ToString="DCIM" (on folderLevel=1) and for files with ".jpg".