DocX.dll from AHK

Post your working scripts, libraries and tools for AHK v1.1 and older
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

DocX.dll from AHK

21 Apr 2019, 12:59

Warning - this is not going to be a short post, just skip to the bottom if you want to find out how to create Word .docx documents without Word - from AHK.

After countless fits and starts I discovered why I don't see any CLR samples with the following snippet:

Code: Select all

 class Program
          {
                     static void Main(string[] args)
Epic fail for dozens of DLLs I've tried with C# snippets cribbed from the libraries' example pages (most of which have the snippet above). Well okay, I'm no genius obviously, and maybe there's a way. Okay, there is, just not what I expected.

It turns out the answer is in Lexikos's original post for CLR 1.2 , staring me right in the face.
Additional command-line arguments can be passed to the compiler via CompilerOptions. For instance, if FileName specifies an .exe file, a console app is generated unless CompilerOptions includes "/target:winexe".
Also, as Lexikos pointed out here,
You can't call static methods directly; you must either use another language or use Reflection. For Reflection, asm (the Assembly object) is the starting point. If you're not familiar with Reflection and don't want to research it by yourself, I would recommend creating a wrapper object in C#/VB instead, as shown in the second post in this thread.
I stubbornly resisted doing anything in either C# or VB because of the failures described above, but, well, okay, here it is.

By blind luck, while trying to compile a script, using another dll (iDitect.Converter.dll, more on that in another post), I got the following error message:
Compilation Failed
---------------------------
Error CS5001 on line 0: Program 'c:\Users\burque505\Desktop\AHK\HTML\iDiTect\ouch.exe' does not contain a static 'Main' method suitable for an entry point
It was pretty much downhill from there, although I would probably still be flailing around blindly if it hadn't been for that error message. There are other compiler options, and I won't get into them. Okay, let's get to the DocX code!
In the archive attached are "DocX.dll" and the script below.

Code: Select all

;DahkX.ahk by burque505
;Easter Sunday, 2019
;first (and probably last) version 0.0.1
;

csharp=
(


    using System;
    using Novacode;
    using System.Drawing;
     
    namespace FeedTheMonkey
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (DocX document = DocX.Create("FeedTheMonkey.docx"))
                {
                    // Add a new Paragraph to the document.
                    Paragraph p = document.InsertParagraph();
                    
                    // Append some text.
                    p.Append("Please feed the monkey . . . ").Font(new FontFamily("Arial Black"));                 
                    p.InsertParagraphAfterSelf("Okay, gimme a recipe!!!").Font(new FontFamily("Times New Roman")).Bold().FontSize(30).Color(Color.Red);
                    Paragraph p2 = document.InsertParagraph();
                    p2.Append("Gee whiz - you're a bad zookeeper. (sigh..)").Font(new FontFamily("Algerian")).Bold().FontSize(15).Color(Color.Blue);
                    
                    p2.AppendLine();
                    var bulletsList = document.AddList( "2 cups of flour", 0, ListItemType.Bulleted );
                    document.AddListItem( bulletsList, "3/4 cup of sugar" );
                    document.AddListItem( bulletsList, "1/2 cup of butter" );
                    document.AddListItem( bulletsList, "2 eggs" );
                    document.AddListItem( bulletsList, "1 cup of milk" );
                    document.AddListItem( bulletsList, "2 teaspoons of baking powder" );
                    document.AddListItem( bulletsList, "1/2 teaspoon of salt" );
                    document.AddListItem( bulletsList, "1 teaspoon of vanilla essence" );
                    
                    document.InsertList(bulletsList);
                    
                    // Save the document.
                    
                    document.Save();
                }
            }
        }
    }


)

; "0" for the AppDomain, "DahkX.exe" for the temporary executable, and "/target:winexe" so a console window doesn't open.
asm := CLR_CompileC#(CSharp,"System.dll|System.Drawing.dll|Microsoft.CSharp.dll|DocX.dll", 0, "DahkX.exe", "/target:winexe")

; Run what you just compiled
RunWait, DahkX.exe

; Delay because sometimes it hiccups - not often
sleep, 100

; Get rid of the executable and .pdb file, comment this out if you want to keep them for some reason.
FileDelete, DahkX.exe
FileDelete, DahkX.pdb

; e finito tutto!
ExitApp
PS: DocX has been taken over as a commercial product here, although there is a free version to download from GitHub at this link. I don't know if any functionality has been stripped from the free version, but I sort of expect so - luckily I have this old Docx.dll still which is in the archive.

Below is a cropped shot of the Word document created. By the way, it does charts, lists, tables of contents, all kinds of good stuff!
docx-monkey.PNG
docx-monkey.PNG (15.89 KiB) Viewed 1777 times
Regards, and Happy Easter,
burque505
Attachments
DocX.zip
(100.61 KiB) Downloaded 152 times
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: DocX.dll from AHK

21 Apr 2019, 16:17

Congratulations on the great work! This is a very hard thing to do....
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: DocX.dll from AHK

21 Apr 2019, 16:46

Thanks, Marius, I appreciate it. Happy Easter!
Regards,
burque505
robodesign
Posts: 934
Joined: 30 Sep 2017, 03:59
Location: Romania
Contact:

Re: DocX.dll from AHK

21 Apr 2019, 17:19

Happy Easter to you too and keep up the great work you do!
-------------------------
KeyPress OSD v4: GitHub or forum. (presentation video)
Quick Picto Viewer: GitHub or forum.
AHK GDI+ expanded / compilation library (on GitHub)
My home page.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Chunjee, Google [Bot], gwarble and 69 guests