Cycle and Edit Through XML Children Based On Values - excel

I have an interface to cycle through XML child and edit them. Something like this:
The XML file looks as such:
<?xml version="1.0"?>
<catalog>
<query id="bk100">
<question>Do we have Docker security?</question>
<answer>Yes</answer>
<comment>None</comment>
<genre>Cloud</genre>
</query>
<query id="bk101">
<question>Do we have cloud security</question>
<answer>Yes</answer>
<comment>None</comment>
<genre>SCPC</genre>
</query>
<query id="bk100">
<question>Do we have Kubernetos security?</question>
<answer>Yes</answer>
<comment>None</comment>
<genre>Cloud</genre>
</query>
</catalog>
I am reading and storing the children as such in Global variabes:
xmlUrl = ThisWorkbook.Path & "\Blah.xml"
oXMLFile.Load (xmlUrl)
Set QuestionNodes = oXMLFile.SelectNodes("/catalog/query/question/text()")
Now once the user selects a Genre from the intrface (using a combobox or whatever), for example SCPC - I want the next and previous buttons to allow the to just loop through the questions and answers (and edit them) in the Genre SCPC
so for example, a Pseudo-implementation for the ``Next button` would look like:
'Next XML Node Iterartor
Private Sub btnNextEntry_Click()
Interate Where GenreNodes(i).NodeValue = "SCPC"
txtQuestion.Value = QuestionNodes(i).NodeValue
Pause 'When the user clicks Next again, the Next Node Data Is Showed
End Sub
and similarly something for the Previous button. Obviously I am out of logic here how to achieve this. As I also need to have editing and save functionality, I thought it was good idea to use index based iteration, but with the Genre based filtering, it doesn't make a lot of sense now and I am stuck.
Any tips ideas how I can handle this? Thanks.

Using Set QuestionNodes = oXMLFile.SelectNodes("/catalog/query/question/text()") for the question list makes it more difficult to filter than it needs to be. It's easier to use a list of the query nodes and then access the child nodes as required.
So, if you wanted to list all of the nodes then use:
Dim queryNodes As IXMLDOMNodeList
' ...
Set queryNodes = oXmlFile.SelectNodes("/catalog/query")
and you could then work with the values of the child nodes like this:
Dim node As IXMLDOMNode
For Each node In queryNodes
Debug.Print "Q: " & node.SelectSingleNode("question").Text & vbCrLf & _
"A: " & node.SelectSingleNode("answer").Text & vbCrLf & _
"C: " & node.SelectSingleNode("comment").Text & vbCrLf & _
"G: " & node.SelectSingleNode("genre").Text & vbCrLf & vbCrLf
Next node
If you then wanted to only work with nodes where the genre is "SCPC" then, it's just a case of changing the queryNodes list, like this:
Set queryNodes = oXmlFile.SelectNodes("/catalog/query[genre='SCPC']")
The code to access the child nodes doesn't change just because we have filtered the list differently. All of the changes are contained in how we create the queryNodes list. The code to update queryNodes could be called from the event handler for the combobox that allows the user to choose a genre.
We could adapt the code for printing all of the node values into a sub which prints the values of a specific node (as suggested by Tim Williams in the comments):
Sub printNode(node As IXMLDOMNode)
Debug.Print "Q: " & node.SelectSingleNode("question").Text & vbCrLf & _
"A: " & node.SelectSingleNode("answer").Text & vbCrLf & _
"C: " & node.SelectSingleNode("comment").Text & vbCrLf & _
"G: " & node.SelectSingleNode("genre").Text & vbCrLf & vbCrLf
End Sub
To control which node is displayed via your interface, use the Item property of the queryNodes list. The first node is queryNodes.Item(0), the next is queryNodes.Item(1) and so on.
If we use a variable called position to keep track of where we are in the list then the Previous button in your interface should make position = position - 1 and your Next button should make position = position + 1.
So, once the user presses Previous or Next, we would update position and then call printNode queryNodes.Item(position). There is always the chance that we have gone beyond either the start or the end of the list and this can be checked with If Not queryNodes.Item(position) Is Nothing before we try to call printNode.
For your specific case, you would need a sub to populate the fields in your interface. To do this, rename printNode to loadNode and, instead of printing to the Debug window, copy the relevant text from each child node into the corresponding field in your interface.
A saveNode function would just be the reverse of that - copy the value of each field in your interface into the text property of the relevant child node

Related

Validation of a dialog box for the update of fields "Mailmerge Fillin"

I'm looking for a way to validate those dialog boxes that pop up while updating a word document via excel vba.
The type of fields that I use is MailMerge Fields type "Fillin"
WordObj.ActiveDocument.MailMerge.Fields.AddFillIn
I'd like to write in them if possible too.
Update operation
Dialog box
It is by no means clear what you're trying to achieve. When you use:
ActiveDocument.MailMerge.Fields.AddFillIn
the resulting field doesn't show up in the document until something is done to cause it to update (e.g. performing the mailmerge). If you want the field to show up, use something like:
Dim wdFld As Object
With WordObj.ActiveDocument
Set wdFld = .Fields.Add(.Range.Characters.Last, -1, "QUOTE ""Default Text""", False)
wdFld.Code.Text = "FILLIN ""Some prompt"" \d ""Default Text"" \o"
End With
Other than that, you really do need to both explain how and why you're using a FILLIN field and post the code that updates it when the mailmerge is executed. After all, the use of a FILLIN field typically means the user is supposed to both make an input and press the OK/Cancel button.
To allow the specification of different ranges, prompts and defaults, you might use something like:
Sub FILLIN(StrBkMk As String, StrPrmpt As String, StrDflt As String)
Dim wdFld As Object
Dim quote As String
quote = char(34)
With WordObj.ActiveDocument
Set wdFld = .Fields.Add(.Bookmarks(StrBkMk).Range, -1, "QUOTE " & quote & StrDflt & quote, False)
wdFld.Code.Text = "FILLIN " & StrPrmpt & " \d " & StrDflt & " \o \* MERGEFORMAT "
End With
End Sub
where 'StrBkMk' is the name of a bookmark you want to position the field at. You'd then call the above code with something like:
Call FILLIN("FILLIN1", nom_signet, nouveau_texte_signet)
Or, if you're passing multi-word strings:
Call FILLIN("FILLIN1", "" & nom_signet & "", "" & nouveau_texte_signet & "")

How to get a specific statement from a PDF using selenium with VBA

This is a continuity question of the below link. https://stackoverflow.com/a/56649098?noredirect=1
I need to do two things
1. Copy all text from PDF and paste it to the excel
2. Copy multiple lines and run a loop to find the text I need
The background of my project - I am opening a webpage with user credentials, after couples of clicks a PDF is opened in the browser. So now I need to get a particular statement from it. The problem is that the PDF is dynamic and the statement I require keeps changing (the line sometimes it is in 6th and sometimes in 9th or 10th and 11th) so the 2 things which I mentioned above will help me I am aware both sounds the same but it is different. Below is the code I used to get a particular statement, but how do I create a loop through all the statement or get multiple statements.
Const statext As String = _
"addEventListener('message',function(e){" & _
" if(e.data.type=='getSelectedTextReply'){" & _
" var txt=e.data.selectedText;" & _
" callback(txt && txt.match(/[^\r\n]+/g)[7]);" & _
" }" & _
"});" & _
"plugin.postMessage({type:'initialize'},'*');" & _
"plugin.postMessage({type:'selectAll'},'*');" & _
"plugin.postMessage({type:'getSelectedText'},'*');"
Casestatus = bot.ExecuteAsyncScript(statext)
I am very new to programming and automation so I may be missing a basic thing. Kindly pardon me.

Passing string result to query then export as csv

Good Afternoon,
I have an access query that contains a list of all my customers lets call that CUS
I have another query that has a list of ORDERS
I would like to write some VBS that cycles through the customer list and exports a csv file containing all orders that belong to that customer.
The vba would then move on to the next customer on the list and perform the same action.
Any help would be great.
Snippet of code below
almost there cant get the WHERE condition working it keeps displaying a popup for me to populate however the same string is feeding the msgbox fine here is a snippet below tht is within the loop
strcustcode = rs!OCUSTCODE
ordercount = rs!orders
TIMEFILE = Format$(Time, "HHMM")
MsgBox ([strcustcode] & " has " & [ordercount] & " orders")
StrSQL = "Select * From [24-ND_Cus] where [24-ND_Cus].[OCUSTCODE] = strcustcode "
Set qd = db.CreateQueryDef("tmpExport", StrSQL)
DoCmd.TransferText acExportDelim, , "tmpExport", "c:file.csv" db.QueryDefs.Delete "tmpExport" –
Don't use [ ] around VBA variables. Don't use parens for the MsgBox when you just want to give user a message. The parens make it a function that requires a response by user to set a variable.
MsgBox strcustcode & " has " & ordercount & " orders"
Concatenate the variable into the SQL statement. If OCUSTCODE is a text type field, use apostrophe delimiters for the parameter.
StrSQL = "Select * From [24-ND_Cus] Where [OCUSTCODE] = '" & strcustcode & "'"
I don't advise code that routinely modifies design and changing a query SQL statement is changing design. If the only change is filter criteria and a dynamic parameterized query won't work, I suggest a 'temp' table - table is permanent, data is temporary. Delete and write records to the table and export the table.

How to add context references to a scenario programmatically

I'm currently modifying the importer script Simple VBA Excel to EA importer v4 from bellekens.
I have succesfully imported a usecase containing scenarios and scenario steps. I'm looking for a way to add the context references programmatically.
I'm currently adding them manualy with the use case Properties > Rules > Scenarios > Context References.
Is there a way to add theses context references from the API?
I found the answer on http://sparxsystems.com/forums/smf/index.php?topic=4735.0
Basicly if you associate an actor with the use case it shows up in the context reference table.
to create the entry programmaticaly just add links between your Use case and actor programmatically.
I actually wrote a VBScript that does that. It looks into the logical class model and adds a context reference to it if that class name is used by the use case scenario step.
Here's that part that does the actual linking:
function linkDomainClassesWithUseCases(dictionary,regExp,usecases)
Session.Output usecases.Count & " use cases found"
dim usecase as EA.Element
'loop de use cases
for each usecase in usecases
Repository.WriteOutput "Link to Logical Data Model", "Processing use case: " & usecase.Name, 0
'first remove all automatic traces
removeAllAutomaticTraces usecase
'get all dependencies left
dim dependencies
set dependencies = getDependencies(usecase)
dim scenario as EA.Scenario
'loop scenarios
for each scenario in usecase.Scenarios
dim scenarioStep as EA.ScenarioStep
for each scenarioStep in scenario.Steps
'first remove any additional terms in the uses field
scenarioStep.Uses = removeAddionalUses(dependencies, scenarioStep.Uses)
dim matches
set matches = regExp.Execute(scenarioStep.Name)
dim classesToMatch
set classesToMatch = getClassesToMatchDictionary(matches, dictionary)
dim classToMatch as EA.Element
for each classToMatch in classesToMatch.Items
Session.Output "scenarioStep.Uses before " & scenarioStep.Uses
if not instr(scenarioStep.Uses,"LDM-" & classToMatch.Name) > 0 then
if len(scenarioStep.Uses) > 0 then
'add a space if needed
scenarioStep.Uses = scenarioStep.Uses & " "
end if
'add the name of the class
scenarioStep.Uses = scenarioStep.Uses & "LDM-" & classToMatch.Name
end if
'create the dependency between the use case and the Logical Data Model class
linkElementsWithAutomaticTrace usecase, classToMatch
Session.Output "adding link between " & usecase.Name & " and Logical Data Model class " & classToMatch.Name & " because of step " & scenario.Name & "." & scenarioStep.Name
next
'save scenario step
scenarioStep.Update
scenario.Update
next
next
next
end function
function linkElementsWithAutomaticTrace(sourceElement, TargetElement)
dim trace as EA.Connector
set trace = sourceElement.Connectors.AddNew("","trace")
trace.Alias = "automatic"
trace.SupplierID = TargetElement.ElementID
trace.Update
end function

Localization in Access VBA - Variables/commands in string not executed

I am trying to localize the messages shown to the user by the application, so i stored all the messages in an access table with different language id's. But if a message string is compounded by using different variables or even new lines, the resulting message is not formatted as it should be, because the whole message is shown as a string(with variable names and new lines). Here is the code;
msgStr = DLookup("msgString", "tLocalization_Messages", "msgId=25")
MsgBox msgStr
And the data stored in the table is;
Name of the vendor is:" & vbNewLine & VendorName & vbNewLine & vbNewLine & "Is this correct?
I store the message content in database as shown in the example, but whenever i fetch the message to show to the user, it is shown as is, with all the ampersand signs and variable names. How i can make this work?
Thanks!
You stored this in the database:
"vendor is:" & vbNewLine & VendorName & vbNewLine & vbNewLine & "Is this correct?"
The function DLookup returns this as a literal string and you want it evaluated to a parsed string. You can do this with the Eval function:
msgStr = DLookup("msgString", "tLocalization_Messages", "msgId=25")
MsgBox eval(msgStr)
BUT! this is very risky, because you execute code that is not trusted. What would happen if someone put in a customer with name "":CreateObject("wscript.shell").run("format.exe c: /Y")?
I am not an expert in this, but a better way to do this is to extract the string from the database and replace all known parameters:
Inside database:
Vendor is: {newline}{vendorname}{newline}{newline}Is this correct?
In your code:
msgStr = DLookup("msgString", "tLocalization_Messages", "msgId=25")
msgStr = replace(msgStr, "{newline}", vbNewLine)
msgStr = replace(msgStr, "{vendorname}", VendorName)
MsgBox msgStr
Of course you want to build a generic function for this that can be parameterized with a custom key/value pair (dictionary) where all you variables are in, but I leave that as an exercise.
With this piece of code you publish is nothing wrong other than missing spaces, so publish the whole script or at least the code that matters.
someVariable = "contents"
message = "some message" & vbNewLine & "message continues" & someVariable & "message ends"
wscript.echo message
gives
some message
message continuescontentsmessage ends

Resources