ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Open non-Office prog. macro (https://www.excelbanter.com/excel-programming/323961-open-non-office-prog-macro.html)

Schneider_4

Open non-Office prog. macro
 
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)

Jake Marx[_3_]

Open non-Office prog. macro
 
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]


Schneider_4 wrote:
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)



jonjo

Open non-Office prog. macro
 
Here is the simple version:

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

Greetings Jonjo

"Schneider_4" wrote:

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)


Rick S.

Open non-Office prog. macro
 
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



"jonjo" wrote:

Here is the simple version:

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

Greetings Jonjo

"Schneider_4" wrote:

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)


Chip Pearson

Open non-Office prog. macro
 
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)

"Rick S." wrote in message
...
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



"jonjo" wrote:

Here is the simple version:

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

Greetings Jonjo

"Schneider_4" wrote:

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)




All times are GMT +1. The time now is 10:24 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com