In VB.NET I can create a key in the Windows Registry like this:
My.Computer.Registry.CurrentUser.CreateSubKey("TestKey")
And I can check if a value exists within a key like this:
If My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\MyKey", _
"TestValue", Nothing) Is Nothing Then
MsgBox("Value does not exist.")
Else
MsgBox("Value exist.")
End If
But how can I check if a key with a specific name exists in the Registry?
One way is to use the Registry.OpenSubKey method
If Microsoft.Win32.Registry.LocalMachine.OpenSubKey("TestKey") Is Nothing Then
' Key doesn't exist
Else
' Key existed
End If
However I would advise that you do not take this path. The OpenSubKey method returning Nothing means that the key didn't exist at some point in the past. By the time the method returns another operation in another program may have caused the key to be created.
Instead of checking for the key existence and creating it after the fact, I would go straight to CreateSubKey.
I use this code. It’s simple, easy, and it works on HKEY_CURRENT_USER\Software\YourAppSettings.
Code:
string[] kyes=Registry.CurrentUser.OpenSubKey(#"Software\YourAppSettings").GetValueNames();
if (!kyes.Contains("keytoknowIfExist"))
{
}
Thanks for the information - this helped me a lot! I did notice while I am in the Visual Studio development module however, the settings were being saved under Computer\HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Elementary\Backup - not the same as the finished Installed Software. I am using ie. SaveSetting("Elementary", "Backup", "BackupFile", bdbname)
Since I didn't know exactly where my settings would be saved in the registry once my product was completed, I tried this and it worked perfect for me. I didn't have to know the exact location, which was helpful.
If GetSetting("Elementary", "Backup", "BackupFile", Nothing) <> Nothing Then
DeleteSetting("Elementary", "Backup", "BackupFile")
bdbname = ""
End If
Anyway, hope it helps someone in the future...
Related
Hi Can someone help me with the Saved Search: So I’m trying to create a formula(numeric) that would say:
CASE WHEN {internalid} = ‘32319’ THEN 1 ELSE 0 END
— didn’t work it didn’t even show on the description
I also tried:
CASE {internalid} WHEN 32319 THEN 1 ELSE 0 END — also didn’t work and didn’t show on the description.
Not sure if perhaps using formula on my saved search is disabled on my end if so how can I know or check? Or maybe I did not use to correct syntax for the CASE WHEN? The formula is also grayed out(highlighted).
I managed to find a solution. As it turns out I just needed to put a value of 1 on the textbox for Value to validate the CASE WHEN Statement. Also whether there's a single quote for the numbers or in this case without a single quote, it still worked.
Either of those forms should work but your screen shots show a Formula(Text) rather than Formula(Numeric)
We have to use VBScript as an embedded script engine in our Hospital Information System.
When we use nothing to set the value of a control (textbox/checkbox/...) it worked always fine. Since somepoint it sets now the textbox to "?>".
item("TEXTBOX").value = nothing ' Leads to -> "?>"
It is not completly clear what causes this, maybe a windows update is responsible, every rollup ~ since KB3212646 Win7 2017-01 seems to cause this error.
My Question is now, has someone else also seem this error, so that it is clear that MS causes this error or is our HIS publisher responsible for not handling nothing correct.
I know setting a textbox to Nothing is not best practice instead "" should be better, but since the item object could be more the just a textbox e.g. a combobox/checkbox this seems, from an objectoriented perpsective, better. Or am I completly wrong?
Following #Ansgar comment, you should apparently change everywhere you have = nothing to = "" in your example
item("TEXTBOX").value = ""
Beware to keep the nothing if you have the Set keyword in left
Set some_object = Nothing
I have some tasks that need to be removed. Some users have installed Chrome under their own profile and the only way to remove it is to "nuke" it since it is not feasible to log in with their account and do the uninstall the proper way. The scheduled tasks have a user sid added to the end of the name, so I have to search for a task with "GoogleUpdate" in the string. When I remove a service, I like to stop the service, disable the service, then delete it. I want to do the same thing with the scheduled tasks. After a lot of scouring of the web, I have found the syntax to disable, delete and that works perfect, but not how to stop. I would prefer not to shell out and use schtasks since I have come this far with a 200 + line script using all VbScript.
Here is what I have so far. I need help figuring out the syntax for the ".Stop" method. If anyone has an example of the stop method, it would be much appreciated.
Set objTaskService = CreateObject("Schedule.Service")
Call objTaskService.Connect()
Set objTaskFolder = objTaskService.GetFolder("\")
Set colTasks = objTaskFolder.GetTasks(0)
For Each objTask In colTasks
With objTask
If InStr(.Name, "GoogleUpdate") Then
objTask.Stop() << Wrong number of args or invalid property assignment
WScript.Sleep(10000)
objTask.Enabled = False << Working properly
objTaskFolder.DeleteTask objTask.Name,0 << Working properly
End If
End With
Next
The official documentation is located here: https://msdn.microsoft.com/en-us/library/windows/desktop/aa382098(v=vs.85).aspx
RegisteredTask.Stop(ByVal flags)
Parameters
flags [in]
Reserved.
Must be zero.
Hence, it should be
objTask.Stop 0
I have read all of the questions on here about this topic and none of them provided me with a workable solution, so I'm asking this one.
I am running a legitimate copy of Excel 2013 in Windows 7. I record a macros where I insert a picture, and in the open file dialog I paste this URL: http://ecx.images-amazon.com/images/I/41u%2BilIi00L._SL160_.jpg (simply a picture of a product on Amazon). This works as expected.
The resulting macros looks like this:
Sub insertImage()
'
' percent Macro
'
'
ActiveSheet.Pictures.Insert( _
"http://ecx.images-amazon.com/images/I/41u+ilIi00L._SL160_.jpg").Select
End Sub
However, when I attempt to run this, the Insert line breaks with the following error:
Run-time error '1004':
Unable to get the Insert property of the Picture class
I am trying to insert a number of pictures into an excel document and I am using the ActiveSheet.Pictures.Insert method to do this. I have been experiencing this issue there, so I recreated it in a way others could replicate to facilitate getting an answer...
An interesting thing to note is:
http://ecx.images-amazon.com/images/I/41u%2BilIi00L._SL160_.jpg 'This is what I pasted
http://ecx.images-amazon.com/images/I/41u+ilIi00L._SL160_.jpg 'This is what the recorded macros recorded
It looks like Excel automatically resolved the %2B to a +. I tried making that change, but to no success.
Another interesting thing to note is that sometimes this does work and sometimes it doesn't. This url is a case where it does not work. Here's one where it does: http://ecx.images-amazon.com/images/I/51mXQ-IjigL._SL160_.jpg
Why would Excel generate a macros it can't run? More importantly, how can I avoid this error and get on with my work!? Thanks!
Try this workaround:
Sub RetrieveImage()
Dim wsht As Worksheet: Set wsht = ThisWorkbook.ActiveSheet
wsht.Shapes.AddPicture "http://ecx.images-amazon.com/images/I/41u+ilIi00L._SL160_.jpg", _
msoFalse, msoTrue, 0, 0, 100, 100
End Sub
All fields are required, which is kind of a bummer since you cannot get the default size. The location offsets and sizes are in pixels/points. Also, the % turning to + is just alright, as % would cause it to be not recognized (dunno why).
Result:
Let us know if this helps.
I'm experiencing the same issue.
After some digging I found out Excel does a HTTP HEAD request before getting the image.
If this HEAD request is unsuccessful Excel will return the exact error messages mentioned in this discussion.
Your could easily test this using Fiddler.
I am writing a script to capture the login time. In the final production, there would be no input from any user. However I am testing it and I wanted to know how I add extra code to determine that
If its in 'debug' mode AND
The user that is logging in is me (lets say my username is joe.smith on the domain called EXAMPLE)
then present an input box to allow me to type the date, time for logging in.
All other users would never see this and it would capture today with the system time.
I would also like to hide the code so if the script is opened by the wrong person, they wouldnt be able to make heads or tails of whats going on.
You can use a command line parameter as Matt says to set the script into debug mode, eg
dim isdebug: isdebug = WScript.Arguments.Named.Exists("debug")
WScript.Echo("in debug mode: " & isdebug)
Which you can invoke with
wscript debugscript.vbs /debug
To get the current user name, you can use either the WMI Service or the WScript.Network object.
Once you have the username, you can conditionally throw up an InputBox and collect the value returned:
dim date_: date_ = Now()
if isdebug and username = "me" then
dim value: value = CDate(InputBox("enter the date and time (dd/mm/yyyy hh:mm:ss)", "please", Now()))
' validate the input here
date_ = CDate(value)
end if
And finally, to obfuscate your code you could use the Scripting.Encoder although it looks like this doesn't seem to be supported on Vista or Windows 7. There does seem to be a few hits on googling the phrase obfuscating vbscript, anyway.
Most of this sounds like it can be resolved by the logic of the script.
Have a command line parameter (debug is an appropriate name) and then have some if logic in the code to do as you wish (present the input box).
For the code obfuscation, I don't know how this could be done in vbscript. Windows scripting host works with JavaScript as well though and there are plenty of tools on the web for making JS harder to read. Maybe you want to look a using JS...
HTH,
Matt
I think you can check the property App.LogMode to see if you are in 'debug' mode or not. If it is 0 then you are running debug mode and if it is 1 you are not.