locked
How Open PDF AND DOCX at same time RRS feed

  • Question

  • Good day,

    I am trying to open a pdf as well as a docx from my program. Individually, it works, ie. if I just code for a pdf or code for docx, but when i try both at same time...no luck. can anyone help me understand this process please? thank you

     Try
                Dim x As Integer
                x = CInt(dgvScanAdd.CurrentRow.Cells(0).Value.ToString)
                'MsgBox("Folder " & x)
    
    
    Dim y as integer
    y= frmPatientList2.dgvPatient.CurrentRow.Cells("LastName").Value.ToString
    
    'm stands for miscellaneous and labels doc or pdf in folder; so there are are multiple files m1, m2, m3, etc which can be pdf or doc etc.
    
                'Finds pdf
                Dim Path = "\\Poseidon\My Book (F)\Laconia Scans\PatientScans\" & y & "\" & "m" & x & ".pdf"
                System.Diagnostics.Process.Start(Path)
    
                'Finds(docx)
                Dim Path2 = "\\Poseidon\My Book (F)\Laconia Scans\PatientScans\" & y & "\" & "m" & x & ".docx"
                System.Diagnostics.Process.Start(Path2)
    
    
            Catch ex As System.Exception
                MsgBox("Error. There is not a miscellaneous file associated with this ID")
            End Try


    Mike

    Thursday, June 7, 2018 6:42 PM

Answers

  • Hi

    I have to assume there are no exceptions thrown as you would mention the messagebox being displayed.

    A generic test here :

        Process.Start("C:\Users\lesha\Desktop\aaa.txt")
        Process.Start("C:\Users\lesha\Desktop\bbb.txt")
    

    works without any problem.

    If any of your files fails because of path inaccuracies or not existing then the Try...Catch would have show the messagebox. Try putting a break point of the second call to Process.Start and see if it opens the first file - then, Continue -see what happens.


    Regards Les, Livingston, Scotland

    • Marked as answer by mikedp Friday, June 8, 2018 2:47 PM
    Thursday, June 7, 2018 7:11 PM
  • I would recommend using If File.Exists for each file, does it exist? run it as Les recommended else show (for now) a message box indicating the file does not exists, repeat for the next file.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by mikedp Friday, June 8, 2018 2:47 PM
    Thursday, June 7, 2018 9:45 PM
  • Hi mikedp,

    I wrote a demo based on my understanding, but there is no problem with what you said. You can open both 1.pdf and 1.docx on the desktop. Please confirm whether your two paths are correct. Open both 1.pdf and 1.docx on the desktop. 

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim t As DataTable = New DataTable()
            t.Columns.Add("ID", GetType(Integer))
            t.Rows.Add(1)
            dgvScanAdd.DataSource = t
    
        End Sub
    
        Private Sub dgvScanAdd_Click(sender As Object, e As EventArgs) Handles dgvScanAdd.Click
            Dim x As Integer
            x = CInt(dgvScanAdd.CurrentRow.Cells(0).Value.ToString)
            Try
                Dim Path = "C:\Users\alexl2\Desktop\" & x & ".docx"
                System.Diagnostics.Process.Start(Path)
    
                Dim Path2 = "C:\Users\alexl2\Desktop\" & x & ".pdf"
                System.Diagnostics.Process.Start(Path2)
            Catch ex As Exception
                MessageBox.Show("123")
            End Try
        End Sub
    End Class
    

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by mikedp Friday, June 8, 2018 2:47 PM
    Friday, June 8, 2018 5:33 AM

All replies

  • Hi

    I have to assume there are no exceptions thrown as you would mention the messagebox being displayed.

    A generic test here :

        Process.Start("C:\Users\lesha\Desktop\aaa.txt")
        Process.Start("C:\Users\lesha\Desktop\bbb.txt")
    

    works without any problem.

    If any of your files fails because of path inaccuracies or not existing then the Try...Catch would have show the messagebox. Try putting a break point of the second call to Process.Start and see if it opens the first file - then, Continue -see what happens.


    Regards Les, Livingston, Scotland

    • Marked as answer by mikedp Friday, June 8, 2018 2:47 PM
    Thursday, June 7, 2018 7:11 PM
  • I would recommend using If File.Exists for each file, does it exist? run it as Les recommended else show (for now) a message box indicating the file does not exists, repeat for the next file.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by mikedp Friday, June 8, 2018 2:47 PM
    Thursday, June 7, 2018 9:45 PM
  • Hi mikedp,

    I wrote a demo based on my understanding, but there is no problem with what you said. You can open both 1.pdf and 1.docx on the desktop. Please confirm whether your two paths are correct. Open both 1.pdf and 1.docx on the desktop. 

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim t As DataTable = New DataTable()
            t.Columns.Add("ID", GetType(Integer))
            t.Rows.Add(1)
            dgvScanAdd.DataSource = t
    
        End Sub
    
        Private Sub dgvScanAdd_Click(sender As Object, e As EventArgs) Handles dgvScanAdd.Click
            Dim x As Integer
            x = CInt(dgvScanAdd.CurrentRow.Cells(0).Value.ToString)
            Try
                Dim Path = "C:\Users\alexl2\Desktop\" & x & ".docx"
                System.Diagnostics.Process.Start(Path)
    
                Dim Path2 = "C:\Users\alexl2\Desktop\" & x & ".pdf"
                System.Diagnostics.Process.Start(Path2)
            Catch ex As Exception
                MessageBox.Show("123")
            End Try
        End Sub
    End Class
    

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by mikedp Friday, June 8, 2018 2:47 PM
    Friday, June 8, 2018 5:33 AM
  • Thanks les, Karen, and Cherry,

    I incorporated all of your suggestions and this now works!

    I also added a "try/catch" (IF exists) after EACH search instead of one "try/catch" at the end of code.

    Try
                Dim Path = "\\Poseidon\My Book (F)\Laconia Scans\PatientScans\" & y & "\" & "m" & x & ".docx"
    
                '& "\" & "m" & "3" & ".docx"
                System.Diagnostics.Process.Start(Path)
    
            Catch ex As Exception
                'MessageBox.Show("no word doc")
            End Try
    
            Try
    
                Dim Path2 = "\\Poseidon\My Book (F)\Laconia Scans\PatientScans\" & y & "\" & "m" & x & ".pdf"
    
                System.Diagnostics.Process.Start(Path2)
    
            Catch ex As Exception
                'MessageBox.Show("no pdf doc")
            End Try


    Mike

    Friday, June 8, 2018 2:56 PM