Solved

To Open WordPad File from VB

Posted on 2002-06-29
3
940 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now