Determine if tree node exists - vba

I am creating a macro where at some point it has to navigate through some folders and sub-folders (tree nodes with children) inside SAP
The main folder is expanded in the following code:
session.findById("wnd[0]/shellcont/shell").expandNode "2**000001"
The sub-folder I´m looking for is the following:
session.findById("wnd[0]/shellcont/shell").selectedNode = "251000001"
However this sub-folder may not exist.
I want to add an IF statement to check if it exists or a way to get the list of sub-folders and look for the one which has the text "Status 51".

I can recognize a GuiTree control, which is of type 2 (column tree).
To determine if a given node exists, if you know its key, you could use the following function that you call this way: If TreeNodeExistsByKey(session.findById("wnd[0]/shellcont/shell"),"251000001") Then...
Function TreeNodeExistsByKey(tree,nodekey)
On Error Resume Next
tree.GetHierarchyLevel(nodekey)
If Err.Number = 0 Then
result = True
Else
result = False
End If
TreeNodeExistsByKey = result
End Function
To get and iterate the children of a given node:
Set tree = session.findById("wnd[0]/shellcont/shell")
Set coll = tree.GetAllNodeKeys()
For i = 0 to coll.Length - 1
nodekey = coll.ElementAt(i)
Next
To get the text of a node by its key, use the method GetNodeTextByKey:
nodeText = tree.GetNodeTextByKey(nodeKey)

Related

SSIS - Script Task Check if Subfolder Exists

I want to check if sub-folder exists or not. If exists, move on. If not exists go to next task.
My sub-folder is "C:\Folder1\Folder2\Folder3" I want to check if Folder3 is exists or not.
I worked on it. Create 2 variables
1> FolderPath = C:\Folder1\Folder2
2> FolderExists = Boolean = False
Script Task
ReadOnlyVariable = #FolderPAth
ReadWriteVariable = #FolderExists
Following script I add in edit script
Dim DirExists As String
DirExists = Dir(CStr(Dts.Variables("Folder3").Value))
If DirExists <> "" Then
Dts.Variables("Folder3").Value = True
Else
Dts.Variables("Folder3").Value = False
End If
Can some one correct me please.
Based on your comment it doesn't seem like you will care if it is c# of VB so here are steps from beginning to end on how to test Existence of a folder and use it in constrained precedence.
Define 2 Package Level Variables: FolderPath string, FolderExists boolean
Add Script Task and configure for C# and add FolderPath as a ReadOnlyVariable and FolderExists as a ReadWriteVariable
Click Edit to Edit the script
Scroll to "#region Namespaces" near the top and add using System.IO;
Scroll to the definition of the Main() sub and add the first line after "TODO" below so that the routine becomes:
public void Main()
{
// TODO: Add your code here
Dts.Variables["User::FolderExists"].Value = Directory.Exists(Dts.Variables["User::FolderPath"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
The script task is complete and you should now be able to use the FolderExists variable as the expression for constrained precedence.
Add your next step in the package and connect with the green success arrow then double click the arrow and set the Constraint Options to evaluate for Expression and Constraint and the expression simply as the FolderExists variable.
this solution was fully tested and is operational

How do I get a variable to work within double quotes in ASP-classic?

The following code is part of a function that is being called for each file the program finds during it's search through a directory.
set searchname = objFSO.OpenTextFile(server.mappath(filename),1, true)
do until searchname.AtEndOfStream
lineData = lcase(searchname.ReadLine())
if instr(lineData,s)>0 then
instances = instances + 1
end if
Loop
This the part of my code I'm confused with. This code worked perfectly yesterday. I made a few edits but mostly with HTML and CSS. When I pulled this code out today to optimize it better, I just keep getting a permission denied error with this line.
set searchname = objFSO.OpenTextFile(server.mappath(filename),1, true)
The problem is that the folder the code scans through contains some files that have more than one word. So it's essential that I get the "filename" variable within double quotes. I've been looking everywhere for a solution and none of them have been helpful yet. Is this even possible or is there a workaround?
Any help would be much appreciated here.
EDIT: As requested here's the code that gets the filename. Although no idea how it pertains to my question.
Function filesearch(name)
ext = instr(name,".txt")
vdirectoryvalue = instr(name,"Files\")+6
idname = mid(name,vdirectoryvalue,ext)
vdirectory = len(idname) - 4
filename = left(idname,vdirectory)
instances = 0
set searchname = objFSO.OpenTextFile(server.mappath(idname), 1, true)
do until searchname.AtEndOfStream
lineData = lcase(searchname.ReadLine())
if instr(lineData,s)>0 then
instances = instances + 1
end if
Loop
End Function

QTP Object Required Error due to dynamic change in Web

When an action is performed, it will last for a while to get a message showing that this action is successful. Here I use a function to test if the message comes out:
For i = 0 To NumberOfiframe - 1
Set objPage = iframe(i).Object
Set objTag = objPage.GetElementsByTagName("span")
intTotalLink = objTag.Length - 1
For intCtr = 0 to intTotalLink
If objTag(intCtr) = null Then
Exit Function
End If
strContent = objTag(intCtr).InnerText
endPosition = InStr(1, strContent, "Not all transport requests yet")
If endPosition > 0 then
TRNotReleased = FALSE
Exit for
End If
Next
Next
while when the message comes out, QTP will pop up a window showing "Object Required: objTag(..)", my point is that QTP is not able to find the set object when Web UI changes.
As the message contains different information for each operation, is there any solutions for me to get over from this issue?
Thanks in advance.
When you use objPage.GetElementsByTagName you're getting a reference to a DOM object on the page, this is not a QTP test object but rather an object belonging to the browser. When the DOM in the browser changes the object you're holding onto ceases to be valid.
If you want to access the object after the HTML changes you'll have to ask QTP to retrieve it again. Perhaps like this:
For i = 0 To NumberOfiframe - 1
Set objPage = iframe(i).Object
Set objTag = objPage.GetElementsByTagName("span")
intTotalLink = objTag.Length - 1
For intCtr = 0 to intTotalLink
' Get the collection of objects again
Set objTagCurr = objPage.GetElementsByTagName("span")
Set currObj = objTagCurr(intCtr)

The 'If' statement part is not being carried out

I'm currently working on a project for school and it includes a login and register system.
This is a function that I have made. The "taken" variable will be passed back to the main program.
Dim taken As Boolean
Dim temp As String
For counter = 1 To totalrecords
FileGet(1, player_info)
temp = player_info.username
If TextBox2.Text = temp Then
msgbox("this is a messagebox")
taken = True
End If
Next
This is the part of the code that checks if any usernames are in use. If so, taken = true and then a message is displayed.
Now, for some reason the if statement part is not being carried out. The message box does not show at all. I have tested this by using more than one same username and the second (same) username is still added to the file. I'm very confused.
Part I believe is not working -
If TextBox2.Text = temp Then
taken = True
End If
You should use String.Equals(String1, String) to compare TextBox text and the temp variable.
In your case it should be written as this.
If String.Equals(TextBox2.Text, temp) Then
taken = true
End if

Prevent duplication of node text in treeview (VB.NET)

Process: A node is added in a treeview control with node text = textbox1.text
I want to prevent addition of duplicate node i.e. say, if a node with text "ABC" is added then next time, a node with text "ABC" should not be added to treeview control.
I tried following methods but could not achieve desired result.
Method A)
Dim list As New ArrayList
list.Add(TextBox1.Text)
if list.Contains(Textbox1.Text) then
MsgBox("Use different name")
else
.....code to add node with text
end if
Method B)
if Treeview1.Nodes.Count > 0 then
For i = 0 to Treeview1.Nodes.Count
if Treeview1.Nodes(i).Text=Textbox1.Text then
MsgBox("Use different name")
end if
next
else
........code to add node with text
end if
I could not understand the solutions suggested for C# on this forum.
Any help will be really appreciated.
Thanks
Method A should work OK. You might have another error in your code (in the else section?). list should be declared static if it is in a function that's being called repeatedly, else it will be reset to new (cleared) each time.
Method B has a couple of errors: (1) the for statement should be For i = 0 to Treeview1.Nodes.Count - 1 (maybe use "for each"), and the else with the code to add the node should be after the msgbox statement. Also, method B only search for the root node(s) of the treeview. You would need to traverse the tree to check all the nodes.
If ListView1.Items.Count > 0 Then
For I = 0 To ListView1.Items.Count - 1
For Each LVL As ListViewItem In ListView1.Items
If ListView1.Items.Item(I).Index = LVL.Index Then
Continue For
Else
If ListView1.Items.Item(I).Text = LVL.Text Then
LVL.Remove()
End If
End If
Next
Next
End If