What should I do to use Response.Write in VBScript?
Everything else works fine up to this statement.
I never used VBScript before...
The code, which is my first in VBScript and is being written incrementally, works OK with exception of these objects:
WScript.Echo Response.Write Print
I have enabled ASP but I couldn't find a way to install these objects on my PC, which acts as a development server. The production website uses VBScript, ASP and SQL Server. Here is the code:
'...
Do While Not Rs.EOF
AllTerms = AllTerms & Rs("Term")
Rs.MoveNext
Loop
MsgBox AllTerms
' # Print AllTerms
' WScript.Echo AllTerms
' Response.Write Rs("Term")
Rs.Close
Are you using ASP? Response is part of the ASP object model. It's not available when running a VBScript under the Windows Script Host.
Edit:
I see you've updated your code and it looks like you are indeed using ASP. Since you're collecting all of the recordset info in a variable named AllTerms, you should be able to display it using the line:
Response.Write AllTerms ' Use this in place of: MsgBox AllTerms
But if you're new to ASP, you may want to just create a simple page just to test your configuration. Something like this should do:
<%
Response.Write "Today is " & Date
%>
Save it with an .asp extension, request the page from the web server, and make sure the output is proper.
Related
This code works but when adding 1000 characters into it, the web page breaks and does not updated the record in SQL.
Set objRecordset3 = cn3.Execute("INSERT INTO MainDB SET Biography = '" & session("Biography") & "' WHERE ID = '" & session("ID") & "'")
Can anyone offer me a better way to write an UPDATE that will update at least 3,000 characters instead of 200 which this one can do?
Greatly appreciate your input.
Running Classic ASP on Windows 2008 sp2 and SQL 2005.
I have an issue with saving an Outlook 2010 email item as a text file using VBScript. I am beginning to think it is a permissions issue, but the VBScript error message is not very helpful:
myscript.vbs(409, 9) (null): Operation aborted
Code looks like this:
' create an object to hold a folder item -
' point it to first item
set olitem = olitems(1)
if err.number <> 0 then
stdout.write("Could not create Outlook Item object - exiting" & vbcrlf)
stdout.write("Err.Number: " & Err.Number & vbcrlf)
wscript.quit(999)
end if
' save the mail item to a text file
olitem.saveas fullpath_src & messagefilename, oltxt
if err.number <> 0 then
stdout.write("Could not save email as a text file - exiting" & vbcrlf)
stdout.write("Err.Number: " & Err.Number & vbcrlf)
wscript.quit(999)
end if
oltxt is defined as a Const of value 0 - the value for a text file.
I get the same error if I try to process an email's body text under Outlook 2010. Under Outlook 2013 - on a different, non-corporate, machine - the code works fine.
I think I read somewhere that some Outlook 2010 functionality may have been crippled under certain circumstances for security reasons - revolving around the presence/absence of antivirus software. If this is true, I have been stopped in my tracks - even though I have corporate grade AV protection.
I am new to vb6 since I always use vb2010. I am debugging an inventory software running in vb6 and couldn't find out how to fill datalist control using recordset.
The software use this code, it uses listbox...
with rs
if .RecordCount then
.MoveLast
.MoveFirst
For Counter=1 to .RecordCount
Me.ListBox1.AddItem rs!ProductName
.MoveNext
Next
End If
.Close
End With
But the software database has now thousand of products and the program loads the data so slow
so I tried this:
Using Datalist
With DataList1
Set .RowSource = rs
.ListField = "ProductName"
End With
my code runs with no errors but no data will appear in the control.
Can anyone solve this problem for me.
thank you in advance
The problem is the MoveLast call which iterates over the entire recordset solely for the purpose of populating RecordCount. Use this instead:
With rs
Do While Not rs.eof
Me.ListBox1.AddItem rs!ProductName
.MoveNext
Loop
.Close
End With
This code will speed up your call by removing the unnecessary traversing of the recordset, but having thousands of items in a listbox is a design/usability issue that speeding the load will not fix.
I wrote a VBScript app to open Word and Excel documents and search and replace blocks of text and various sections, pulling the new text from a plain text file. I purposely avoided any error checking, primarily because I couldn't figure it out at the time (and the script ran reliably anyway). Now months later on my local machine, I am inexplicably getting error messages about Normal.dot being changed and a message box asking what I want to do about it (which requires three more dialogs to finally answer). Of course this kills my ability to run the script and simply walk away, as it causes the script to fail. Currently when this happens, I have to open the Task Manager, find Winword.exe (of which the GUI isn't running) and kill it then re-run my script.
What's a reasonable way of catching the error and successfully shutting down Word (or Excel). Based on this question I'm trying this:
Set objDoc = objWord.Documents.Open(curDir1 + "\docs\template_spec.dot")
If Err.Number <> 0 Then
WScript.Echo "Error in Word Open:" & Err.Description
objWord.Quit
Else
Set objSelection = objWord.Selection
'Do replacement activities'
ReplaceText(objSelection)
objDoc.SaveAs(curDir1 + "\docs\mynewdocument.doc")
objWord.Quit
End If
Set objShell = Nothing
Set objWord = Nothing
Set objExcel = Nothing
Of course, as fate would have it, I cannot replicate the problem, so it works like normal. Does this solution seem reasonable? And a side question: How the heck do I get Word to stop complaining about Normal.dot (or get the script to handle it)? It's as if Word leaves itself open in the background after I have closed the GUI in some cases.
have you considered wrapping everything into an 'On Error Resume Next' statement so that your script ignores all the errors and continues to run as much as possible before calling the objWord.quit regardless of success or fail.
if you want more information on the correct use of 'On Error Resume Next' then go over to the msdn article on it!
Hope this helps!
Paul
I'm afraid that
WScript.Echo "..."
if it ever fires, is going to stall your script. Other than that, everything looks right. I'll play with it when I get home.
Edit: Word does hang out in the background, quite frequently. For one thing, if you use Outlook, and use Word as your Outlook editor, Word won't go away until Outlook is gone.
I'd agree with the use of "on error resume next".
If you really need to forcefully terminate Word, you can use WMI and the Win32_Process class to find and kill the process. This should be a last resort if everything else fails.
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'winword.exe'")
For Each objProcess in colProcess
objProcess.Terminate()
Next
This was a modified example from:
http://www.computerperformance.co.uk/vbscript/wmi_process_stop.htm
Also, make sure all your references to the Word automation object are closed and/or set to nothing before you terminate the process.
The most reliable way to terminate all ActiveX instances, clean up garbage, and release resources is to put the code for that purpose into Sub Class_Terminate() of a dummy class, created instance of the class allows to handle script quit event.
Option Explicit
Dim objBeforeQuitHandler, objWord
' create a dummy class instance
Set objBeforeQuitHandler = New clsBeforeQuitHandler
' create word app instance
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Add.ActiveWindow.Selection.TypeText "80040000 error was raised. About to terminate the script." & vbCrLf & "Word will be quitted without saving before script termination just you close popped up error message."
' your code here...
' raise an error
Err.Raise vbObjectError
Class clsBeforeQuitHandler
' dummy class for wrapping script quit event handler
Private Sub Class_Terminate()
Dim objDoc
On Error Resume Next ' to prevent errors in case of unexpected word app termination
If TypeName(objWord) <> "Object" Then ' word app has not been closed yet
objWord.DisplayAlerts = False
For Each objDoc In objWord.Documents
objDoc.Saved = True ' to prevent save as dialog popping up
objDoc.Close
Next
objWord.Quit
End If
End Sub
End Class
In a macro for Visual Studio 6, I wanted to run an external program, so I typed:
shell("p4 open " + ActiveDocument.FullName)
Which gave me a type mismatch runtime error. What I ended up having to type was this:
Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
strResult = wshShell.Run("p4 open " + ActiveDocument.FullName)
What is going on here? Is that nonsense really necessary or have I missed something?
As lassevk pointed out, VBScript is not Visual Basic.
I believe the only built in object in VBScript is the WScript object.
WScript.Echo "Hello, World!"
From the docs
The WScript object is the root object of the Windows Script Host
object model hierarchy. It never needs to be instantiated before invoking its
properties and methods, and it is always available from any script file.
Everything else must be created via the CreateObject call. Some of those objects are listed here.
The Shell object is one of the other objects that you need to create if you want to call methods on it.
One caveat, is that RegExp is sort of built in, in that you can instantiate a RegExp object like so in VBScript:
Dim r as New RegExp
VBScript isn't Visual Basic.
Give this a try:
Shell "p4 open" & ActiveDocument.FullName
VB6 uses & to concatenate strings rather than +, and you'll want to make sure the file name is encased in quotes in case of spaces. Try it like this:
Shell "p4 open """ & ActiveDocument.FullName & """"