I’m trying to make a program that opens files in word after giving it file extension. But obviously not all files can be opened in word. So I was wondering if there’s a way to get some kind of exception if word is unable to open the file. Any ideas would help. But I’m using VB.
try {
$a = ./test.docx #pdf file changed to .docx
}
catch {
Write-Error "Error!" Exit
} Write-Host "No Error!"
Based on your comments above, I’d suggest that you look up the ShellExecute WinAPI call.
If you pass the file (full path, name, extension) to ShellExecute, Windows figures out what app is registered to open the file and calls it for you. In other words, if you call ShellExecute on c:\some_dir\myfile.docx it’ll open the file in Word or return a value indicating that there was an error.
There’s an example here on my PPT FAQ site, along with a link to much more info on Randy Birch’s superb site:
http://www.pptfaq.com/FAQ00479_ShellExecute_Example.htm