tanvi
tanvi

Reputation: 628

Custom Paths in Web Browser Control for PDFs in MS Access

I want to display PDF files for each separate record on my form in Access. The form is populated from a combo box's result (select a key field from combo box), and form gets populated with a large amount of info from a query.

For each unique value in the underlying table, there is a pdf file related to it. I want to use the web browser control to define the file path for each unique key. For example,

S:\FolderPath\xyz\keyvalue.pdf"

where keyvalue.pdf is literally the contents of a text box on the form that has the text field source for that record followed by a '.pdf' so if my key is 1, it will be 1.pdf, if it is A-22ds, the file will be A-22ds.pdf.

Upvotes: 0

Views: 1180

Answers (1)

Erik A
Erik A

Reputation: 32682

Let's say you have a form with a web browser control called MyWebbrowserControl, and you want to get the PDF in there:

Public Sub LoadPDF(MyPdfLocation As String)
    Dim wb As Object
    Dim strHTML As String
    strHTML = "<html><head><title>My PDF</title></head><body>" & _
    "<embed style=""width:100%;height:100%;"" src=""" & Application.HtmlEncode(MyPdfLocation) & """ type=""application/pdf""></embed></body></html>"
    Set wb = MyWebbrowserControl.Object
    With wb
        .Navigate2 "about:blank"
        Do Until .ReadyState = 4 '=READYSTATE_COMPLETE
            'This is an inefficient way to wait, but loading a blank page should only take a couple of milliseconds
            DoEvents
        Loop
        .Document.Open
        .Document.Write strHTML 
        .Document.Close
    End With
End Sub

(I'm using very generic HTML, it might benefit from some tweaking).

Them call it on Form_Current, for example:

Private Sub Form_Current()
    Dim path As String 
    Dim filename As String 
    path = "S:\MDM\MIRS\SCOPE-MP\" 
    filename = Me.cboPSENumber.Value + "_MP.pdf" 'Final path should be path + filename 
    Me.LoadPDF path & filename
End Sub

Upvotes: 1

Related Questions