?
Solved

Underscore in Visual Basic

Posted on 2010-09-04
11
Medium Priority
?
1,136 Views
Last Modified: 2012-05-10
I am writing a script that needs to find the string "ox_shipaddress" within text. I can see the string in the text but the script does not find the string. So when I use:

if instr (body.innertext, "ox_shipaddress") then ...

it skips the if statement because it is not locating  "ox_shipaddress" . I am sure this is because of the underscore in  "ox_shipaddress" . Can somebody please tell me how to rewrite the script to locate the string?
0
Comment
Question by:bpfsr
11 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33603406
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 33603409
The underscore itself should not pose a problem as you have used it.  It is treated like a normal character in a string, so if "ox_shipaddress" is in body.innertext it will locate it.

I suspect the problem is that the string in body.innertext isn't what you think it is.  Perhaps the underscore is a different character?  Or there is a space around it?  

Can you paste up what is in body.innertext when the test fails?

~bp
0
 
LVL 8

Expert Comment

by:spinzr0
ID: 33603420
You can also just loop through body.innertext to see what character the _ is by using this:

For i=1 To Len(body.innertext)
    Msgbox Asc(Mid(body.innertext, i, 1)
Next

Additionally, just to make sure its not case sensativity you should use this:
If InStr (1, body.innertext, "ox_shipaddress", vbTextCompare) Then
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33603422
If this is VB6 or VBA, then the underscore is NOT your problem.  For example:MsgBox instr(1,"blah_blah_hee blah blah", "blah_hee")will return 6, as expected.More likely, either the string being sought is not truly there, or you are using a case-sensitive search (by default, InStr is case sensitive, unless you use vbTextCompare in an optional argument, or have a module declaration of Option Compare Text.Perhaps you should insert this line before the one above, and see what is written to the Immediate window:Debug.Print "|" & body.innertext & "|"
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33603433
Slow fingers :)
0
 

Author Comment

by:bpfsr
ID: 33604163
Thanks for the help; here is the paragraph from the source document:

      </div>
    </form>
   <table>
    <tr>
     <td valign="top">
      <form  method="post" action="/gp/checkout/address/edit.html/ref=ox_shipaddress_edit_addr_1" id="edit-joqkltnomp">
        <input type="hidden" name="addressID" value="joqkltnomp" />
        <input type="hidden" name="changeAddressOrderID" value="" />
        <input type="hidden" name="useAddressWidget" value="1" />

and here is the snippet from the script:

If InStr(Body.innerText, "ox_shipaddress_edit_addr_1") Then

I actually copied and pasted the phrase.
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33604264
Yes, and what was the value of the Body.innerText at the time?
0
 

Author Comment

by:bpfsr
ID: 33604504
When I hover over it it shows "Select a shipping address..." Is there a way to select the entire content?
0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 33605005
See above, with Debug.Print
0
 

Author Comment

by:bpfsr
ID: 33605125
right, sorry. Okay, that seems to be it. The phrase I am looking for is part of the html source code for an Internet Explorer page, while whats being outputted in the Immediate Screen is the output of the html, i.e. the IE page itself, sans the source code. I'm guess then that is the function of body.innertext. Is there then a function that would search the source code?
0
 

Author Comment

by:bpfsr
ID: 33605220
got it - body.innerHTML
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question