Open non-Office prog. macro

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Just a warning - I'm not very well versed in VB like this. Here's what I
want to do:

Off of a button in Excel, I want to open a shortcut to another document
(non-office document)which is on my desktop. Let's just say the document's
name is "ABC" for the example. Can someone help me with what the macro text
would look like? TIA -Steve (operating in XP)
 
Hi Schneider_4,

You can use the Windows API to do this. Here's some sample code:

'/ ---------BEGIN CODE---------
Public Const SW_SHOWMAXIMIZED = 3
Public Const ERROR_FILE_NOT_FOUND = 2&


Public 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


Public Function gbOpenDocument(rsFullPath As String, _
rsErrMsg As String) As Boolean
Dim lResponse As Long


lResponse = ShellExecute(Application.hwnd, _
"open" & vbNullChar, rsFullPath & vbNullChar, _
vbNull, vbNull, SW_SHOWMAXIMIZED)


If lResponse = ERROR_FILE_NOT_FOUND Then rsErrMsg _
= "File not found."
gbOpenDocument = (lResponse > 32)
End Function
'/ ---------END CODE---------

To use this, just follow these steps:

1) Paste the above code into a Standard Module in the VBE (Alt+F11 to get
there)

2) To open a document, your code would just look something like this:

'/ ---------BEGIN CODE---------
Sub demo()
Dim sErr As String
Dim sPath As String

sPath = "C:\test.gif"

If Not gbOpenDocument(sPath, sErr) Then
MsgBox "Error opening '" & sPath & "': " _
& sErr, vbExclamation, "Error"
End If
End Sub
'/ ---------END CODE---------

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 
Here is the simple version:

Sub Makro1()
Shell "cmd.exe /c start C:\document.pdf"
End Sub

Greetings Jonjo
 
FYI
Learned from using this example code. The cmd.exe shell is limited to the
old DOS 8.3 file naming conventions.
Thus, a path string like "C:\Mydocument.pdf" would be condensed to
"C:\Mydocu~1.pdf"

--
Regards

Rick
XP Pro
Office 2007
 
And if you need to convert to an 8.3 name from a long file name, use

Public Declare Function GetShortPathName Lib "kernel32" Alias
"GetShortPathNameA" ( _
ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Long) As Long

Function ShortFileName(LongFileName As String) As String
Dim S As String
Dim L As Long
Dim R As Long
L = 260
S = String$(L, vbNullChar)
R = GetShortPathName(LongFileName, S, L)
If R Then
ShortFileName = Left(S, R)
Else
ShortFileName = vbNullString
End If
End Function


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top