Automate the transfer of articles from Microsoft Word to Geektimes
I like to write on Geektimes , but there is one inconvenience. This is the need to use custom markup in the style of habra and manually transfer pictures to habrastorage.org . I write articles in MS Word under Windows . I honestly spent about an hour to find ready-made solutions to this problem. The best found was an analysis of the habrastorage API to automate image loading, but it seems outdated. There was a mention of a certain converter, but with a broken link.
The essence of the proposed automation to the touch of a button in Word to get the text ready for copying the window editor Geektimes. And after copying, so that pictures automatically appear there.
The technology is very simple. MS Word , as you know, may contain embedded code in Visual Basic . This is a special Visual Basic dialect called VBA ( Visual Basic for Applications ). In VBA, you can write quite complex scripts for converting documents to Word. Procedures written in VBA are called macros. Macros in Word can be called up explicitly from a special menu, or you can assign a call to their execution to a specific button on the toolbar.
Pictures same Geektimeshe pumps to himself. In order for him to download from somewhere, he needs to place the pictures somewhere on the Internet. In order not to take up my time developing and analyzing various APIs for free image hosting sites like photos.google.com, I decided to turn to the good old FTP . We purchase or in any way receive a website hosting service with FTP content downloading . Sam MS Word VBA has no tools to work on the protocol the FTP , but it works seamlessly with COM objects (ie, special dll files). For work on FTP , the COM module from the WinSCP project was used.
The VBA macro written by me first replaces the specified styles of the Word document with the markup of the text, then converts the Word document into an HTML document . The HTML representation itself is not necessary for us. As a result of conversion, Word creates a subdirectory with all the pictures from the document in the format. png . Pictures in bulk are transferred to a given FTP server. After that, the macro deletes the pictures in the document and substitutes in their place links to the site where these pictures were just uploaded. It remains in the window of the Word document to select all the text and copy it to the editor window in Geektimes. Click a button in Geektimes to save it as a draft. Then again click on editing and make sure that Geektimes has already uploaded the pictures and replaced them with links to habrastorage . It is done.
WinSCP Installation
Download the file . NET assembly / COM library and unpack it into an accessible directory.
In this directory, run the command line: % WINDIR% \ Microsoft.NET \ Framework64 \ v4.0.30319 \ RegAsm.exe WinSCPnet.dll / codebase /tlb:WinSCPnet64.tlb
That's it.
How to create an article on this technology.
To simplify matters, a template file , Article_GT.dotm , was created that contains the necessary VBA macro and a pointer to connect to the WinSCP object .
To look at the macro and check the operability of connecting the WinSCP object, after opening the template file, click this button on the Word toolbar
and select View Macros in it , then Edit . A window of this kind should appear:
In this window, in the Tools-> References menu , this dialog opens. It should be marked line with WinSCP .
If you open the file directly in Googlethen a blank sheet will open. That's right, the template is empty. In it, only the Normal style and the Heading 1 style resemble the habr style. You must write the text yourself.
The macro in the template must be adjusted before use. At the beginning of the macro is such a block
Const TMPNAME = "TMP" ' Имя временного файла в который будет конвертирован документ
Const URL = "http://www.мойсайт.ru"
Const FTPPATH = "/public_html/imgs/" ' путь к папке с картинками при загрузке по FTP, написать правильный
Const WEBPATH = "/imgs/" ' путь к картинкам при просмотре в браузере, написать правильный
Const HOSTNMAE = "ftp.мойфтпсервер.ru" ' ввести правильный адрес FTP сервера
Const USERNAME = "мой логин" ' ввести правильное имя пользователя на FTP сервере
Const PASSWORD = "мой пароль" ' ввести правильный пароль
You need to open the file as a template in Word , enter the correct parameters and save as a template.
Then you can click on the template and write an article. Save the article and invoke the ConvertToHabr macro .
If the download of pictures went fine, this will be written in the status bar in the Word window .
The initial document closes, in its place the converted text appears.
Denial of responsibility.
This is not a fully functional converter. Now it only replaces the styles of the Bold and Italic fonts with the Haber tags, changes the Heading 1..5 styles to the tags and substitutes the tags instead of hyperlinks and pictures. Since this is all I needed from this converter when writing this article.
The template file is available here and is available to everyone for improvement, but I can not guarantee the continuation of this project, its support, its quality, its patent purity and the eternal life of this link .