September 30, 2021

Open Any File Using Excel VBA

How to open PDF, Text, Word, etc files using VBA in Excel. Watch the video below:

Open Any File Type like PDF using VBA

An easy way to open a file is to open the file explorer and double click on a file. The file is automatically opened because Windows knows the file extension and the associated program to open the file. See the image below:

Opening File Using the File Explorer
Opening File Using the File Explorer

We can automate this process of opening a file via Excel VBA using the following macro code:

Option Explicit

Sub OpenAnyFile()

VBA.Shell “Explorer.exe C:\Diya-David Lowenthall SEC.pdf”, vbNormalFocus ‘Active line of code

‘VBA.Shell “Explorer.exe C:\Users\takyar\Documents\confucianism-how-it-framed-chinese-economy-society-culture-polity.docx”, vbMaximizedFocus

‘VBA.Shell “Explorer.exe C:\ETV-22-09-2021\excel-training-videos-16\simple-calculation-using-IF.xlsx”, vbNormalFocus

‘VBA.Shell “Explorer.exe C:\Users\takyar\Documents\cleaning-tank.txt”, vbMaximizedFocus

End Sub

Some of the VBA code lines have been remarked out using an apostrophe. If you remove the apostrophe the line of code becomes active i. e gets executed on running the macro.