VBScript returns an error of "Name redefined" - vbscript

I have an alert box, and if I'm currently on the test Database, I want the alert box to have different text when compared to live database.
So I declared a variable called isTestDb and set it to True, but I keep getting this annoying error:
Microsoft VBScript compilation error '800a0411'
Name redefined
myfiles/conn_open.asp, line 12
Dim isTestDb
----^
Here is my simple code which sits inside an include at a much higher level than where I'm doing my conditional check, I'm assuming that isn't a problem.
Dim isTestDb
isTestDb = True
-- Much Later in another file somehwere
If isTestDb Then
Response.Write("<h1>It is set to True</h1>")
Response.End
Else
Response.Write("<h1>It is set to False</h1>")
Response.End
End If
I have checked all my working directory and I'm certain that variable is not set somewhere else by the same name, I even tried different names for the variable and got the exact same error.

In my case this was due to the fact that I had referenced the same file twice in my .asp file using:
<!--#include file="myFile.asp">
And the referenced file had the declaration in it hence the second declaration conflicted with the first

This is happenning because the variable, isTestDb has already been defined in another page.

I have no idea why, but when I removed the Dim isTestDb line, it all works as expected now!! :/

it can also be because you are including file you already including in other include file.
For example:
File a.asp is including x.asp.
And in your main file you including both:
<!--#include file="a.asp">
<!--#include file="x.asp">

Related

Error Saving File in Document Folder of Library

I am writing an HTA file with VBScript as the scripting language. I have a function where the user is prompted to choose the folder in which they would like to save a document. I didn't write this code myself, but instead borrowed it from here.
Function SelectFolder(myStartFolder)
Dim objFolder, objItem, objShell
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "Select Folder to Save New File", 0, myStartFolder)
If IsObject(objFolder) Then SelectFolder = objFolder.Self.Path
End Function
I call this function in another one in order when I make the file and prompt the user to choose where to save it:
Sub Example()
Dim destPath, destFile, objWorkbook
destPath = SelectFolder(libPath)
destPath = destPath & "\Sample Export"
Set destFile = CreateObject("Excel.Application")
Set objWorkbook = destFile.Workbooks.Add()
objWorkbook.SaveAs(destPath)
(code to edit excel file)
End Sub
Example() works fine except when someone chooses to save their document in one of the four default libraries in Windows (Documents, Music, Pictures, Videos). In that case, I receive an error saying "The file could not be accessed" and indicating the error is at the line that says "objWorkbook.SaveAs(destPath)". The error box then gives me the following suggestions:
Make sure the specified folder exists
Make sure the folder that contains the file is not read-only
Make sure the file name does not contain any of the following characters: < > ? { } : | or *
Make sure the file/path name doesn't contain more than 218 characters.
The error occurs when I open the HTA file and click a button calling Example(), which then opens a dialog box to ask the user to choose the file location. When Documents, Music, Pictures, or Videos is chosen, a "Script Error" box pops up with the error message listed above. I am not familiar enough with VBScript to know what the problem is exactly. I would appreciate any suggestions as to what to do.
I don't know exactly what the solution to the above issue was. However, I noticed that the path I pass to SelectFolder had a typo in it. libPath was supposed to be to the %userprofile% from the environment variable. However, I had pluralized "userprofiles" so that it was %userprofiles% instead. This took me to somewhere that looked right but wasn't. I fixed the typo and now BrowseForFolder takes me to C:/Users/*username* like it's supposed to. It turns out that I was being sent to the Desktop location because of the fourth argument in BrowseForFolder, instead of the user profile like I wanted.

Printing VBScript variables in QTP UFT

How can I check values of my variables at run time when using QTP UFT?
I simply want to create a variable, do logic and fill it with data, set a breakpoint in the line following the variable and then check or output its value somewhere.
I have tried:
print variableName
WScript.Echo variableName
The first produces error: Print function type mismatch
The second produces error: Object required: "WScript"
I'm not sure where the problem lies as I've just started to get into both UFT and VBScript (mostly did C# and javascript and everything is quite different here). Could someone tell me the correct solution and perhaps also explain these errors to me?
If you wanted to see all variables use the debug viewer in QTP.
View -> Debug Viewer
There you can list all the variables you want to watch. You should be able to see them in break points.
Note : Ensure you have Windows script debugger installed to use the debug viewer.
You can also add the variable to watch .. Insert a breakpoint>> start the script then right click on the variable under test and add it to watch(Add to Watch) . After Adding you will see a watch window and the value of the variable will be displayed.
I obsess over my variables... so much so that I sprinkly my code with print statements... Actually, I abstract print into my own UDF (so that I can optionally add logging to a file if needed)...
Here's my function: (it's actually a sub because it doesn't return anything)
Sub say(textToSay)
If talkative Then 'note that if I set global var "talkative" to false, then the whole log is disabled
print textToSay
End If
End Sub
Then, ALL my code typically looks like this...
say "click submit button"
Broswer("WebSite").Page("Search").WebButton("Submit").click
say "check for result"
if not Browser("WebSite").Page("Results").Exist then
say "results page didn't appear after exist timeout"
failtest
else
say "successfully found results page"
end if
Honestly, I'm shocked that your "print variableName" statement gave an error, print is actually available in the VBScript api, so it should have worked.
All of my output goes to the Output pane in UFT. I would give my right-arm to find a way to programmatically clear that output pane between runs, but noone seems to know how to do it.
The benefit of all this logging is that I can watch my code run and see EVERY branch taken by the code, and I can add statements to print my variables.
Here's an example that shows how I would answer your question:
result = browser("WebSite").Page("Results").WebElement("Result").GetROProperty("innertext")
say "result:" & result
if result = "Approved" then
Reporter.ReportEvent micPass, "Check for approved", "Approved!"
else
Reporter.ReportEvent micFail, "Check for approved", "NOT Approved!"
End If
Notice the say statement in there - I'll be able to see that immediately during code execution without waiting until the results are shown at the end.

Cannot convert Group Description to String Variable

I have the following code:
Set objHoldGroup = GetObject("LDAP://" & objGroup)
strGroupDesc = (objHoldGroup.Description)
WScript.Echo(strGroupDesc)
The variable strGroupDesc returns nothing when echoed. I can output the description directly but I need it for further processing. Thoughts?
Explanation: Apparently your script sets Option Explicit (good), which you didn't tell us about (bad). This option makes defining variables before you can use them mandatory (good). Normally that would raise an "undefined variable" error, though. Since that doesn't seem to happen with your code, you seem to also have an On Error Resume Next somewhere in your code (very bad), which, again, you chose to keep quiet about (bad).
Next time please don't omit parts of your code that are vital for troubleshooting the problem. And don't use On Error Resume Next.
Issue found: I forgot to Dim strGroupDesc.

Insert Property of Picture Class

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.

VB6 "Invalid use of property" error where the code seems fine

I am having a very strange problem. First, the code.
Private Function ProcessRecord(ByVal rsDocs As ADODB.Recordset) As Variant
Dim rsTemp As ADODB.Recordset
rsTemp = rsDocs
rsDocs = RemoveDuplicateDocs(rsTemp)
Exit Function
The error is occurring on the second line of the function, where rsTemp is set equal to rsDocs. It's saying: "Compile error: Invalid use of property". I've looked for information on this error elsewhere, and all the reports are cases where people either forgot an equal sign, or incorrectly added the "Set" command to the beginning of the line of code. This error makes no sense to me, because it was compiling fine before, and the changes I've made to this project are not even in the class that throwing the error. The code here is identical to the way it was before. Has anyone ever seen an error like this pop up for what seems to be no good reason? Thanks!
You need to use
set rsTemp = rsDocs
since rsTemp is an object.

Resources