AHK/CLR/OpenXml - Office docs w/o Office

Put simple Tips and Tricks that are not entire Tutorials in this forum
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

AHK/CLR/OpenXml - Office docs w/o Office

31 May 2018, 16:08

I've just used this for .docx so far, but it appears to be used extensively for .xlsx. Office not required!
Here's a very simple example for docx. I was able to open existing docs a long time ago, but it took me forever to be able to create one. I ran down a lot of blind alleys before I got there.
If you do a search for OfficeOpenXML, ooxml, or DocumentFormat.OpenXml you'll get tons of information.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.

; This requires DocumentFormat.OpenXml.dll be in the same directory.
; You may have to register it, but you shouldn't have to if
; it's in the same directory.

If (FileExist("SampleDoc2.docx"))
	FileDelete, SampleDoc2.docx

#Include <CLR>

vb =
(
	Imports System.IO
	Imports System.Runtime
    Imports DocumentFormat.OpenXml.Packaging
    Imports DocumentFormat.OpenXml.Wordprocessing
    Class Foo
        Public Sub Create()
		Dim wordDocPath As String = Directory.GetCurrentDirectory & "\SampleDoc2.docx"
		Dim type as Object = DocumentFormat.OpenXml.WordProcessingDocumentType.Document
        ' I had to declare "type" and then use it below.
        ' Just using "WordProcessingDocument.Create(wordDocPath, WordProcessingDocumentType.Document
        ' threw an error
        Using myDocument As WordprocessingDocument = WordprocessingDocument.Create(wordDocPath, type)
            ' Add a main part. 
            Dim mainPart As MainDocumentPart = myDocument.AddMainDocumentPart()

            ' Create the document structure.
            mainPart.Document = New Document()
            Dim body As Body = mainPart.Document.AppendChild(New Body())
            Dim para As Paragraph = body.AppendChild(New Paragraph())
            Dim run As Run = para.AppendChild(New Run())

            ' Add some text to the document.
            run.AppendChild(New Text("I created this with AHK and DocumentFormat.OpenXml."))
            Dim para2 As Paragraph = body.AppendChild(New Paragraph())
            Dim run2 As Run = para2.AppendChild(New Run())
			run2.AppendChild(New Text("It was like pulling teeth"))
            myDocument.MainDocumentPart.Document.Save()
        End Using
        End Sub
    End Class
)
asm := CLR_CompileVB(vb, "System.dll | System.IO.dll | DocumentFormat.OpenXml.dll ")
obj := CLR_CreateObject(asm, "Foo")
obj.Create()
Regards,
burque505
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: AHK/CLR/OpenXml - Office docs w/o Office

03 Apr 2019, 06:02

oh yes
this one is C#
works fine

Code: Select all

myCode =
(
	using System.IO;
	using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
	class myClass
	{
		public void myMethod()
		{           
			string myPath = Directory.GetCurrentDirectory() + @"\SampleDoc2.docx";  
			using(WordprocessingDocument wd = WordprocessingDocument.Create(myPath, WordprocessingDocumentType.Document))
			{
				MainDocumentPart m = wd.AddMainDocumentPart();
				m.Document = new Document();
				m.Document.AppendChild(new Body())
						  .AppendChild(new Paragraph())
						  .AppendChild(new Run())
						  .AppendChild(new Text("Hello Word !!"));
			}              
		}
	}
)
myReference = 
(	Join|
	System.IO.dll
	DocumentFormat.OpenXml.dll
	WindowsBase.dll
)
CLR_CreateObject(CLR_CompileC#(myCode, myReference), "myClass").myMethod()
Klarion
Posts: 176
Joined: 26 Mar 2019, 10:02

Re: AHK/CLR/OpenXml - Office docs w/o Office

03 Apr 2019, 06:34

DocumentFormat.OpenXml.dll version 250
WindowsBase.dll version 403
.NET Framework version 452
32bit (amazingly)
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: AHK/CLR/OpenXml - Office docs w/o Office

08 Apr 2019, 10:22

@Klarion, both your .docx and .xlsx samples work fine for me. I'm very happy to see you have had more luck with OOXML than I had. I appreciate your efforts.
Perhaps you would be interested in EPPlus with AHK (also no Excel needed) . I'm extremely interested in all projects that use Clr_LoadLibrary to interact with 3rd-party dlls, as I mentioned here.
ovidiugabriel
Posts: 9
Joined: 22 Jul 2018, 14:19

Re: AHK/CLR/OpenXml - Office docs w/o Office

19 Jul 2019, 03:30

Hello, It looks like a method is accessible only if it returns primitive types?
In my case DocumentFormat.OpenXml.Packaging.SpreadsheetDocument does not have any public constructors so I cannot create a SpreadsheetDocument using CLR_CreateObject.

The following code produces the error:

Error: 0x80131509 Specifically: Create

Most probably there is something wrong in the Create method and for this reason it is not available, but I don't receive any errors inside the method.

Do you have any idea what's wrong here?

Code: Select all

#Include CLR.ahk

code =
(
        using System;
        using System.Windows.Forms;
        using DocumentFormat.OpenXml;  
        using DocumentFormat.OpenXml.Packaging;  
        using DocumentFormat.OpenXml.Spreadsheet; 

    class _SpreadsheetDocument {
    	public SpreadsheetDocument Create() {
    		return SpreadsheetDocument.Create(@"C:\Users\ovidi\Downloads\challenge.xlsx", SpreadsheetDocumentType.Workbook);
    	}

    	public bool ReturnTrue() {
    		return true;
    	}
    }
)

asm := CLR_CompileC#(code, "DocumentFormat.OpenXml.dll | System.dll | System.Windows.Forms.dll | WindowsBase.dll" )
obj := CLR_CreateObject(asm, "_SpreadsheetDocument")
logCheck("obj", obj)

boolValue := obj.ReturnTrue()
logCheck("boolValue", boolValue)

spreadsheetDocument := obj.Create()
logCheck("spreadsheetDocument", spreadsheetDocument)
burque505
Posts: 1732
Joined: 22 Jan 2017, 19:37

Re: AHK/CLR/OpenXml - Office docs w/o Office

19 Jul 2019, 07:50

I had nothing but grief with it to start with, but try this post to see if it gives you any help.
Regards,
burque505

Return to “Tips and Tricks (v1)”

Who is online

Users browsing this forum: No registered users and 14 guests