View PDF from selected listbox

Ralevam

New Member
Joined
Mar 23, 2019
Messages
9
I'm using a multiple page form and I'm looking for code to view the selected file from my list box in the AcroPDF1 frame. I'd also like to print PDF file.
I'm trying to insert an image of my form to view but I'm unable to do so any suggestions?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Enter the VBA menu, tools, references and check that you have these:

5c420fc728c3673c6997b6de66e88380.jpg



Try this:

Code:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
dim wPath as String, wName as String
    wPath = "C:\files\pdf\
    if right(wPath, 1) <> "\" then wPath = wPath & "\"
    wName = ListBox1.List(ListBox1.ListIndex)

    if lcase(right(wName, 4)) <> ".pdf" then wName = wName & ".pdf"


    UserForm1.WebBrowser1.Navigate wPath & wName
End Sub

eg.

028cc6d4d23a0884b9e0ac5fcebd0186.jpg
 
Upvote 0
Thanks fort getting back to me i really appreciate your time.
I'm new to VBA and don't have much experience.
Could you look at my code and tell me where I should insert the code you provided.
I could also email you my file if that would help.
Thanks again for you time!

Dim Mode As Integer


Private Sub cboTool_Change()
On Error Resume Next


myImg = Me.cboTool.List(Me.cboTool.ListIndex, 2)
Me.Image1.Picture = LoadPicture(myImg)


End Sub


Private Sub cboProduct_AfterUpdate()
On Error Resume Next
Dim ws As Worksheet
Dim cPart As Range
Set ws = Worksheets("LookupLists")


Me.cboTool.Value = ""
Me.cboTool.RowSource = ""


With ws
.Range("CritPartCat").Cells(2, 1).Value _
= Me.cboProduct.Value
.Columns("A:D").AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=.Range("CritPartCat"), _
CopyToRange:=.Range("ExtPartDesc"), _
Unique:=False
End With


'redefine the static named range
ThisWorkbook.Names.Add Name:="PartSelList", _
RefersTo:="=" & ws.Name & "!" & _
ws.Range("PartSelCatList").Address


Me.cboTool.RowSource = "PartSelCatList"


End Sub
Private Sub cmdAdd_Click()
Dim lRow As Long
Dim lPart As Long
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
'revised code to avoid problems with
'Excel lists and tables in newer versions
'find first empty row in database
''lRow = ws.Cells(Rows.Count, 1) _
'' .End(xlUp).Offset(1, 0).Row
lRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

lPart = Me.cboTool.ListIndex


If cboOperator.ListIndex = -1 Then
Cancel = 1
MsgBox "Operator Not Selected"
cboOperator.SetFocus
Exit Sub
End If


'check for a part number
If Trim(Me.cboTool.Value) = "" Then
Me.cboTool.SetFocus
MsgBox "Product Not Selected"
Exit Sub
End If


If cboMachine.ListIndex = -1 Then
Cancel = 1
MsgBox "Machine Not Selected"
cboMachine.SetFocus
Exit Sub
End If


If cboTool.ListIndex = -1 Then
Cancel = 1
MsgBox "Tool Not Selected"
cboTool.SetFocus
Exit Sub
End If


If cboReason.ListIndex = -1 Then
Cancel = 1
MsgBox "Reason Not Selected"
cboReason.SetFocus
Exit Sub
End If


If txtLife = "" Then
Me.txtLife.SetFocus
MsgBox "Tool Life Not Entered"
Exit Sub
End If


'copy the data to the database
With ws
.Cells(lRow, 1).Value = Me.cboTool.Value
.Cells(lRow, 2).Value = Me.cboProduct.Value
.Cells(lRow, 3).Value = Me.cboTool.List(lPart, 1)
.Cells(lRow, 4).Value = Me.cboMachine.Value
.Cells(lRow, 5).Value = Me.cboReason.Value
.Cells(lRow, 6).Value = Me.cboOperator.Value
.Cells(lRow, 7).Value = Me.txtDate.Value
.Cells(lRow, 8).Value = Me.txtTime.Value
.Cells(lRow, 9).Value = Me.txtLife.Value
End With


'clear the data
'ClearParts
Me.cboProduct.Value = ""
Me.cboReason.Value = ""
Me.cboOperator.Value = ""
Me.txtLife.Value = ""
Me.cboTool.Value = ""
Me.cboTool.RowSource = ""
Me.Image1.Picture = LoadPicture("")
Me.cboMachine.Value = ""
Me.txtDate.Value = Date
Me.txtTime.Value = Time()
Me.cboProduct.SetFocus


ThisWorkbook.Save


Unload Me


End Sub


Private Sub cmdClose_Click()
Unload Me
End Sub




Private Sub cmdReset_Click()
Dim iControl As control


For Each iControl In Me.Controls
If iControl.Name Like "cbo*" Then iControl = vbNullString
If iControl.Name Like "txtLife*" Then iControl = vbNullString
Next


Set ws = Worksheets("LookupLists")
With ws
.Range("CritPartCat").Cells(2, 1).ClearContents
.Range("PartSelList").ClearContents

End With


End Sub


Private Sub Image1_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, ByVal DragState As MSForms.fmDragState, ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)


End Sub


Private Sub UserForm_Initialize()
Dim cType As Range
Dim cPart As Range
Dim cLoc As Range
Dim ws As Worksheet
Set ws = Worksheets("LookupLists")


With ws
.Range("CritPartCat").Cells(2, 1).ClearContents
.Range("PartSelList").ClearContents

End With


For Each cType In ws.Range("PartCatList")
With Me.cboProduct
.AddItem cType.Value
End With
Next cType


For Each cLoc In ws.Range("MachineList")
With Me.cboMachine
.AddItem cLoc.Value
End With
Next cLoc


Me.cboTool.RowSource = ""


Me.txtDate.Value = Date
Me.txtTime.Value = Time()
Me.cboProduct.SetFocus


End Sub
 
Upvote 0
Check Microsoft Internet Controls.
5c420fc728c3673c6997b6de66e88380.jpg


In tools Add Microsoft Web Browser:

c12470aa75d6487d6193eb5ab98f678d.jpg


Just add this code to the end of your code.
Change ListBox1 by the name of your listbox
Change C:\files\pdf\ by the path of your folder

In the list of your listbox you must have the file name of the pdf.


Code:
Private Sub [COLOR=#ff0000]ListBox1_DblClick[/COLOR](ByVal Cancel As MSForms.ReturnBoolean)
dim wPath as String, wName as String
    wPath = "[COLOR=#ff0000]C:\files\pdf\[/COLOR]"
    if right(wPath, 1) <> "\" then wPath = wPath & "\"
    wName = [COLOR=#ff0000]ListBox1[/COLOR].List(ListBox1.ListIndex)


    if lcase(right(wName, 4)) <> ".pdf" then wName = wName & ".pdf"




    UserForm1.WebBrowser1.Navigate wPath & wName
End Sub
 
Upvote 0
I'm getting this error:

Run-time error '2110':

Can't move focus to the control because it is invisible, not enabled, or
of a type that does not accept the focus.
 
Upvote 0
Try everything in a new userform, just put in the userform a listbox and a WebBrowser1 controls
 
Upvote 0
Dante,
It works perfect! I'm using a multiple page form I probably need to make Page2 active. How would I go about this?
 
Upvote 0
MultiPage1.Value=0 selects your 1st Multipage
MultiPage1.Value=1 selects your 2nd
 
Upvote 0

Forum statistics

Threads
1,223,959
Messages
6,175,645
Members
452,663
Latest member
MEMEH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top