Work with MS COM objects (e.g. Word)

Thanks in advance for all help. I am in my first few minutes using Racket 8.7 on Win10 Pro 64-bit.
I want to work with Microsoft COM objects using Racket. Using ChatGPT I asked how to work with MS COM objects in Racket. I was told to install the 'ffi/com' pkg. I believe my installation to be missing the MzCOM binary. I don't know how to download it or compile it locally.

I searched for MzCOM on the forum and didn't find anything. Perhaps its use is deprecated?

Regards,
burque505

2 Likes

Hi and welcome to Racket!

Here is the documentation for ffi/com: 5.17.1 COM Automation . It doesn't seem to mention any MzCOM binaries. It does say the following however:

The ffi/com library is based on the MysterX library by Paul Steckler. MysterX is included with Racket but deprecated, and it will be replaced in the next version with a partial compatibility library that redirects to this one.

Note that I have never used ffi/com nor similar libraries.

By the way, my personal opinion is that you are still better off using traditional search engines rather than AI text generators, because the latter are what they are: generators of text which appears coherent, but which does not always fully make sense.

2 Likes

@scolobb, thanks for your quick response.
Regarding ChatGPT, though, it actually came up with some COM-related code that I wasn't able to easily find with the usual-suspect search engines. (Even if it was wrong :grinning:)
Maybe MzCOM isn't required, and probably shouldn't be for my purposes.

The following code works, so I at least have a Word object:


#lang racket/base
(require ffi/com)
(define word (com-create-instance "Word.Application"))

Now to actually access methods and properties ...

Regards,
burque505

1 Like

By the way, in case anyone else is interested in this topic, I found this working code on the Rosetta Code site:

#lang racket
(require racket/lazy-require)
(lazy-require [ffi/com (com-create-instance com-release com-invoke)])
(define (speak text)
  (cond [(eq? 'windows (system-type))
         (define c (com-create-instance "SAPI.SpVoice"))
         (com-invoke c "Speak" text)
         (com-release c)]
        [(ormap find-executable-path '("say" "espeak"))
         => (λ(exe) (void (system* exe text)))]
        [else (error 'speak "I'm speechless!")]))
(speak "This is an example of speech synthesis.")
2 Likes

Some initial success with Word: I can create a COM instance, add a document, and type some text.

#lang racket/base
(require ffi/com)
(define word (com-create-instance "Word.Application"))
(com-set-property! word "Visible" 'True)
(define doc (com-get-property word "Documents"))
(com-invoke doc "Add")
(define selection (com-get-property word "Selection"))
(com-invoke selection "TypeText" "Hello to COM from Racket!")

Of use was the following, but executed in DrRacket (nearly the same code, but outputs lists of methods and properties)

#lang racket/base
(require ffi/com)
(define word (com-create-instance "Word.Application"))
(com-set-property! word "Visible" 'True)
(define doc (com-get-property word "Documents"))
(com-invoke doc "Add")
(com-methods word)
(com-get-properties word)