?
Solved

To Open WordPad File from VB

Posted on 2002-06-29
3
Medium Priority
?
996 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

621 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