Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML and VB 2

Status
Not open for further replies.

cyberprof

Programmer
Jun 10, 2003
229
0
0
GB
I want to include a web page I've developed in my VB program. I know how to use the Internet Controls to link to the htm files, but this file can be edited and change the result of the program.

Is there anyway of using a HTML files and lock it in the VB program so it cannot be deleted or edited.

Thanks

J

 
J,

Don't know if this is what you want but I used to build a (data dependant) changing html file on the fly (at runtime). Nobody can alter it as the file is written at runntime and then executed. For example:

'/////////////
Dim FILENAME As String
FILENAME = "C:\REFUND1.HTML"

Close #1 ' just in case it was left open

Open MYHTMLFILE For Output As #1


Print #1, "<html>"
Print #1, "<BODY>"
Print #1, "HELLO THERE"
Print #1, "</html>"
Print #1, "</BODY>"

close #1


WebBrowser1.Navigate MYHTMLFILE
'/////////////


Hope this helps


Dan
 
Thanks...

When I run the program I get this error,

Runtime Error 75

Path/File Access Error

On this line 'Open MYHTMLFILE For Output As #1'
 
Sorry J,

I had the DIM filename wrong (it was filename. Change it to
MYHTMLFILE and change the statement to filename = "C:\REFUND1.HTML" to MYHTMLFILE = "C:\REFUND1.HTML"
I changed it in the code below and it works now.


Enjoy

Dan


'/////////////
Dim MYHTMLFILE As String
MYHTMLFILE = "C:\REFUND1.HTML"

Close #1 ' just in case it was left open

Open MYHTMLFILE For Output As #1


Print #1, "<html>"
Print #1, "<BODY>"
Print #1, "HELLO THERE"
Print #1, "</html>"
Print #1, "</BODY>"

close #1


WebBrowser1.Navigate MYHTMLFILE
'/////////////


 
J,

I Forgot to mention, When you open the file as shown above (for writing) it dumps the contents of the exisitng file. So even if sombody changes it , it wont matter since the VB app will rewrite it. Change the filepath to anything you want.
 
Code:
Private Sub Form_Load()
  WebBrowser1.Navigate "about:blank"
  WebBrowser1.Document.Write "<html><BODY>HELLO THERE</html></BODY>"
End Sub


or if you want to build it first, something like this...
Code:
Private Sub Form_Load()
  Dim Site As String
  Site = Site & "<html>" & vbCrLf
  Site = Site & "  <BODY>" & vbCrLf
  Site = Site & "    HELLO THERE" & vbCrLf
  Site = Site & "  </html>" & vbCrLf
  Site = Site & "</BODY>" & vbCrLf
  WebBrowser1.Navigate "about:blank"
  WebBrowser1.Document.Write Site
End Sub

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
That Looks good to Cube. Hows the Christmass tree business?
Smile dan
 
Sales aren't going so good...

It's like I'm GIVING them away ;-)

People seem to like them though :)

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Thanks for the second example, excellent code.

How could I manage images, do I have to link to the as normal <img src="pic.jpg"> or could I use images from an ImageList.
 
not sure...

I think you have to use a file with the src attribute... [sad]

Though... you can change the pic from vb if you give the tag a name or id...

such as: <img id="ChangeMe" src="pic.jpg">

then you can use the DOM to change the src...


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Sorry about this, just another quick question on this subject.

Hyperlinks, can I link to another page, that has to be created dynamically. I can't imagine you can?
 
Having problems.

Loading up the first peice of dynamic HTML is fine, but when I what to view new HTML, the page is blank, even though the HTML string is correct.

Is there a clear/refresh command that needs to be used before a new page can be displayed in the webbrowser control.

Cheers

J
 
>> (Q1) Hyperlinks, can I link to another page, that has to be created dynamically. I can't imagine you can?

Sure you can... :)

>> (Q2) Is there a clear/refresh command that needs to be used before a new page can be displayed in the webbrowser control.

Yes... You have to wait for the ready state, see below...

OK...

1 Form (Form1)
1 WebBrowser (wb1)
1 CommandButton (Command1)

this code:
Code:
Const siteChoose = "<body><a href='a'>Test A</a><br/><a href='b'>Test B</a></body>"
Const siteA = "<body>You selected Site A</body>"
Const siteB = "<body>You selected Site B</body>"

Private Sub Command1_Click()
[b]  'Reset[/b]
  Form_Load
End Sub

Private Sub Form_Load()
  wb1.Navigate2 "about:blank"
  WaitForLoad
  wb1.Document.write siteChoose
End Sub

Private Sub wb1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
[b]  '(Answer to: Q1)[/b]
  If URL <> "about:blank" Then
    wb1.Navigate2 "about:blank"
    WaitForLoad
    Select Case LCase(URL)
    Case "about:blanka"         'From link "a"
      wb1.Document.write siteA
      Cancel = True
    Case "about:blankb"         'From link "b"
      wb1.Document.write siteB
      Cancel = True
    End Select
  End If
End Sub

Sub WaitForLoad()
[b]  '(Answer to: Q2)[/b]
  Do: DoEvents: Loop Until wb1.ReadyState = READYSTATE_COMPLETE
End Sub

Hope This Helps...

Enjoy ;-)
-Josh



PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
you can even set up the constants like this and switch between them if you want...
Code:
Const siteChoose = "<body><a href='a'>Test A</a><br/><a href='b'>Test B</a></body>"
Const siteA = "<body>You selected Site A<br/><a href='b'>Test B</a></body>"
Const siteB = "<body>You selected Site B<br/><a href='a'>Test A</a></body>"


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
or...

if you prefer to use functions instead of constants:
Code:
Private Sub Command1_Click()
  'Reset
  Form_Load
End Sub

Private Sub Form_Load()
  wb1.Navigate2 "about:blank"
  WaitForLoad
  wb1.Document.write siteChoose
End Sub

Private Sub wb1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
  If URL <> "about:blank" Then
    wb1.Navigate2 "about:blank"
    WaitForLoad
    Select Case LCase(URL)
    Case "about:blanka"
      wb1.Document.write siteA
      Cancel = True
    Case "about:blankb"
      wb1.Document.write siteB
      Cancel = True
    End Select
  End If
End Sub

Sub WaitForLoad()
  Do: DoEvents: Loop Until wb1.ReadyState = READYSTATE_COMPLETE
End Sub

Function siteChoose()
  Dim Temp As String
  Temp = Temp & "<body>"
  Temp = Temp & "  <a href='a'>Test A</a><br/>"
  Temp = Temp & "  <a href='b'>Test B</a>"
  Temp = Temp & "</body>"
  siteChoose = Temp
End Function

Function siteA()
  Dim Temp As String
  Temp = Temp & "<body>"
  Temp = Temp & "  You selected Site A<br/>"
  Temp = Temp & "  <a href='b'>Test B</a>"
  Temp = Temp & "</body>"
  siteA = Temp
End Function

Function siteB()
  Dim Temp As String
  Temp = Temp & "<body>"
  Temp = Temp & "  You selected Site A<br/>"
  Temp = Temp & "  <a href='a'>Test A</a>"
  Temp = Temp & "</body>"
  siteB = Temp
End Function

These are a little more flexible, and you can set them up to be more dynamic... ;-)


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
arghhhh... copy/paste typo...

Code:
Function siteB()
  Dim Temp As String
  Temp = Temp & "<body>"
  Temp = Temp & "  You selected Site [COLOR=red][b]B[/b][/color]<br/>"
  Temp = Temp & "  <a href='a'>Test A</a>"
  Temp = Temp & "</body>"
  siteB = Temp
End Function


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
just keeps gettin better ;-)

Here is a sub I just threw together to create a function from an html page (or any text file for that matter)...
Code:
Sub HtmlToFunction(File As String)
  Dim HtmlLines() As String, TempOut As String
  Dim fPath As String, fName As String, fTemp() As String
  Dim i As Integer
  If Len(Dir(File)) Then   'File Exists?
    'Get html text...
    Open File For Input As #1
      HtmlLines = Split(Input(LOF(1), #1) & vbCrLf, vbCrLf)  'Split File into seperate lines...
    Close
    'Get Name and path of file... (Yes there are easier ways, probably using File System Objects)
    fTemp = Split("\" & File, "\")
    fName = fTemp(UBound(fTemp))  'C:\path\file.html --> file.html
    Path = Left(File, Len(File) - Len(fName))  'C:\path\file.html --> C:\path\
    fTemp = Split("." & File, ".")
    fName = Replace(fName, ("." & fTemp(UBound(fTemp))), "")  'file.html --> file
    'Create Function Header...
    TempOut = "Function " & fName & "()" & vbCrLf
    TempOut = TempOut & "  Dim Temp As String" & vbCrLf
    'Create Function Content...
    For i = 0 To UBound(HtmlLines)
      If Len(Trim(HtmlLines(i))) Then  'Not a blank line?
        TempOut = TempOut & "  Temp = Temp & """ & HtmlLines(i) & """ & vbCrLf" & vbCrLf
      End If
    Next
    'Create Function Footer...
    TempOut = TempOut & "  " & fName & " = Temp" & vbCrLf
    TempOut = TempOut & "End Function" & vbCrLf
    'Save it to a file...
    Open Path & fName & ".sub" For Output As #1
      Print #1, TempOut
    Close
  End If
End Sub

When used like this:
Code:
Private Sub Command2_Click()
  HtmlToFunction "C:\Path\TestSite.html"
End Sub

It takes this (C:\Path\TestSite.html):
Code:
<html>
  <body>
    This is a test to convert html to a vb function.<br/>
    I repeat. This is only a test
  </body>
</html>

And creates this (C:\Path\TestSite.sub):
Code:
Function TestSite()
  Dim Temp As String
  Temp = Temp & "<html>" & vbCrLf
  Temp = Temp & "  <body>" & vbCrLf
  Temp = Temp & "    This is a test to convert html to a vb function.<br/>" & vbCrLf
  Temp = Temp & "    I repeat. This is only a test" & vbCrLf
  Temp = Temp & "  </body>" & vbCrLf
  Temp = Temp & "</html>" & vbCrLf
  TestSite = Temp
End Function

So you can just copy/paste into your program for use with the methods listed above...

Have Fun ;-)


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top