Solved

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

Posted on 2004-08-24
7
1,569 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now