Opening a PDF using VBA and Shell

Kemidan2014

Board Regular
Joined
Apr 4, 2022
Messages
229
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Dim shell As Object
Set shell = CreateObject("Wscript.Shell")
Dim path As String

Set path = "O:\2 - QC Data Tracking System\Database Tables\brokepinsheets\" & Me.Program & " " & Me.Product & ".pdf"

  If Me.Defect = "Broke Pin" Then
   shell.Run "path"
  End If

I am attempting to create a simple code to open a PDF but i am getting a compile error saying "Object required" then blue highlights ".product" in blue I tried changing concatenation but it went but my path string went red on me.

any suggestions?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Check your spelling? Whatever Me.Product is, it isn't being resolved hence the error.
Are you using Option Explicit at the top of all of your modules? I suspect not because if you were and you compiled your code you'd know right away that .Product would be an issue instead of that happening at run time.
 
Upvote 0
I am now =)
i did check the spelling, there was no spaces or anything in the name.
1695816703200.png


however I ALSO clicked "All" tab and looked at the "Name" and it was reading "cboProd" so I copy pasted that into code instead and I still get the same compile error compiling it. it cant be the fact that original author was using 2 seperate tables for each feild to create the drop down list for the form.
 
Upvote 0
OOOOO I cant believe i missed this, I needed to Not put the word Set in front of Path, when i compiled it, the error went away. Now for the next hurdle that did not show up with compiling.

I tested the code and the following error occurred
1695823086881.png

VBA Code:
Dim shell As Object
Set shell = CreateObject("WScript.Shell")
Dim path As String

path = "O:\2 - QC Data Tracking System\Database tables\brokepinsheets\" & Me.Program & " " & Me.Product & ".pdf"

  If Me.Defect = "Broke Pin" Then
   shell.Run "path", vbNormal, True
  End If
    
    DoCmd.GoToRecord , "", acNext
    DoCmd.OpenQuery "prodIDupdate", acViewNormal, acEdit
    DoCmd.Close acQuery, "prodIDupdate"
    DoCmd.GoToRecord , "", acPrevious
    DoCmd.OpenReport "Sort Sheet", acViewReport, "", "[IDNum]=[Forms]![NotesfrmSortsheet]![IDNum]", acNormal


Command206_Click_Exit:
    Exit Sub
 
Upvote 0
Yikes! I must have been half asleep. You don't Set strings. Nor do you delimit string variables with quotes:
path, vbNormal not "path", vbNormal

Did you include Scripting RunTime library in your references?
Since this is Access vba then IIRC, FollowHyperlink works for opening files and is simpler for this purpose. In this case, something like
Application.FollowHyperlink(path)
 
Upvote 0
Micron, help me understand why THIS works =/

updated code is below, As you can see I had deactivated some lines that product some kind of issue, why would putting the path directly into this line
"VBA.shell "Explorer.exe O:\2 - QC Data Tracking System\Database tables\brokepinsheets\" & Me.Program & Space(1) & Me.Product & ".pdf", vbNormalFocus" lwork but

"VBA.shell "Explorer.exe" & path, vbNormalFocus" trying to use a String statement to designate file path make it unable to see the file path and tell me that it cant find the file? is it a concatenation issue?


VBA Code:
'Dim shell As Object
'Set shell = CreateObject("WScript.Shell")
Dim path As String

path = "O:\2 - QC Data Tracking System\Database tables\brokepinsheets\" & Me.Program & Space(1) & Me.Product & ".pdf"

  If Me.Defect = "Broke Pin" Then
   'shell.Run path
   'Application.FollowHyperlink path
   'VBA.shell "Explorer.exe" & path, vbNormalFocus
   VBA.shell "Explorer.exe O:\2 - QC Data Tracking System\Database tables\brokepinsheets\" & Me.Program & Space(1) & Me.Product & ".pdf", vbNormalFocus
  End If
  
    DoCmd.GoToRecord , "", acNext
    DoCmd.OpenQuery "prodIDupdate", acViewNormal, acEdit
    DoCmd.Close acQuery, "prodIDupdate"
    DoCmd.GoToRecord , "", acPrevious
    DoCmd.OpenReport "Sort Sheet", acViewReport, "", "[IDNum]=[Forms]![NotesfrmSortsheet]![IDNum]", acNormal


Command206_Click_Exit:
    Exit Sub

Command206_Click_Err:
    MsgBox Error$
    Resume Command206_Click_Exit

End Sub
 
Upvote 0
Learn to step through your code and inspect variables as you go, either by mousing over them or inquiring in the immediate window, or using Debug.Print statements.
But as a beginner to those methods (sorry if that's not accurate) I suggest you concatenate strings & variables and debug those rather than concatenating literal parts with strings in code. F'rinstance, if you put a break point on the code and step through and have this
path = "Explorer.exe" & path
Debug.Print path

When you see the result in the immediate window you probably have at least one error, that being no space after .exe

While looking at some old code, I also note that I have used the full path of the application whereas you are not. If a missing space is your current issue, fix that and you might still have the error because of way you invoke the application. You may need to do that as well. Also, if your path contains spaces I'm fairly certain you need to literally include quotes and they can get quite convoluted:
Shell "C:\Program Files (x86)\Windows Media Player\wmplayer /play /close " & """" & strMP3 & """", vbMaximizedFocus

which is why I'd try FollowHyperlink instead. It will use the application that your system has registered for it.
 
Upvote 0

Forum statistics

Threads
1,223,708
Messages
6,174,005
Members
452,542
Latest member
Bricklin

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