Sunday, June 18, 2023

Displaying blog content within my blog manager

After my last episode about adding features to my blog manager program, I've been using it frequently. During this period, the only new feature that I have considered adding is the ability to store the actual blog text and display it within the program. Displaying shouldn't be a problem but I wasn't sure how to save html text (although I have done this in the past, but in those cases, the text was generated within the program and not originally external). I haven't had much time to even think about this, but on Friday afternoon I had the time and space. 

I spent a frustrating hour and a half trying to save html text to a new table; my guide was a question on Stack Exchange where someone asked how to save the text displayed within a web browser - not exactly my situation, but close. Big mistake. Eventually I realised that I was knocking my head against a brick wall, so I decided to take the dog for a walk. 

As usual, when away from the computer, the beginning of a solution appeared. These blogs are automatically sent to my email account, so I save them in Outlook. I can use the 'save' option to create an html file from each letter. My original intention was to somehow save the text of those files in the blog manager, but when thinking about it, I realised that I was trying to do something completely unnecessary. Save the blogs as files, definitely, but then within the blog manager simply load a given html file into an internal web browser. If the files are named according to their blog entry index number, then no database access is required: if I want to display the text of blog #1617, then all I need do is access the file 1617.html from a predefined directory. Display it if it exists otherwise do nothing. Very simple. 

The only problem left is that I run Outlook on a different computer, and anyway there's no automatic way (that I know, apart from automating Outlook - there's an idea for a rainy day) of creating those html files; they also have the wrong name when created and they're on the wrong computer. Trivial problems. Yesterday I created files from all the blog entries from the beginning of May, transferred them then renamed them. The blog manager displays them without breaking a sweat. 

To my surprise, pictures included in the blog entry get displayed. I have just had a quick look at the source html code in order to see how this trick is managed: the internal web browser is accessing the picture as it is stored in Blogger! That explains why blogs with pictures load slowly.

At the moment, the internal web browser that is displaying the text is placed on a tab sheet within a page control; I haven't yet internalised that I need to switch tabs in order to display another blog. The list of blogs is in the 'data' tab whereas the text is in the 'text' tab. As I write this, I think it would be better to transfer the browser from where it is now to a separate stand-alone window. I could also add a 'close browser windows' command to the main menu that would close these without affecting any other window. Hmmm ... yellow rubber duck time.

In the end, I did write Outlook automation code but it was a very difficult process. As opposed to Word and Excel, Outlook lacks a clearly defined object structure: there are folders within folders and items of different types within those folders (although each folder will hold only one type of item). The major problem that I had was obtaining a pointer to the correct folder, so that I could iterate over the items (letters) and save them to html files. Eventually I found code that would recursively iterate over all the folders, saving them to a treeview. This code served as a template for what I wanted, although there were still a few problems to solve.

I couldn't get a pointer to the correct folder but I did manage to get the index to this folder, and this number ('foldernum') was the key. At first, the code saved the files with the names of the blog entries, but certain blog entries caused problems - at least, one blog with a backslash (e.g. 'passports/2'). As this program was intended to run only once properly, I substituted the file name with the counter. Unfortunately, this number does not correspond to the blog number: there are comments saved in the Outlook folder, as well as blog entries from a different blog. Had I been a bit more on the ball, I would have used a separate index, but this refinement is not worth the bother. 

Now I have 1500+ files stored; I have to check for each one what its corresponding blog number is and renumber appropriately.
const olHTML = 5; procedure TForm1.FormActivate(Sender: TObject); var i: integer; s: string; begin CoInitialize (nil); outlook:= createoleobject ('Outlook.Application'); namespace:= outlook.getnamespace ('MAPI'); found:= false; GetFolder (namespace.folders); myfolder:= myfolder.item[foldernum]; for i:= myfolder.items.count downto 1 do begin s:= myfolder.items[i].subject; if pos ('[Perceptions]', s) = 1 then myfolder.items[i].saveas ('c:\tmp\' + inttostr (i) + '.htm', olHTML); end; myfolder:= unassigned; outlook:= unassigned; namespace:= unassigned; CoUnInitialize; end; procedure TForm1.GetFolder(folder: variant); var i: integer; begin i:= 0; while (i < folder.count) and not found do begin inc (i); if folder.item[i].name = 'Blog' then begin found:= true; myfolder:= folder; foldernum:= i; end else getfolder (folder.item[i].folders); end; end; end.


This day in history:

Blog # Date Title Tags
486 18/06/2012 Dubrovnik log 3 Kindle, Peter Robinson, Holiday, Dan Ariely, Dubrovnik
601 18/06/2013 Edinburgh log (5): Around and about Holiday, Edinburgh, Bagpipes
722 18/06/2014 Intermission: the tale of the linen jacket (Sorrento log 7A) Holiday, Sorrento, Italy
723 18/06/2014 The isle of Capri (Sorrento log 7B) Holiday, Sorrento, Italy
1149 18/06/2018 This must be the place Personal
1517 18/06/2022 Aches and pains: my first swim of the year Health, Swimming

No comments: