Vary a variable-name in a for loop - asp.net

Im looking for help with trying to vary a variable-name in a for loop:
For b = 1 To Count
' the below image_var(b) varible needs to vary on each iteration'
Dim image_var(b) As New LinkedResource(Server.MapPath(myArray(b-1)))
' need also to have the b in myArray(b-1) bit work through the loop too'
' that is I''m after the array index b minus 1'
image_var(b).ContentId = "imageContentId_" + b.ToString
' add the LinkedResource to the appropriate view'
htmlView.LinkedResources.Add(image_var(b))
Next b
Thanks in advance as I can't logon to accept an answer...
Thanks Guffa - my pics are now making it through to the email and showing up.
The (b) in the image_var(b) was just a stub for me too - till I found the code I was after... Am new and didn't even know/realise that it made an array... am a nobb.
Thanks again...

I don't know why you think that you need a separate variable for each instance. The variable just holds a reference to the object, it doesn't matter at all what you call the variable. You can just reuse the same variable for each of the objects:
For b = 1 To Count
Dim image As New LinkedResource(Server.MapPath(myArray(b-1)))
image.ContentId = "imageContentId_" + b.ToString
' add the LinkedResource to the appropriate view'
htmlView.LinkedResources.Add(image)
Next b

Related

Trouble with "string.contains" function

I am writing a web form that has a unique travelID per travel request. (see blank column in picture below, I'm not high enough reputation to embed the picture in here)
Some of the travel requests have attached documents and others do not. I wrote this code:
Dim dirs As String() = Directory.GetFiles("E:\DomainWebs\Intranet\fileups\TravelDocs\")
Dim i As Integer = 0
For Each GridRecord As GridRecord In TravelWebDataGrid.Rows
For Each doc As String In dirs
If doc.Contains(TravelWebDataGrid.Rows(i).Items(10).Value) = True Then
TravelWebDataGrid.Rows(i).Items(11).Value = True
End If
Next
i += 1
Next
Just for testing, only the top Travel Request has documents attached. As you can see, it is checking the second row checkbox as well even though it doesn't have any documents attached.
This is what is being compared to row 2:
"DocName" - E:\DomainWebs\Intranet\fileups\TravelDocs\RONNIEP20180124141116-Hello.docx
"TravelID" - RONNIEP20180124135357
Does anyone know why it is doing this? Let me know if you need any more information. Thanks in advance for your responses.
This might be off because I haven't worked with a datagrid but it looks like the code has been over-complicated with the counter. Can you do this?
' you're looping gridrecords/rows.
For Each GridRecord As GridRecord In TravelWebDataGrid.Rows
For Each doc As String In dirs
' use the gridrecord here, not rows(i).
If doc.Contains(GridRecord.Items(10).Value) = True Then
' and here.
GridRecord.Items(11).Value = True
End If
Next
Next

Getting documents of last 25 minutes with formula and lotusscript

In lotus I have a view with order documents.
I am building an agent to search for all orders which are modified in the last 25 minutes.
For this I have done code like:
strFormule = "Form=""Order"" & #Modified >= #Adjust(#Today;0;0;0;0;-25;0) & Deleted !=""J"""
Set ndcOrder = currentDB.Search( strFormule, Nothing, 0 )
If ndcOrder.Count <> 0 Then
Set doc = ndcOrder.GetFirstDocument
While Not doc Is Nothing
So if it is 11.00 then it need to take orders which are modified today from 10.35
But in the debugger I also get orders which where modified 2 hours earlier.
How is this possible?
I think it's might be because you're using #today which doesn't have a time element. Try #Now instead ?
In the past I used the LotusScript method GetModifiedDocuments which lets you specify a NotesDateTime object to retrieve any document modified since.
Your code could then look like this:
Dim session As New NotesSession
Dim db As NotesDatabase
Dim dc As notesdocumentcollection
Dim since As New NotesDateTime("")
Set db = session.CurrentDatabase
Call since.SetNow()
Call since.AdjustMinute(-25)
Set dc = db.GetModifiedDocuments(since)
My experience with this method was very good so far. More info on GetModifiedDocuments
Why use formula at all?
I would create a hidden view, first column is last modified date-time, sorted descending.
Then I would write my Lotusscript code to start at the top and work its way down until it encounters a date/time value that is older than 25 minutes ago.
Something like this:
Dim docs List As NotesDocument
Set dt25 = New NotesDateEntry(Now())
Call dt25.AdjustMinutes(-25)
Dim col as NotesViewEntryCollection
Dim entry as NotesViewEntry
Set col = view.AllEntries
Set entry = col.GetFirstEntry
Do Until entry Is Nothing
If Cdat(entry.ColumnValues(0))<Cdat(dt25.LSLocalTime) Then
Exit Loop
End If
Set docs(entry.Document.UniversalID) = entry.Document
Loop
' Now you have a list of documents created in the last 25 minutes.

How to autofill OpenOffice Math formula editor in OOo Calc?

I am using OpenOffice Calc spreadsheet formulas with psuedo-random numbers to generate arrays of arithmetic problems which I can easily update to creating new worksheets (I'm a teacher)
Problems are output as formula mark-ups in string form. OOo Math formulas use these string commands typed into the editor to display nicely formatted maths expressions.
I can do this next step manually:
1) go to source cell and copy string mark-up to clipboard
2) select target cell and clear existing contents and objects
3) create new Math object anchored to target cell
4) open Math editor window and paste in mark-up string
5) exit Math editor window and return cursor to source cell
Result: a nice maths expression of given arithmetic problem.
I need to be able to do this for entire columns of source cells on various sheets.
...even better, to then add a listener to dynamically update as sources are updated.
I found code here: Cell content inside formula that achieves this for a fixed pair of cells, but despite all my best efforts, I have had to admit defeat - generalising this code is simply beyond my expertise!
The absolute ideal would be a macro function that I could call like a spreadsheet function; with input arguments (sourceCell, targetCell, listenerON/OFF) that could run the above algorithm and dynamically update if required.
Can anybody help me? A solution like this, or any kind of workaround would be immensely helpful.
UPDATE 2016/10/27
Thank you Jim K, that did work, but use of the dispacher comes with a whole host of difficulties I hadn't foreseen.
I just found Charlie Young's post in the OpenOffice forum which makes use of the API. I have included my adaptation of his code below.
Can anybody help me to integrate it into a function in a similar way as I've described? I don't know how to solve placement of the Math object in to the target cell.
The API code is great as it will create a new Math object each time the code is updated. Existing ones do need to be deleted though.
I think the limitation of not being able to delete existing objects from within a function is going to persist. Would this be the case even if done by a subroutine called by the function?
function InsertFormula(paraFromCell, paraToCell)
Dim oDoc As Object
Dim oSheet As Object
Dim oShape As Object
oDoc = ThisComponent
oSheet = oDoc.Sheets(0)
oShape = oDoc.createInstance("com.sun.star.drawing.OLE2Shape")
oShape.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
oSheet.Drawpage.Add(oShape)
oShape.Model.Formula = paraFromCell
oShape.setSize(oShape.OriginalSize)
end function
NEXT UPDATE
I've been managing to solve my own problems quite quickly now...
I've decided to go with a sub, not a function, so I can access the sheet to delete existing objects. Code is attached - Source cells are in Column C and target cells in matching rows of Column A. So far I am only able to send objects to $A$1.
How do I anchor each new object to a specific cell?
REM ***** BASIC *****
Sub InsertThisFormula
Dim oDoc As Object
Dim oSheet As Object
Dim oShape As Object
Dim sourceCell As Object
Dim targetCell As Object
oDoc = ThisComponent
oSheet = oDoc.Sheets(1)
Dim n As Integer
n = 1 'number of rows of formulas
for i = 0 To n-1
rem loop through cells
sourceCell = oSheet.getCellByPosition(2, i)
targetCell = oSheet.getCellByPosition(0, i)
rem clear target cell object/s
targetCell.ClearContents(128)
oShape = oDoc.createInstance("com.sun.star.drawing.OLE2Shape")
oShape.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
oSheet.Drawpage.Add(oShape)
oShape.Model.Formula = sourceCell.string
oShape.setSize(oShape.OriginalSize)
Next i
End Sub
Starting from Mifeet's example, add this to My Macros:
rem ----------------------------------------------------------------------
rem Creates a math formula from text
Function InsertFormulaFromCell(paramCellFrom, paramCellTo)
dim document as object
dim dispatcher as object
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem go to cell containing markup and copy it
dim fromCellArgs(0) as new com.sun.star.beans.PropertyValue
fromCellArgs(0).Name = "ToPoint"
fromCellArgs(0).Value = paramCellFrom
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, fromCellArgs())
dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())
rem go to cell where I want the formula displayed
dim toCellArgs(0) as new com.sun.star.beans.PropertyValue
toCellArgs(0).Name = "ToPoint"
toCellArgs(0).Value = paramCellTo
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, toCellArgs())
rem open Star.Math
oDesk = createUnoService ("com.sun.star.frame.Desktop")
dispatcher.executeDispatch(document, ".uno:InsertObjectStarMath", "", 0, Array())
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem paste clipboard using Array() as place-holder for variable name
dispatcher.executeDispatch(document, ".uno:Paste", "", 0, Array())
rem exit Star.Math
dispatcher.executeDispatch( _
document, ".uno:TerminateInplaceActivation", "", 0, Array())
InsertFormulaFromCell = "Math Formula updated " & Now()
End Function
To run it, put this formula in cell C5:
=INSERTFORMULAFROMCELL("$C$3","$C$20")
Now when the values get updated, it creates another formula.
Note: I could not get the .uno:Delete section of Mifeet's code to work, perhaps because functions are not supposed to access other cells. This may require manually deleting the formulas before creating new ones.
(Posted solution on behalf of the OP).
This is now solved. After a lot of searching, I found what I needed! Simple really. Future improvements might be to resize cells appropriately. Happy for now. Thanks to Jim K and the rest of the Stack Overflow community!
Complete macro below:
REM ***** BASIC *****
Sub InsertThisFormula
Dim oDoc As Object
Dim oSheet As Object
Dim oShape As Object
Dim sourceCell As Object
Dim targetCell As Object
oDoc = ThisComponent
oSheet = oDoc.Sheets(1)
Dim n As Integer
n = 6 'number of rows of formulas
for i = 0 To n-1
rem loop through cells
sourceCell = oSheet.getCellByPosition(2, i)
targetCell = oSheet.getCellByPosition(3, i)
rem clear target cell object/s
targetCell.ClearContents(128)
oShape = oDoc.createInstance("com.sun.star.drawing.OLE2Shape")
oShape.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
oSheet.Drawpage.Add(oShape)
oShape.Model.Formula = sourceCell.string
oShape.setSize(oShape.OriginalSize)
oShape.Anchor = targetCell
oShape.MoveProtect = True
Next i
End Sub

delete an element from an array in classic ASP

Given the following array as an example...
arr(0)(0) = 3
arr(0)(1) = name
arr(0)(2) = address
arr(1)(0) = 7
arr(1)(1) = name
arr(1)(2) = address
arr(2)(0) = 14
arr(2)(1) = name
arr(2)(2) = address
I need to delete the middle element (id=7) from the array. I understand that I need to loop through the array and move each record that isnt to be deleted into a new array. I tried like this...
Dim newArr,i
Redim newArr(Ubound(arr))
For i = 0 to Ubound(arr)
If (CStr(arr(i)(0)) <> 7 ) Then
newArr(i) = arr(i)
End if
Next
When debugging this I can see the if statement work so I know only 2 elements are copied but newArr is empty at the end of this. What am I missing. I am a PHP coder that is new to classic asp and Im used to having array functions that make this kind of thing unnecessary. Any help appreciated. Thank you.
You don't need new array, you can just reassign the items and "crop" the array:
Const removalIndex = 1
For x=removalIndex To UBound(arr)-1
arr(x) = arr(x + 1)
Next
ReDim Preserve arr(UBound(arr) - 1)
This code will remove the array item at index 1 from the main array. If you don't know in advance the index of the item to remove, you can easily find it with a simple loop over the array.
Instead of using array you can give Scripting.Dictionary a try.
It is much more flexible, and has, among others Remove method.
I suggest using Scripting.Dictionary and using it as a List/collection instead, as it allows for insertions and deletions. See here: Lists in VBScript
I don't know the definitive answer, but if I were to take a stab in the dark guess I'd suggest that since the array is two dimensional maybe you have to explicitly refer to it that way?
Dim newArr,i
Redim newArr(Ubound(arr),3)
For i = 0 to Ubound(arr)
If (CStr(arr(i)(0)) <> 7 ) Then
newArr(i)(0) = arr(i)(0)
newArr(i)(1) = arr(i)(1)
newArr(i)(2) = arr(i)(2)
End if
Next
I see some VBScript syntax issues. First:
arr(0)(0) = 3 'ERROR: Subscript out of range
arr(0, 0) = 3 'CORRECT
Next:
ReDim newArr(Ubound(arr)) 'this is 1 dimensional array
newArr(0) = arr(0) 'this will NOT work
newArr(0) = arr(0, 0) 'this will work
And finally: why you convert to String and then compare it to an Integer with:
(CStr(arr(i)(0)) <> 7)

Obtain data from dynamically incremented IDs in JQuery

I have a quick question about JQuery. I have dynamically generated paragraphs with id's that are incremented. I would like to take information from that page and bring it to my main page. Unfortunately I am unable to read the dynamically generated paragraph IDs to get the values. I am trying this:
var Name = ((data).find("#Name" + id).text());
The ASP.NET code goes like this:
Dim intI As Integer = 0
For Each Item As cItem in alProducts1
Dim pName As New System.Web.UI.HtmlControls.HtmlGenericControl("p")
pName.id = "Name" & intI.toString() pName.InnerText = Item.Name controls.Add(pName) intI += 1
Next
Those name values are the values I want...Name1, name2, name3 and I want to get them individually to put in their own textbox... I'm taking the values from the ASP.NET webpage and putting them into an AJAX page.
Your question is not clear about your exact requirement but you can get the IDs of elements with attr method of jQuery, here is an example:
alert($('selector').attr('id'));
You want to select all the elements with the incrementing ids, right?
// this will select all the elements
// which id starts with 'Name'
(data).find("[id^=Name]")
Thanks for the help everyone. I found the solution today however:
var Name = ($(data).find('#Name' + id.toString()).text());
I forgot the .toString() part and that seems to have made the difference.

Resources