Solved

How do you do a FILE > SAVE AS with IE from VB.NET

Posted on 2004-08-24
7
1,585 Views
Last Modified: 2008-01-09

I have code doing the following...

    Private WithEvents IE As SHDocVw.InternetExplorer

        If TextBox1.Text <> "" Then
            If IE Is Nothing Then
                IE = New SHDocVw.InternetExplorer
            End If

            IE.Visible = True
            IE.Navigate(TextBox1.Text)

            Do While IE.Busy
                Application.DoEvents()
            Loop

            ' page finished loading...
            ' do something in here

        End If

I have other code doing something with IE after the page is loaded.  It all works fine.

But I want to programatically perform the FILE > SAVE AS functionality as if you did it with the mouse interactively.  

How can I do a FILE > SAVE AS (including specifying a SAVE AS file type) with an IE object as I have it in the code above ?

Thanks,

John

0
Comment
Question by:trzesniakj
  • 3
  • 2
7 Comments
 
LVL 4

Expert Comment

by:Daniellus83
ID: 11887801
Okay; there are two things:

> 1. opening/setting the filename of the file you wanna do the save-ass operation to
> 2. get the information from the loaded html document and save it 'as' file...

(use also this link:  http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20953741.html )

ad 1.:

        Dim number As Integer = FreeFile()
        Dim FileName As String

        If SaveFile.ShowDialog() = DialogResult.OK Then

            FileName = SaveFile.FileName & ".html"
            FileOpen(nummer, FileName, OpenMode.Output) ' Open file for output.
           
            WriteLine(number , "your text")

            FileClose(nummer)
        End If


ad 2.: use this VB-code:

   Private Sub Form_Load()
      WebBrowser1.Navigate "www.google.com"
   End Sub
   
   Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
      Dim sHead As String, sBody As String
      sHead = WebBrowser1.Document.All.tags("HEAD").Item(0).outerhtml
      sBody = WebBrowser1.Document.body.outerhtml
      Debug.Print "<HTML>" & sHead & sBody & "</HTML>"
   End Sub


Now, this combined with your code should be:

------------------------------------------------------------------------------------------------------
    Private WithEvents IE As SHDocVw.InternetExplorer

        If TextBox1.Text <> "" Then
            If IE Is Nothing Then
                IE = New SHDocVw.InternetExplorer
            End If

            IE.Visible = True
            IE.Navigate(TextBox1.Text)

            Do While IE.Busy
                Application.DoEvents()
            Loop
 
         '--------------------------------------------------------------
            Dim number As Integer = FreeFile()
            Dim FileName As String
            Dim sHead As String, sBody As String


            If SaveFile.ShowDialog() = DialogResult.OK Then

                FileName = SaveFile.FileName & ".html"
                FileOpen(nummer, FileName, OpenMode.Output) ' Open file for output.
           
                     sHead = IE.Document.All.tags("HEAD").Item(0).outerhtml
                     sBody =  IE.Document.body.outerhtml

                 WriteLine(number , "<HTML>" & sHead & sBody & "</HTML>")
 
                 FileClose(nummer)
             End If

        End If
------------------------------------------------------------------------------------------------------

Warning: you have to add a 'SaveFileDialog' to your Form (and call it "SaveFile") to get this working.
0
 
LVL 4

Expert Comment

by:Daniellus83
ID: 11887848
sorry; forgot to replace some of varnames. Here is the corrected code:

------------------------------------------------------------------------------------------------------
    Private WithEvents IE As SHDocVw.InternetExplorer

        If TextBox1.Text <> "" Then
            If IE Is Nothing Then
                IE = New SHDocVw.InternetExplorer
            End If

            IE.Visible = True
            IE.Navigate(TextBox1.Text)

            Do While IE.Busy
                Application.DoEvents()
            Loop
 
         '--------------------------------------------------------------
            Dim number As Integer = FreeFile()
            Dim FileName As String
            Dim sHead As String, sBody As String


            If SaveFile.ShowDialog() = DialogResult.OK Then

                FileName = SaveFile.FileName & ".html"
                FileOpen(number, FileName, OpenMode.Output) ' Open file for output.
           
                     sHead = IE.Document.All.tags("HEAD").Item(0).outerhtml    '<< This code might be different
                     sBody =  IE.Document.body.outerhtml                                '<< This code might be different

                 WriteLine(number , "<HTML>" & sHead & sBody & "</HTML>")
 
                 FileClose(number)
             End If

        End If
------------------------------------------------------------------------------------------------------
0
 

Author Comment

by:trzesniakj
ID: 11888234
No.  You're not quite understanding what I want to do.  My original request had it correct.

To clarify...I do not want my VB.APP to do the saving (although your code is perfectly fine to do that).  I want to use IE's save as capabilities.

So I'll ask again...I want to programatically perform the FILE > SAVE AS functionality as if you did it with the mouse interactively.  How can I do a FILE > SAVE AS (including specifying a SAVE AS file type) with an IE object as I have it in the code above ?

Thanks,

John
0
 
LVL 4

Accepted Solution

by:
Daniellus83 earned 500 total points
ID: 11890125
Okay; the answer to that is:

 you cannot do this only with this component!  (check this: http://www.experts-exchange.com/Community_Support/help.jsp#hi54)

Daniël
0
 

Author Comment

by:trzesniakj
ID: 11891693
Thank you for taking the time to reply.  I appreciate the effort.

I'd like to know if anyone else has a potential solution to this.

Thanks,

John
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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