Friday, July 26, 2013

Insert Words, Combine/Merge RTF Files

This came from a question inside foxite forum when a member tried to merge two RTF files into one via FILETOSTR() and STRTOFILE() and the member cannot see the second set of words added at the end of the rtf document, and wonder why.  So here is the explanation.

An RTF file starts with {\rtf1 and ends with corresponding }. So if you combine two or more rtf documents via a FILETOSTR() then later STRTOFILE(), each of those will have their own opening and closing RTF codes.  And an RTF document will just read whatever is within its opening and closing codes that whatever is added after the closing bracket will be ignored.

Here is a test for you to see better.  Copy these codes and paste to a notepad, save with an RTF extension, quit  and open the file in wordpad or msWord:

{\rtf1\Ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Arial;}}
\viewkind4\uc1\pard\f0\fs20 This is sample\f1\fs18
\par }


Now, we will simulate FILETOSTR() and STRTOFILE() with additive clause at the end.  Imagine if we copy the same set of codes and paste it twice inside, on notepad, it will look like this:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Arial;}}
\viewkind4\uc1\pard\f0\fs20 This is sample\f1\fs18
\par }
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Arial;}}
\viewkind4\uc1\pard\f0\fs20 This is sample\f1\fs18
\par }

Save it, close and open in msWord or Wordpad.  Surprise!!! There is still one line of word "This is a sample"  instead of two.  Again because an RTF ignores anything that is after the closing } bracket.

So we cannot blame FILETOSTR() and later STRTOFILE() as those are doing properly what they are designed for.  Either you clean up the RTF codes yourself or better use Word Automation so it will clean the RTF codes for you.

Here is a sample of word automation:
Set Safety Off
Declare Integer ShellExecute In shell32.Dll ;
      INTEGER hndWin, ;
      STRING cAction, ;
      STRING cFileName, ;
      STRING cParams, ;
      STRING cDir, ;
      INTEGER nShowWin
#Define wdStory           6
#Define wdPasteDefault    0

Local lcSource, lcTarget
lcTarget = Addbs(Getenv("TMP"))+"Target.rtf"
lcSource = Getfile("rtf")
If !Empty(m.lcSource)
      Create Cursor junk (myMemo M)
      Insert Into junk Values (Filetostr(m.lcSource))

      * Let us create the RTF file
      Strtofile(junk.myMemo,m.lcTarget)

      * Now we will make two copies of the content in a single RTF file
      Local loRTF As Word.Application
      loRTF = Createobject("Word.Application")
      With loRTF
            .DisplayAlerts = .F.
            .Documents.Open(m.lcTarget)
            * Copy everything, paste as a new set, save and quit
            .Selection.WholeStory
            .Selection.Copy
            .Selection.EndKey(wdStory)
            .Selection.PasteAndFormat(wdPasteDefault)
            .ActiveDocument.SaveAs(m.lcTarget,6)
            .ActiveDocument.Close
            .Quit
      Endwith
      * Check using wordpad
      ShellExecute(0,"Open","wordpad.exe",m.lcTarget,"",1)
Else
      Messagebox('You aborted!')
Endif




I hope this helps you on your needs!

2 comments:

  1. Hola Jun.
    Primero que nada, felicidades por haber recibido el premio CEIL SILVER AMBASSADOR, creo que es merecido por tu colaboración al mundo de la informatica.

    Quisiera saber si hay alguna posibilidad de que me envies la herrameinta "Enhanced RTF Tool Revealed", que la he buscado por mucho tiempo y no la puedo descargar.
    Encontré un blog donde pones a disposicion de la gente, pero no lo puedo descargar porque me lleva a una direccion que no termina en nada.
    mi correo es ricardo.riago@gmail.com, por si me lo puedes enviar, o inidicar un link desde donde pueda descargarlo.
    Ni desde FOXITE puedo descargarlo, nada más por eso te molesto.
    Te escribí en otro blogs, y espero me disculpes por tanta insistencia, pero tengo un sistema médico que debo terminar una vez que pueda acceder a ese modulo tuyo.

    Desde ya, AGRADECIDO EN DIOS por tus aportes.

    Desde PARAGUAY
    Ricardo González

    ReplyDelete
    Replies
    1. Hello Ricardo,

      I believe this is the one you are looking for. I shared this new link last time in foxite. Please get back to me when this is not the one: https://dl.dropboxusercontent.com/u/15025358/ssrtf.rar

      Delete