?
Solved

To Open WordPad File from VB

Posted on 2002-06-29
3
Medium Priority
?
979 Views
Last Modified: 2012-08-13
We can open any  exicutable file from VB using Shell command ,
ie :  shell "c:\windows\write.exe"   but here I want to open a particular wordpad file from VB
   How can I give name of the file as argument .
     and note that -> if we have installed MSOFFICE in our System  , wordpad file will automatically open in MS WORD ,
       I want the file to open  in Wordpad itself

     -> in short, I want to open a particular file in Wordpad itself from VB

advanced sweet thanks for helping
0
Comment
Question by:romance
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
bruintje earned 200 total points
ID: 7118839
Hi romance,

an easy way would be
Shell "C:\Program Files\Accessories\WORDPAD.EXE MyFilePath"

more safe way from a newsgroup post by Joe Le Vasseur

___________snip__________________________________________

Here is how I would do it. This should work even if someone has removed/uninstalled WordPad.

Option Explicit
Private Declare Function FindExecutable Lib "shell32.dll" _
   Alias "FindExecutableA" _
  (ByVal lpFile As String, _
   ByVal lpDirectory As String, _
   ByVal sResult As String) As Long

Private Sub Command1_Click()
    Dim lRet As Long
    lRet = Shell(AssociatedEXE("WRI") & " " _
        & "C:\Autoexec.bat", vbNormalFocus)
End Sub

Public Function AssociatedEXE(Ext As String) As String
   Dim iFN                          As Integer
   Dim lRet                         As Long
   Dim szResult                     As String
   Dim szTmpFile                    As String
   Const MAX_PATH                   As Long = 260
   Const FE_SUCCESS                 As Long = 32
   '-------------------------------------------------
   On Error GoTo errAssociatedEXE
   szResult = Space$(MAX_PATH)
   iFN = FreeFile
   szTmpFile = HomeDirectory() & "AE_TMP." & Ext
   If FileExists(szTmpFile) Then
      Kill (szTmpFile): DoEvents
   End If
   Open szTmpFile For Random As #iFN
   Close #iFN: DoEvents
   lRet = FindExecutable(szTmpFile, "", szResult)
   If (lRet >= FE_SUCCESS) Then
      lRet = InStr(szResult, Chr$(0))
      If (lRet > 0) Then
         szResult = Left$(szResult, lRet - 1)
      End If
   End If
   szResult = Trim$(szResult)
   If FileExists(szTmpFile) Then
      Kill (szTmpFile): DoEvents
   End If
   AssociatedEXE = szResult
Exit Function
errAssociatedEXE:
   Close #iFN
   Resume Next
End Function

Public Function HomeDirectory() As String
    HomeDirectory = App.Path & IIf(Right$(App.Path, 1) = "\", "", "\")
End Function

Public Function FileExists(ByVal szFileName As String) As Boolean
' ///////////////////////////////////////////////////
' Purpose: See function name                       '/
' Parameters: String containing file name          '/
' Return Value: True/False                         '/
'////////////////////////////////////////////////////
   Dim AttrRet         As Integer
   Dim bIsDirectory    As Boolean
   '------------------------------------------------
   On Error Resume Next
   AttrRet = GetAttr(szFileName)
   If Err.Number Then
      Err.Clear
      FileExists = False
   Else
      bIsDirectory = CBool((AttrRet And vbDirectory) = vbDirectory)
      If (bIsDirectory = False) Then
         FileExists = True
      Else
         FileExists = False
      End If
   End If
End Function
___________snip__________________________________________


HAGD:O)Bruintje
0
 

Expert Comment

by:salman_ilm
ID: 7120261
you can use SHELL command in VB
shell ("path of any program to be run")

0
 
LVL 44

Expert Comment

by:bruintje
ID: 7120374
that was the easy way already stated
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question