hi all! would just like to seek assistance... i am trying to open a pdf file in visual basic. if im trying to put it in as OLE it would only work if i have set the source file in the properties box. but if i put the source file in codes using app.path, it won't open. please help... thanks so much!:confused:

Dani AI

Generated

noted that an OLE object set at design time works but assigning the file at runtime (via App.Path) does not. rightly suggested an in-form browser approach and showed an OLE create example. The core points below cover reliable alternatives, why runtime OLE can fail, and quick troubleshooting steps.

A simple, robust solution for classic VB is to launch the PDF with the system default viewer (no embedding). This avoids OLE quirks and works on any machine with a PDF viewer installed:

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
     ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Const SW_SHOWNORMAL = 1

Private Sub Command1_Click()
    Dim p As String
    p = App.Path
    If Right$(p, 1) <> "\" Then p = p & "\"
    p = p & "document.pdf"
    If Dir$(p) = "" Then
        MsgBox "File not found: " & p, vbExclamation
        Exit Sub
    End If
    ShellExecute Me.hWnd, "open", p, vbNullString, vbNullString, SW_SHOWNORMAL
End Sub

For inline viewing, the WebBrowser control or an Adobe/third‑party PDF ActiveX control is the usual choice (the WebBrowser will host whatever PDF plugin the machine has). OLE container embedding is hit-or-miss because the installed PDF handler must expose an embeddable OLE server; if it does not, assigning SourceDoc at runtime will fail.

Troubleshooting checklist:

  • Confirm the exact full path (watch the trailing backslash). Use Dir$ to test existence.
  • When debugging, print App.Path to ensure it points where expected.
  • If embedding fails, prefer ShellExecute (external) or add an actual PDF ActiveX control to the form.
  • In VB.NET, use Process.Start(Path.Combine(Application.StartupPath, "document.pdf")) for equivalent behavior.

This keeps behavior predictable and avoids runtime-only OLE problems.

Recommended Answers

All 3 Replies

Hi ,

I am not sure whether you like to open within a form or outside the vb app. If you like to open the pdf inside the form you can place a web browser control inside the form and use the url as "c:\...\file.pdf".

Regards
Marikanna Narayanan

Hi ,

I am not sure whether you like to open within a form or outside the vb app. If you like to open the pdf inside the form you can place a web browser control inside the form and use the url as "c:\...\file.pdf".

Regards
Marikanna Narayanan

i am trying to open the pdf file in the command button of the form... i dunno if there is another way to open it aside from OLE control. thanks so much!:confused:

OLE1.OLETypeAllowed = acOLEEmbedded
OLE1.SourceDoc = "C:\1.pdf"
OLE1.Action = acOLECreateEmbed

Try with this one..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.