0
votes

I have been trying to open a file using ShellExecute method in VB6, filename will be taken from a textbox within the form. But the file doesn't open and no errors too.

However the same method works smooth if i pass the file name directly instead of referring from a variable. Code below for reference. Not sure where problem exists but any help is much appreciated.

WorkingCode

ShellExecute 0, vbNullString, "F:\Desktop\SBKL\template.xlsx", vbNullString, vbNullString, vbNormalFocus  

NonWorking Code

Dim FlNme As String
FlNme = Trim(Me.T_Doc_Link.Text)
ShellExecute 0, vbNullString, FlNme, vbNullString, vbNullString, vbNormalFocus
1
What exactly is in the FlNme variable when it fails?RBarryYoung
Also, you should probably call it as a function to get its return value: intReturn = ShellExecute(0, vbNullString, FlNme, vbNullString, vbNullString, vbNormalFocus).RBarryYoung
@RBarryYoung Thanks a lot. I tried LOOKING into what is exactly there inside FlNme and found that there were few line breaks. Am clueless how the linebreaks came. However, the following code works smooth : FlNme = Replace(Replace((Trim(Me.T_Doc_Link.Text)), vbCrLf, ""), vbNewLine, "")Maruthi

1 Answers

1
votes

Before to call ShellExecute() you should check if file exists using Dir$() and warning the user when the path and/or file are wrong:

If Dir$(FlNme, vbNormal) = vbNullString Then
    MsgBox "File not found:" & vbCrLf & FlNme
    Exit Sub
End If
ShellExecute 0, vbNullString, FlNme, vbNullString, vbNullString, vbNormalFocus