Solved

To Open WordPad File from VB

Posted on 2002-06-29
3
949 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
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
bruintje earned 50 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net 2008 2 60
Zip Folders Using Chilkat Routines 1 41
VBA - If Bookmark = "XXBOOKMARKXX" then 15 29
backup program with robocopy 6 22
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

810 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