[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1660
  • Last Modified:

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


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
trzesniakj
Asked:
trzesniakj
  • 3
  • 2
1 Solution
 
Daniellus83Commented:
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
 
Daniellus83Commented:
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
 
trzesniakjAuthor Commented:
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
 
Daniellus83Commented:
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
 
trzesniakjAuthor Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now