Racket COM error opening Word document: expected argument of type <(opt (box any))>

29 views
Skip to first unread message

Scott Gudeman

unread,
Oct 8, 2019, 12:51:30 PM10/8/19
to Racket Users
I am working on converting a powershell script to Racket that takes a Word XML document and saves it as PDF.

In powershell, when I call Word's `Documents.Open` method I can just pass it the naked string as the file path without anything special done to it. In Racket, however, when I run the following code I get an error telling me the type is bad

```
  (define xml-document (string-append (path->string (path->complete-path (build-path (find-system-path 'orig-dir)  "temp"))) "\\" tag ".xml"))
  (define pdf-document (string-append (path->string (simplify-path (path->complete-path (build-path 'up "formatted")) #t)) "\\" tag ".pdf"))
  (define word-instance (com-create-instance "Word.Application"))
  (define word-document (com-invoke (com-get-property word-instance "Documents") "Open" xml-document))
  (com-invoke book "ExportAsFixedFormat"
      pdf-document 
      17 com-omit com-omit com-omit com-omit com-omit com-omit com-omit com-omit 
      1)
  (com-invoke book "Close")
  (com-invoke word-instance "Quit")
  (com-release word-instance)

```
This gives the following error on the line the **define word-document** 

> Open: expected argument of type <(opt (box any))>; given:
> "...\\pdf_gen\\temp\\filename.xml"


I am very confused by this as the documentation suggests that I should be able to plug in the path as a string, the error suggests otherwise?

I've tried both the regular and CS versions of Racket 7.4

Relevant Racket and Word documentation:


Shu-Hung You

unread,
Oct 8, 2019, 2:02:59 PM10/8/19
to Scott Gudeman, Racket Users
I don't know COM but here's an attempt to guess what happened based on
the documentation.

On Tue, Oct 8, 2019 at 11:51 AM Scott Gudeman <sdgu...@gmail.com> wrote:
>
> I am working on converting a powershell script to Racket that takes a Word XML document and saves it as PDF.
>
> In powershell, when I call Word's `Documents.Open` method I can just pass it the naked string as the file path without anything special done to it. In Racket, however, when I run the following code I get an error telling me the type is bad
>
> ```
> (define xml-document (string-append (path->string (path->complete-path (build-path (find-system-path 'orig-dir) "temp"))) "\\" tag ".xml"))
> (define pdf-document (string-append (path->string (simplify-path (path->complete-path (build-path 'up "formatted")) #t)) "\\" tag ".pdf"))
> (define word-instance (com-create-instance "Word.Application"))
> (define word-document (com-invoke (com-get-property word-instance "Documents") "Open" xml-document))
> (com-invoke book "ExportAsFixedFormat"
> pdf-document
> 17 com-omit com-omit com-omit com-omit com-omit com-omit com-omit com-omit
> 1)
> (com-invoke book "Close")
> (com-invoke word-instance "Quit")
> (com-release word-instance)
>
> ```
> This gives the following error on the line the **define word-document**
>
> > Open: expected argument of type <(opt (box any))>; given:
> > "...\\pdf_gen\\temp\\filename.xml"
>
>
> I am very confused by this as the documentation suggests that I should be able to plug in the path as a string, the error suggests otherwise?
>

Based on the message, the method expects a value whose type
description is '(opt (box any)). From 5.17.1.9, this means that this
is an optional, call-by-reference (probably means a pointer in C
sense) argument and that on Racket's side it expects the callee to
modify the argument. I'm wondering if this would work:

(define word-document (com-invoke (com-get-property word-instance
"Documents") "Open" (box xml-document)))

5.17.1.9 COM Types
https://docs.racket-lang.org/foreign/com-auto.html#%28part._com-types%29


> I've tried both the regular and CS versions of Racket 7.4
>
> Relevant Racket and Word documentation:
>
> [Racket COM Documentation](https://docs.racket-lang.org/foreign/com-auto.html?q=com)
>
> [MS Word COM Open Method](https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.documents.open?view=word-pia#parameters)
>
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/423334b3-607e-4041-b046-06528c0ea5d7%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages