locked
Open pdf/Word file on Button Click Vb.Net

    Question

  •  

    Hi,

           I have 2 queries

    1) How do we open a pdf file in vb.net on the click of a button

     

    2)  The below code opens a word file.How do i check if it is already open so as not open another instance of it

    appWord = New Word.Application

    docWord = New Word.Document

    appWord.Visible = True

    docWord = appWord.Documents.Open(file_name, , False, False, , , True)

    docWord.Activate()

     

    Thanks,

    Anand.

    Wednesday, January 09, 2008 6:50 AM

Answers

  • 1) How do we open a pdf file in vb.net on the click of a button

    Code Block

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Process.Start("D:\book.pdf") ' Approach 1

            WebBrowser1.Navigate("D:\book.pdf")  ' Approach 2

         AdobeReader.src = "D:\book.pdf ' Approach 3:  Add Reference to COM component: Adobe pdf Reader

        End Sub

    Reference: How to open pdf file in vb.net applicatin

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1771833&SiteID=1

     

    2)  The below code opens a word file.How do i check if it is already open so as not open another instance of it

     

    Using this code block:

    Code Block

          Dim p() As Process = Process.GetProcessesByName("WinWord")

            If p.Count = 0 Then

               ' No word instance opened

            Else

               ' Any Word instance opened

          End If

     

    Add Reference to COM component: Microsoft Word 12.0 Object Library

    Code Block

    Imports Word = Microsoft.Office.Interop.Word

    Imports System.Runtime.InteropServices

     

    Public Class Form1

        Dim oWord As Word.Application

        Dim oDoc As Word.Document

        Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph

       

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     

            Dim p() As Process = Process.GetProcessesByName("WinWord")

            If p.Count = 0 Then

                oWord = CreateObject("Word.Application")

                DoSomething()

                System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc)

                'oDoc.Close()

                oDoc = Nothing

                oWord.Application.Quit()

                oWord = Nothing

     

            Else

                oWord = Marshal.GetActiveObject("Word.Application")

                DoSomething()

                oDoc.Close()

                oDoc = Nothing

            End If

     

        End Sub

     

        Public Sub DoSomething()

            oWord.Visible = True

            oDoc = oWord.Documents.Add("E:\1.doc")

            oPara1 = oDoc.Content.Paragraphs.Add

            oPara1.Range.Text = "Heading 1"

            oPara1.Range.Font.Bold = True

            oPara1.Format.SpaceAfter = 24

            oPara1.Range.InsertParagraphAfter()

        End Sub

     End Class

     

    Detail sample: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2547110&SiteID=1

    Monday, January 14, 2008 2:13 PM
  • you need the Office PIA's in order to see them, and installing office would also help you to see it in the references...

    http://www.microsoft.com/DOWNLOADS/details.aspx?familyid=3C9A983A-AC14-4125-8BA0-D36D67E0F4AD&displaylang=en
    Need 2 be back @ MS - MS All the way! Follower since 1995 MS Super Evangelist| MSDN Forums Moderator
    • Proposed as answer by ahmedilyas Wednesday, March 04, 2009 7:29 PM
    • Marked as answer by Martin Xie - MSFT Friday, March 06, 2009 8:19 AM
    Wednesday, March 04, 2009 7:29 PM

All replies

  • 1) How do we open a pdf file in vb.net on the click of a button

    Code Block

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Process.Start("D:\book.pdf") ' Approach 1

            WebBrowser1.Navigate("D:\book.pdf")  ' Approach 2

         AdobeReader.src = "D:\book.pdf ' Approach 3:  Add Reference to COM component: Adobe pdf Reader

        End Sub

    Reference: How to open pdf file in vb.net applicatin

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1771833&SiteID=1

     

    2)  The below code opens a word file.How do i check if it is already open so as not open another instance of it

     

    Using this code block:

    Code Block

          Dim p() As Process = Process.GetProcessesByName("WinWord")

            If p.Count = 0 Then

               ' No word instance opened

            Else

               ' Any Word instance opened

          End If

     

    Add Reference to COM component: Microsoft Word 12.0 Object Library

    Code Block

    Imports Word = Microsoft.Office.Interop.Word

    Imports System.Runtime.InteropServices

     

    Public Class Form1

        Dim oWord As Word.Application

        Dim oDoc As Word.Document

        Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph

       

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     

            Dim p() As Process = Process.GetProcessesByName("WinWord")

            If p.Count = 0 Then

                oWord = CreateObject("Word.Application")

                DoSomething()

                System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc)

                'oDoc.Close()

                oDoc = Nothing

                oWord.Application.Quit()

                oWord = Nothing

     

            Else

                oWord = Marshal.GetActiveObject("Word.Application")

                DoSomething()

                oDoc.Close()

                oDoc = Nothing

            End If

     

        End Sub

     

        Public Sub DoSomething()

            oWord.Visible = True

            oDoc = oWord.Documents.Add("E:\1.doc")

            oPara1 = oDoc.Content.Paragraphs.Add

            oPara1.Range.Text = "Heading 1"

            oPara1.Range.Font.Bold = True

            oPara1.Format.SpaceAfter = 24

            oPara1.Range.InsertParagraphAfter()

        End Sub

     End Class

     

    Detail sample: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2547110&SiteID=1

    Monday, January 14, 2008 2:13 PM
  • Thanks...

     

    Wednesday, January 23, 2008 8:34 AM
  • Hello,

    I have recently downloaded VB express and have installed the MSDN libraries.  I do not see the COM component "Microsoft Word 12.0 Object Library" when attempting to add that reference to my project.

    I do not have MS Word installed on this computer, could this be the reason?

    Thank you,

    dan
    Wednesday, March 04, 2009 5:35 PM
  • you need the Office PIA's in order to see them, and installing office would also help you to see it in the references...

    http://www.microsoft.com/DOWNLOADS/details.aspx?familyid=3C9A983A-AC14-4125-8BA0-D36D67E0F4AD&displaylang=en
    Need 2 be back @ MS - MS All the way! Follower since 1995 MS Super Evangelist| MSDN Forums Moderator
    • Proposed as answer by ahmedilyas Wednesday, March 04, 2009 7:29 PM
    • Marked as answer by Martin Xie - MSFT Friday, March 06, 2009 8:19 AM
    Wednesday, March 04, 2009 7:29 PM
  • Thank you ahmedilyas for your friendly help. ahmedilyas is right.

    http://msdn.microsoft.com/en-us/library/15s06t57.aspx
    To use the features of a Microsoft Office application from .NET project, you must use the primary interop assembly for the application. The primary interop assembly enables managed code to interact with a Microsoft Office application's COM-based object model.


    How to: Install Office Primary Interop Assemblies.
    You can install the complete set of primary interop assemblies in the global assembly cache in two ways:

    • Perform a Complete installation of Microsoft Office.

    • Install them from the redistributable primary interop assemblies package.


    Best regards,
    Martin Xie

    Friday, March 06, 2009 8:39 AM
  • AoA

    Please tell me how to load a list of the word documents that are currently open in the task bar? how to do it vb.net/c#??

    Thanks in Advance 

    Asad Naeem
    Thursday, July 30, 2009 4:30 PM
  • Hi Asad,

    Here is code sample: Load a list of the word documents (that are currently open in the task bar) into ListBox1.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim pro As Process = Process.GetCurrentProcess

            Dim wordProes As Process() = Process.GetProcessesByName("WINWORD")

            For Each pro In wordProes

                ListBox1.Items.Add(pro.ProcessName & vbTab & pro.MainWindowTitle)

            Next

            MessageBox.Show("Total Word document " & wordProes.Length)

        End Sub

     
    Best regards,
    Martin Xie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback on our support, please contact msdnmg@microsoft.com
    Friday, July 31, 2009 3:44 AM