Jump to content

Stuck trying to open rtf file using "Run"


Recommended Posts

This works:

Case $msg = $button1
Run("My Folder\Batch File.bat")

But this won't open an RTF text file in wordpad.exe, nothing happens when I click the button:

Case $msg = $button2
Run("My Folder\Info File.rtf")

I tried this to no avail:

Case $msg = $button2
Run("%ProgramFiles%\Windows NT\Accessories\wordpad.exe", "My Folder\Info File.rtf")

Cheers folks.

Link to comment
Share on other sites

5 hours ago, TalesFromTheScript said:

Cheers but I can never get ShellExecute to work, gave up trying.

ShellExecute (ShellExecuteWait) opens the specified file (here : test.rtf). The program used to open the file depends on the file association specified for .rtf. By default this is Wordpad.exe, but can also be, as in my case, another program, e.g. OpenOffice.

Try this :

Local $sMyRTFFile = @ScriptDir & "\test.rtf" ; <== full path to the .rtf file
ShellExecuteWait($sMyRTFFile, "", "", "open")
If @error Then MsgBox(BitOR(4096, 16), "Message", "ShellExecute has failed" & @CRLF)

EDIT : Just out of curiosity - What happens if you run my example ?

Local $sMyRTFFile = @ScriptDir & "\test.rtf" ; <== full path to the .rtf file
RunWait(@ProgramFilesDir & "\Windows NT\Accessories\wordpad.exe " & $sMyRTFFile, "")
If @error Then MsgBox(BitOR(4096, 16), "Message", "RunWait has failed" & @CRLF)
Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Cheers Musashi. I've not run your code yet but the way I am doing it is running from a button.

 

Case $msg = $button1
Run("MyFolder\OpenRTF.bat")

 

That bat file contains:

cd /d %~dp0

start "" "Doc Files\RTF Document.rtf"

exit


It works this way but it would be good to not have a command window flashing on screen so I will have a play around with ShellExecute when I get time to.

Edited by TalesFromTheScript
Link to comment
Share on other sites

On 4/27/2021 at 8:57 AM, Musashi said:
Local $sMyRTFFile = @ScriptDir & "\test.rtf"
Run(@ProgramFilesDir & "\Windows NT\Accessories\wordpad.exe " & $sMyRTFFile, "")

 

 

It should be like this:

Local $sMyRTFFile = @ScriptDir & "\test.rtf" ; <== full path to the .rtf file
RunWait(@ProgramFilesDir & '\Windows NT\Accessories\wordpad.exe "' & $sMyRTFFile & '"')

 

Link to comment
Share on other sites

2 hours ago, Zedna said:

It should be like this:

Local $sMyRTFFile = @ScriptDir & "\test.rtf" ; <== full path to the .rtf file
RunWait(@ProgramFilesDir & '\Windows NT\Accessories\wordpad.exe "' & $sMyRTFFile & '"')

 

You are correct, of course. This also takes into consideration, that the file name may contains blanks.

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Happy day

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <FileConstants.au3>

Global $sFile_RTF, $sIPID_Wordpad, $nMsg

#Region GUI
Global $GUICtrl_hWnd = GUICreate("Open File RTF", 452, 140, -1, -1)
Global $GUICtrl_Input_1 = GUICtrlCreateInput("", 64, 16, 297, 21)
Global $GUICtrl_Label_1 = GUICtrlCreateLabel("RTF File:", 16, 16, 47, 17)
Global $GUICtrl_Button_1 = GUICtrlCreateButton("...", 368, 16, 75, 25)
Global $GUICtrl_Button_2 = GUICtrlCreateButton("Open RTF File", 136, 48, 307, 49)
Global $GUICtrl_iStatus = GUICtrlCreateLabel("Ready", 0, 112, 444, 17)
Global $GUICtrl_GUICtrlCreateRadio_1 = GUICtrlCreateRadio("WordPad.exe", 16, 56, 89, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$GUICtrl_GUICtrlCreateRadio_2 = GUICtrlCreateRadio("Default OS", 16, 80, 81, 17)
GUISetState(@SW_SHOW)
#EndRegion GUI

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUICtrl_Button_2
            $sFile_RTF = GUICtrlRead($GUICtrl_Input_1)
            If $sFile_RTF = '' Then $sFile_RTF = _OpenDialog_SelectFile()
            If Not FileExists($sFile_RTF) And FileExists(@ScriptDir & '\' & $sFile_RTF) Then $sFile_RTF = @ScriptDir & '\' & $sFile_RTF
            If FileExists($sFile_RTF) Then
                If (GUICtrlRead($GUICtrl_GUICtrlCreateRadio_1) = $GUI_CHECKED) Then
                    ; Run wordpad
                    $sIPID_Wordpad = Run('"' & @ProgramFilesDir & '\Windows NT\Accessories\wordpad.exe" "' & $sFile_RTF & '"')
                Else
                    $sIPID_Wordpad = ShellExecute('"' & $sFile_RTF & '"', '', @ScriptDir)
                EndIf
                If $sIPID_Wordpad Then
                    GUICtrlSetData($GUICtrl_iStatus, 'Open file success (PID:' & $sIPID_Wordpad & ')')
                Else
                    GUICtrlSetData($GUICtrl_iStatus, 'Open file failure !')
                EndIf
            Else
                GUICtrlSetData($GUICtrl_iStatus, 'No file(s) were selected.')
            EndIf
        Case $GUICtrl_Button_1
            _OpenDialog_SelectFile()
    EndSwitch
WEnd

Func _OpenDialog_SelectFile()
    ; Display an open dialog to select a list of file(s).
    $sFile_RTF = FileOpenDialog('Select a RTF file', @WindowsDir & "\", "Rich Text (*.*)", BitOR($FD_FILEMUSTEXIST, $FD_MULTISELECT))
    If @error Then
        ; Display the error message.
        GUICtrlSetData($GUICtrl_iStatus, 'No file(s) were selected.')
    Else
        ; Replace instances of "|" with @CRLF in the string returned by FileOpenDialog.
        $sFile_RTF = StringReplace($sFile_RTF, "|", @CRLF)
        ; Display the list of selected files.
        ;MsgBox(0, "", "You chose the following files:" & @CRLF & $sFile_RTF)
    EndIf
    ; Change the working directory (@WorkingDir) back to the location of the script directory as FileOpenDialog sets it to the last accessed folder.
    FileChangeDir(@ScriptDir)
    GUICtrlSetData($GUICtrl_Input_1, $sFile_RTF)

    Return $sFile_RTF
EndFunc   ;==>_OpenDialog_SelectFile

 

Regards,
 

Link to comment
Share on other sites

Thanks folks, I don't know why it wasn't working last time (maybe because I was trying to open ".lnk" shortcuts, trying to keep the shortcut properties intact) but it does work now with that simple line:

ShellExecute ("Path To\My Document.rtf")

The folder for the RTF file is next to the compiled autoit exe and it works fine now.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...