VBScript text Formatting issues - vbscript

I have data that has <br> and <BR> Tags in it. I have to update a old site that is running VBScript Classic ASP. I know little to nothing about either of these two but i came up with some hackish logic to try and quickly resolve the problem and i do not understand why it is not working.
If InStr(1,objRecN("News"),"<BR>")> 1 Then
response.write "This is 1<BR>"
body = Replace(objRecN("News"),"<BR>", vbCrLf)
Else
response.write "This is 2<br>"
body = Replace(objRecN("News"),"<br>", vbCrLf)
End If
The Response.Writes are working effectively in proving that the right logic is being applied but the replace is not working.
body = Replace(Replace(objRecN("News"),"<br>", vbCrLf), "<BR>", vbCrLf)
Also this is not working. This was my first attempt at trying to resolve this. Any clues?
As soon as other logic is applied it breaks!
If InStr(1,objRecN("News"),"<BR>")> 1 Then
response.write "This is 1<BR>"
body = Replace(objRecN("News"),"<BR>", vbCrLf )
End If

All hard info (c) #Alex.
VBScript's Replace function works; it can even be asked to ignore case (mark the use of pre-defined vbTextCompare instead of magic number). If it does not seem so, the programmer is to blame. Evidence:
>> s = "abc<br>def<BR>ghi"
>> WScript.Echo qq(s)
>> s = Replace(s, "<br>", "!!!!", 1, -1, vbTextCompare)
>> WScript.Echo qq(s)
>>
"abc<br>def<BR>ghi"
"abc!!!!def!!!!ghi"
If you replace with vbCrLf (or other whitespace) and then write the result to HTML, you won't 'see' the hard work Replace did for you.
If you insist on two (or more) replacements, you have to feed the previous result to the current operation. That's why your
body = Replace(objRecN("News"),"<BR>", vbCrLf)
body = Replace(objRecN("News"),"<br>", vbCrLf)
'work' separately but not together. In contrast, both
body = objRecN("News")
body = Replace(body,"<BR>", vbCrLf)
body = Replace(body,"<br>", vbCrLf)
or
body = Replace(Replace(objRecN("News"),"<br>", vbCrLf), "<BR>", vbCrLf)
will deal with all <BR> and <br>, but not with <Br>, which is no problem for the vbTextCompare version.
If you next data contain <br/>, <br />, and <br />, you'll need a regular expression.

Thank you everyone for your help.
body = Replace(body,"<BR>", "<br>")
Although i learned a lot from your posts. This is the code that actually fixed my problem. I am sorry if i didn't not convey it better.

Related

how can i add VB6 Adodc.recordset.find

I'm trying to search and display my database on textbox using VB6, but im not quite sure where the problem is,how would i display my data base in textboxes after i search for it?
I've aldready tried different set of codes but none seems to work. i've also tried the inputbox.
Dim search As String
search = Text5.Text
Adodc1.Recordset.Find "Studno = " & search
If Adodc1.Recordset.EOF Then
MsgBox "NO record"
Else
Adodc1.Recordset.Fields("Studno") = Text1.Text
Adodc1.Recordset.Fields("Studname") = Text2.Text
Adodc1.Recordset.Fields("Age") = Text3.Text
Adodc1.Recordset.Fields("Address") = Text4.Text
End If
i would like to display all the fields in the textboxes after i searched for the studno.
You're trying to add the content of the textboxes to the recordset fields rather than the other way round. Try:
Text1.Text = Adodc1.Recordset.Fields("Studno")
Text2.Text = Adodc1.Recordset.Fields("Studname")
Text3.Text = Adodc1.Recordset.Fields("Age")
Text4.Text = Adodc1.Recordset.Fields("Address")
Ok. Updated answer. Try:
Adodc1.Recordset.Find "Studno = '" & search & "'"
and put a breakpoint on the first line after the Else statement to make sure it's getting that far. (Note there is a single quote both before and after the search string.)
You'll still need the changes I posted originally to actually see the results.

Why does this classic ASP loop to split a textarea into lines not work?

I am trying to put a textarea into an array, splitting it where there are new lines. I know that docnos does in fact contain the characters found within the textarea. Can anyone please tell me why this doesn't work? It is not putting anything into the array for some reason.
docnos = dbencodeStr(Request.form("docnos"))
Dim myArray
myArray = Split(docnos, vbCrLf)
for i = 0 to UBound(myArray,2)
tempstr = trim(myArray(0,i))
strSQL = "insert into DocumentNumbers (queryid, documentnumber) values('"&queryid&"','"&tempstr&"');"
Response.write(strSQL)
Response.write(tempstr)
Set rs = TransactionQueriesConn.Execute(strSQL, ,adCMdTExt)
next
Given myArray = Split(docnos, vbCrLf), UBound(myArray,2) will throw an "Subscript out of range" error (no second dimension). Remove the "On Error Resume Next".
Assuming myArray holds an one dimensional array of lines, the loop needs UBound(myArray) or UBound(myArray,1).
If docnos contains "234234234<BR>567567<BR>345345345", you need to Split on <BR>. Evidence:
>> s = "234234234<BR>567567<BR>345345345"
>> WScript.Echo Split(s, "<BR>")(1)
>>
567567

Is there a way to make special characters work when using InStr in VBScript?

A VBScript is in use to shorten the system path by replacing entries with the 8.3 versions since it gets cluttered with how much software is installed on our builds. I'm currently adding the ability to remove duplicates, but it's not working correctly.
Here is the relevant portion of code:
original = "apple;orange;apple;lemon\banana;lemon\banana"
shortArray=Split(original, ";")
shortened = shortArray(1) & ";"
For n=2 to Ubound(shortArray)
'If the shortArray element is not in in the shortened string, add it
If NOT (InStr(1, shortened, shortArray(n), 1)) THEN
shortened = shortened & ";" & shortArray(n)
ELSE
'If it already exists in the string, ignore the element
shortened=shortened
End If
Next
(Normally "original" is the system path, I'm just using fruit names to test...)
The output should be something like
apple;orange;lemon\banana
The issue is entries with punctuation, such as lemon\banana, seem to be skipped(?). I've tested it with other punctuation marks, still skips over it. Which is an issue, seeing how the system path has punctuation in every entry.
I know the basic structure works, since there are only one of each entry without punctuation. However, the real output is something like
apple;orange;lemon\banana;lemon\banana
I thought maybe it was just a character escape issue. But no. It still will not do anything with entries containing punctuation.
Is there something I am doing wrong, or is this just a "feature" of VBScript?
Thanks in advance.
This code:
original = "apple;orange;apple;lemon\banana;lemon\banana"
shortArray = Split(original, ";")
shortened = shortArray(0) ' array indices start with 0; & ";" not here
For n=1 to Ubound(shortArray)
'If the shortArray element is not in in the shortened string, add it
'i.e. If InStr() returns *number* 0; Not applied to a number will negate bitwise
' If 0 = InStr(1, shortened, shortArray(n), 1) THEN
If Not CBool(InStr(1, shortened, shortArray(n), 1)) THEN ' if you insist on Not
WScript.Echo "A", shortArray(n), shortened, InStr(1, shortened, shortArray(n), vbTextCompare)
shortened = shortened & ";" & shortArray(n)
End If
Next
WScript.Echo 0, original
WScript.Echo 1, shortened
WScript.Echo 2, Join(unique(shortArray), ";")
Function unique(a)
Dim d : Set d = CreateObject("Scripting.Dictionary")
Dim e
For Each e In a
d(e) = Empty
Next
unique = d.Keys()
End Function
output:
0 apple;orange;apple;lemon\banana;lemon\banana
1 apple;orange;lemon\banana
2 apple;orange;lemon\banana
demonstrates/explains your errors (indices, Not) and shows how to use the proper tool for uniqueness (dictionary).

Excel VBA Outlook / Mail functions requires recalculation or sets all formulas to #Value

I'll try to keep it short and precise.
I really hope you can help me.
I am facing the following problem:
Background:
Workbook with a lot of formulas -> Calculation set to manual
Recalculation takes 5-10 minutes each time
What I want to do:
Generate ranges of data individually for multiple people
then select those ranges, and paste them into the body of an e-mail
send those e-mails one by one
What is the problem?
If I use the "Envelope" method to prepare the e-mails everything is fine until I press send. However, every time I press send excel automatically recalculates the entire Workbook. Obviously I do not want to wait 5-10 minutes to send out each e-mail (always between 10 and 20)
Since I thought it might have to do with the "Envelope" method I decided to switch to creating an e-mail directly via Outlook (outlook object). It worked fine as far as opening the e-mail and sending it without recalculation. However, after the e-mail is opened by Outlook, all(!) formulas in the entire Workbook are set to #Value. This obviously also forces me to recalculate as I cannot create the table for the next person's e-mail.
Does anyone know what is causing the recalculation/error values and what I can do to stop it? I'd be really glad about any suggested solutions.
I am also attaching my code, though I doubt it will help in clearing up the issue
`'DESCRIPTION:
'This routine prepares an e-mail for requesting the progress estimates from the deliverable owners
'1. set all the values based on named ranges in PI and Config sheets
'2. Concatenate all relevant strings to full e-mail text
'3. select PI table
'4. Create e-mail and display
Sub PrepareEmail()
Dim s_EmailAddress As String, s_FirstName As String
Dim s_Email_Greeting As String, s_Email_MainText1 As String, s_Email_MainText2 As String, s_Email_DeadlineRequest As String
Dim s_Email_Deadline As String, s_Email_Subject As String, s_Email_ClosingStatement As String, s_Email_SenderName As String, s_Email_CC As String
Dim s_Email_Full As String
Dim rng_PI_TableValues As Range, rng_PI_TableFull As Range
Dim s_Email_FullText As String
Dim obj_OutApp As Object
Dim obj_OutMail As Object
s_EmailAddress = [ptr_PI_Email]
s_FirstName = [ptr_PI_FirstName]
s_Email_Subject = [ptr_Config_PIEmail_Subject]
s_Email_Greeting = [ptr_Config_PIEmail_Greeting]
s_Email_MainText1 = [ptr_Config_PIEmail_MainText1]
s_Email_MainText2 = [ptr_Config_PIEmail_MainText2]
s_Email_DeadlineRequest = [ptr_Config_PIEmail_DeadlineRequest]
s_Email_Deadline = [ptr_Config_PIEmail_Deadline]
s_Email_ClosingStatement = [ptr_Config_PIEmail_ClosingStatement]
s_Email_SenderName = [ptr_Config_PIEmail_SenderName]
s_Email_CC = [ptr_Config_PIEmail_CC]
'Concatenate full e-mail (using HTML):
s_Email_Full = _
"<basefont face=""Calibri"">" _
& s_Email_Greeting & " " _
& s_FirstName & ", " & "<br> <br>" _
& s_Email_MainText1 & "<br>" _
& s_Email_MainText2 & "<br> <br>" _
& "<b>" & s_Email_DeadlineRequest & " " _
& s_Email_Deadline & "</b>" & "<br> <br>" _
& s_Email_ClosingStatement & "," & "<br>" _
& s_Email_SenderName _
& "<br><br><br>"
'-------------------------------------------------------
Set rng_PI_TableValues = Range("tbl_PI_ProgressInput")
Set rng_PI_TableFull = Union(rng_PI_TableValues, Range("tbl_PI_ProgressInput[#Headers]"))
Application.EnableEvents = False
Application.ScreenUpdating = False
Set obj_OutApp = CreateObject("Outlook.Application")
Set obj_OutMail = obj_OutApp.CreateItem(0)
With obj_OutMail
.To = s_EmailAddress
.CC = s_Email_CC
.Subject = s_Email_Subject
.HTMLBody = s_Email_Full & RangetoHTML(rng_PI_TableFull)
.Display
End With
Application.EnableEvents = True
Application.ScreenUpdating = True
Call update_Status
End Sub
`
If you're using RangeToHTML from Ron de Bruin's website, that's what's causing your problem. That utility is fine if you need perfect fidelity and have a heavily formatted bu otherwise fairly simple range. But if your range has a bunch of dependencies, you'll have problems. It's putting the range into its own workbook, so any formulas that refer to data outside the range get funky.
If you need perfect fidelity, then you're stuck because the only way it will be perfect is by saving the range as HTML and reading that back. But if you don't have a bunch of heavy formatting or you just need a nice looking table, then I suggest you write your own RangeToHTML function that produces the HTML strings.
David McRitchie has some functions that do a pretty good job if you don't want to roll your own. http://dmcritchie.mvps.org/excel/xl2html.htm
Also, I don't know what update_Status does, but if it's causing a recalc, then you have two problems. If that's the case, figure out how to store up all the stuff that update_status does and do it once at the end rather than in the loop.

Need to find character within apostrophe substring and replace it, any ideas?

Ok, let me preface this question with the fact that I am extremely new to vb script, but I need to make an addition to a script that another developer created and he is away.
Problem. :
I am getting data that is feed into a process. The script I already have handles some things that I won't go into. One thing I found out that it doesn't handle correctly is commas that occur in double quotes.
So I get some data like this. :
Joe, Bill, James, Alex, "Google, Inc", Rose, "Kickstarter, LLC"
What happens is that when this script kicks to the process application it blows up because of these commas in the double quotes. I need to be able to have this script go in and replace those commas in the Google and Kickstarter blocks with something a little more unique.
If you have the answer to this I would appreciate it, but I wouldn't mind some direction either of something that does something like this, etc.
Any answers are greatly appreciated. Again very new to vbascript just started reading syntax on it today.
You left out a few details about the format of the input data and the replacement text. However, the approach you need to take is to use the regular expression capability of VBScript to identify patterns in your input data, then use the "Replace" method to replace them. Here's a short MS article about regular expressions in VBScript.
Here's an example of what it might look like. This example is definitely NOT bullet proof and it makes some assumptions, but I think it will get you started:
Dim re, strstr, newstrstr
Dim inputstrarray(7)
Set re = new regexp
inputstrarray(0) = "Joe"
inputstrarray(1) = "Bill"
inputstrarray(2) = "James"
inputstrarray(3) = "Alex"
inputstrarray(4) = """" & "Google, Inc" & """"
inputstrarray(5) = "Rose"
inputstrarray(6) = """" & "Kickstarter, LLC" & """"
re.pattern = ",\s" 'pattern is a comma followed by a space
For Each strstr In inputstrarray 'loop through each string in the array
newstrstr = re.Replace(strstr, "_") 'replace the pattern with an underscore
If StrComp(strstr,newstrstr) Then
Wscript.Echo "Replace " & strstr & " with " & newstrstr
Else
Wscript.Echo "No Change"
End If
Next
Output looks like the following:
No Change
No Change
No Change
No Change
Replace "Google, Inc" with "Google_Inc"
No Change
Replace "Kickstarter, LLC" with "Kickstarter_LLC"
No Change

Resources