Results 1 to 2 of 2

Thread: [RESOLVED] Convert VBA to VB.NET

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2016
    Posts
    27

    Resolved [RESOLVED] Convert VBA to VB.NET

    I regularly use some VBA code within an Excel file to convert a chemical CAS number to a SMILES string. I didn't write it and all credit goes to the ChemCell team for coming up with it. My issue is that I would like to do same thing within a .NET app using VB.

    Can anyone advise on how to modify the following code so that it gives me the same functionality without using Excel.

    Code:
    Private Function strip(ByVal str As String) As String
      Dim last
      
      For i = 1 To Len(str) Step 1
        If Asc(Mid(str, i, 1)) < 33 Then
          last = i
        End If
      Next i
      
      If last > 0 Then
        strip = Mid(str, 1, last - 1)
      Else
        strip = str
      End If
    End Function
    
    Public Function getSMILES(ByVal name As String) As String
      Dim XMLhttp: Set XMLhttp = CreateObject("MSXML2.ServerXMLHTTP")
      XMLhttp.setTimeouts 2000, 2000, 2000, 2000
      XMLhttp.Open "GET", "http://cactus.nci.nih.gov/chemical/structure/" + name + "/smiles", False
      XMLhttp.send
    
      If XMLhttp.Status = 200 Then
        getSMILES = strip(XMLhttp.responsetext)
      Else
        getSMILES = ""
      End If
    End Function
    Public Function getInChIKey(ByVal name As String) As String
      Dim XMLhttp: Set XMLhttp = CreateObject("MSXML2.ServerXMLHTTP")
      XMLhttp.setTimeouts 1000, 1000, 1000, 1000
      XMLhttp.Open "GET", "http://cactus.nci.nih.gov/chemical/structure/" + name + "/stdinchikey", False
      XMLhttp.send
    
      If XMLhttp.Status = 200 Then
        getInChIKey = Mid(strip(XMLhttp.responsetext), 10)
      Else
        getInChIKey = ""
      End If
    End Function
    To explain how this works, I enter =getSmiles(cellreference) into a cell and it returns the SMILES string generated from the cactus site.

    I would like to achieve the same thing by simply entering text into a textbox.

    Thanks in advance for any thoughts on the topic.

    Steve

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    Re: Convert VBA to VB.NET

    Alternatives for XMLhttp in VB.NET are WebRequest class and WebClient class.

    As for the string functions, here's the link VB.NET String Functions
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width