Chuckero Posted November 12, 2020 Share Posted November 12, 2020 (edited) Hello, I'm trying to work with RTF files and I started with a very simple test, I got the example from the _GUICtrlRichEdit_StreamFromFile(), which is: expandcollapse popup#include <GUIConstantsEx.au3> #include <GuiRichEdit.au3> #include <WindowsConstants.au3> Example() Func Example() Local $hGui, $iMsg, $idBtnNext, $iStep = 0, $idLblMsg, $hRichEdit $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, StringLen(".exe")) & ")", 320, 350, -1, -1) $hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 300, 220, _ BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL)) $idLblMsg = GUICtrlCreateLabel("", 10, 235, 300, 60) $idBtnNext = GUICtrlCreateButton("Next", 270, 310, 40, 30) GUISetState(@SW_SHOW) _GUICtrlRichEdit_SetText($hRichEdit, "First paragraph") While True $iMsg = GUIGetMsg() Select Case $iMsg = $GUI_EVENT_CLOSE _GUICtrlRichEdit_Destroy($hRichEdit) ; needed unless script crashes ; GUIDelete() ; is OK too Exit Case $iMsg = $idBtnNext $iStep += 1 Switch $iStep Case 1 _GUICtrlRichEdit_StreamToFile($hRichEdit, @DesktopDir & "\gcre.rtf") GUICtrlSetData($idLblMsg, "Streamed to gcre.rtf") Case 2 _GUICtrlRichEdit_SetText($hRichEdit, "") _GUICtrlRichEdit_StreamFromFile($hRichEdit, @DesktopDir & "\gcre.rtf") GUICtrlSetData($idLblMsg, "Streamed from gcre.rtf") Case 3 _GUICtrlRichEdit_SetSel($hRichEdit, 2, 6) _GUICtrlRichEdit_StreamFromFile($hRichEdit, @DesktopDir & "\gcre.rtf") GUICtrlSetData($idLblMsg, "Replaced selection: an intentional mess!") GUICtrlSetState($idBtnNext, $GUI_DISABLE) EndSwitch EndSelect WEnd EndFunc ;==>Example So, on the first click on the "Next" button the scrip creates the file "gcre.rtf" in the Desktop. This is the content got from Notepad++: So far so good. At this point I opened the file in WordPad, no changes, and just saved it. And in the Notepad I got: Note the NUL char added to end of the file, this mess up the file and the script can not read the file anymore. This problem happens in all files I tried to create using WordPad. If I use Word, its ok, but the file increase from 1kB to 43kB! I'm using Windows and WordPad 10 version 1903 (OS Build 18362.1139). Someone knows what is happening here? Edited November 12, 2020 by Chuckero Link to comment Share on other sites More sharing options...
Nine Posted November 12, 2020 Share Posted November 12, 2020 You could do something like this (untested) by replacing : _GUICtrlRichEdit_StreamToFile($hRichEdit, @DesktopDir & "\gcre.rtf") with Local $sTxt = FileRead(@DesktopDir & "\gcre.rtf") If StringRight($sTxt,1) = chr(0) Then $sTxt = StringTrimRight($sTxt,1) _GUICtrlRichEdit_StreamFromVar ($hRichEdit, $sTxt) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
water Posted November 12, 2020 Share Posted November 12, 2020 Or simply use Word (if possible). Size doesn't matter nowadays. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
pseakins Posted November 12, 2020 Share Posted November 12, 2020 5 hours ago, Chuckero said: At this point I opened the file in WordPad, no changes, and just saved it Notepad has the "save" option greyed out. You cannot save it with no changes. You must have done something to it in Notepad in order for it to "save". If I do a "Save As" in Notepad I get an identical file;- Phil Seakins Link to comment Share on other sites More sharing options...
Chuckero Posted November 13, 2020 Author Share Posted November 13, 2020 On 11/12/2020 at 9:25 AM, Nine said: You could do something like this (untested) by replacing : _GUICtrlRichEdit_StreamToFile($hRichEdit, @DesktopDir & "\gcre.rtf") with Local $sTxt = FileRead(@DesktopDir & "\gcre.rtf") If StringRight($sTxt,1) = chr(0) Then $sTxt = StringTrimRight($sTxt,1) _GUICtrlRichEdit_StreamFromVar ($hRichEdit, $sTxt) Good idea, but I tried and it doesn't work. Very weird. This is what I got in the GUICtrlRichEdit: Link to comment Share on other sites More sharing options...
Chuckero Posted November 13, 2020 Author Share Posted November 13, 2020 (edited) On 11/12/2020 at 3:24 PM, pseakins said: Notepad has the "save" option greyed out. You cannot save it with no changes. You must have done something to it in Notepad in order for it to "save". If I do a "Save As" in Notepad I get an identical file;- I'm not using NotePad I'm using WordPad. Even that, for me both software have the save option enabled, I can save the files directly with no changes in the content. And I can see the NUL char in NotePad++ and also in WinMerge. This is another interesting situation: I used Notepad to test what you said, and in really NotePad can read the file and it has corrected the error created by WordPad when I saved the file. Edited November 13, 2020 by Chuckero Link to comment Share on other sites More sharing options...
pseakins Posted November 14, 2020 Share Posted November 14, 2020 3 hours ago, Chuckero said: I'm not using NotePad I'm using WordPad My mistake. Well, anything can happen with Microsux products, I have gotten into a world of pain when my colleagues have used editors which have changed plain text Ascii files by adding headers and or trailers rendering them incompatible with my tools. It looks like your setup with Wordpad has it injecting it's own personality into your files. Phil Seakins Link to comment Share on other sites More sharing options...
Nine Posted November 14, 2020 Share Posted November 14, 2020 (edited) Ok found a way to work it out : $hFile = FileOpen("gcre.rtf", $FO_BINARY) $dTxt = FileRead($hFile) FileClose($hFile) $sTxt = BinaryToString($dTxt) ConsoleWrite($sTxt & @CRLF) If StringRight($sTxt, 1) = Chr(0) Then ConsoleWrite("found char 0" & @CRLF) $sTxt = StringTrimRight($sTxt, 1) EndIf _GUICtrlRichEdit_StreamFromVar($hRichEdit, $sTxt) Replace _GUICtrlRichEdit_StreamFromFile($hRichEdit, @DesktopDir & "\gcre.rtf") with the above code. Tested and working Edited November 14, 2020 by Nine Chuckero 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Chuckero Posted November 17, 2020 Author Share Posted November 17, 2020 On 11/14/2020 at 8:20 AM, Nine said: Replace _GUICtrlRichEdit_StreamFromFile($hRichEdit, @DesktopDir & "\gcre.rtf") with the above code. Tested and working YES!! Now it's working perfectly! Thanks! What a workaround to be able to make this work 😉 Link to comment Share on other sites More sharing options...
Nine Posted November 17, 2020 Share Posted November 17, 2020 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Skysnake Posted November 19, 2020 Share Posted November 19, 2020 Glad you found a solution. The problem is that RTF does have versions, and not all parsers are up to date or equally powerful. You will find that WordPad is really primitive and clumsy when it comes to editing RTF. Skysnake Skysnake Why is the snake in the sky? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now