Author Topic: lisp to read ms word documents  (Read 5205 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
lisp to read ms word documents
« on: March 01, 2013, 10:25:35 AM »

is it possible?

any tutorials?

any examples I can learn from?

thanks

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: lisp to read ms word documents
« Reply #1 on: March 02, 2013, 05:25:55 AM »
It would be similar to reading Excel files. I.e. you link to Word through ActiveX and open or use an already opened document. From there you'll need to figure out how the Word ActiveX library works - do some googling on Word VBA.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

andrew_nao

  • Guest
Re: lisp to read ms word documents
« Reply #2 on: March 04, 2013, 09:59:32 AM »
ive been googling it for a couple days before i asked here
i dont know what vba code to look for
and anything lisp wise for this is limited.
i was hoping maybe someone here had a link or a tutorial or example code they would be willing to share so i could learn.

ill keep searching

ronjonp

  • Needs a day job
  • Posts: 7529
Re: lisp to read ms word documents
« Reply #3 on: March 04, 2013, 10:08:32 AM »
See what this does:

Code - Auto/Visual Lisp: [Select]

The .12 portion of the string may be different depending on the version of Office you have installed.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

andrew_nao

  • Guest
Re: lisp to read ms word documents
« Reply #4 on: March 04, 2013, 10:28:29 AM »
See what this does:

Code - Auto/Visual Lisp: [Select]

The .12 portion of the string may be different depending on the version of Office you have installed.

lol here is what is returned.. ill have to go thru this a little at a time but maybe someone else could use the info

ronjonp

  • Needs a day job
  • Posts: 7529
Re: lisp to read ms word documents
« Reply #5 on: March 04, 2013, 11:03:14 AM »
This might be a little more help. See if you can pick this apart  :-)

Code: [Select]
(defun c:test (/ doc docs result wa words)
  (and (setq wa (vlax-get-or-create-object "Word.Application"))
       (setq docs (vlax-get wa 'documents))
       (setq doc (vlax-invoke docs 'open "C:\\test.docx"))
       (setq words (vlax-get doc 'paragraphs))
       (vlax-for word words (setq result (cons (vlax-get (vlax-get word 'range) 'text) result)))
       (foreach line (reverse result) (alert line))
  )
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

andrew_nao

  • Guest
Re: lisp to read ms word documents
« Reply #6 on: March 04, 2013, 11:11:17 AM »
thanks Ronjob, this is good learning code for me

one thing, i cant open the file now.

i need to close it but
(vla-close Docs) doesnt work whats missing?


andrew_nao

  • Guest
Re: lisp to read ms word documents
« Reply #7 on: March 04, 2013, 11:13:08 AM »
oopps never mind its closed.. dunno what that was about tho..

ronjonp

  • Needs a day job
  • Posts: 7529
Re: lisp to read ms word documents
« Reply #8 on: March 04, 2013, 11:16:15 AM »
You can use (vlax-invoke doc 'close) to close  :-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

andrew_nao

  • Guest
Re: lisp to read ms word documents
« Reply #9 on: March 04, 2013, 11:25:13 AM »
this is excellent

thanks again, sir

ronjonp

  • Needs a day job
  • Posts: 7529
Re: lisp to read ms word documents
« Reply #10 on: March 04, 2013, 11:36:23 AM »
Glad to help  8-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: lisp to read ms word documents
« Reply #11 on: March 04, 2013, 11:45:58 AM »
Be careful to release the Word Application object when operations have finished, and to also include the required error trapping to ensure that this object is safely released should an error occur whilst interfacing with the word document.

Also, you will need to use the Quit method of the Application object to close the Word Application before releasing the object, otherwise the winword.exe process will remain active with no possible way to access the application without terminating the process manually using the Task Manager.

In short, it is good programming practice to clean up after yourself  :-)

andrew_nao

  • Guest
Re: lisp to read ms word documents
« Reply #12 on: March 04, 2013, 11:59:56 AM »
Be careful to release the Word Application object when operations have finished, and to also include the required error trapping to ensure that this object is safely released should an error occur whilst interfacing with the word document.

Also, you will need to use the Quit method of the Application object to close the Word Application before releasing the object, otherwise the winword.exe process will remain active with no possible way to access the application without terminating the process manually using the Task Manager.

In short, it is good programming practice to clean up after yourself  :-)

thats funny i was just going to ask this.
i notice in an excel function it says this for error handling
would this be the same but with my variables?

Code: [Select]
  (defun *error* (msg)
    (and msg
(/= msg "Function Canceled")
(princ (strcat "\nError: " msg))
    )
    (and wBook (vlax-invoke-method wBook 'Close :vlax-False))
    (and xlApp (vlax-invoke-method xlApp 'Quit))
    (and reg (vlax-release-object reg))
    (mapcar (function (lambda (obj) (and obj (vlax-release-object obj))))
    (list rng wBook xlApp)
    )
    (gc)
  )

also the doc file opens and closes after running the code
i tried
(vla-put-visible doc :vlax-false)
but that doesnt work, whats the equivalent of this?
or how do stop the doc from popping up and closing?

where should i be looking for the vlax functions?
« Last Edit: March 04, 2013, 12:03:57 PM by andrew_nao »

ronjonp

  • Needs a day job
  • Posts: 7529
Re: lisp to read ms word documents
« Reply #13 on: March 04, 2013, 12:28:23 PM »
To hide Word try using (vlax-put wa 'visible 0).

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

andrew_nao

  • Guest
Re: lisp to read ms word documents
« Reply #14 on: March 04, 2013, 01:56:18 PM »
how can i get the header and footer info?

andrew_nao

  • Guest
Re: lisp to read ms word documents
« Reply #15 on: March 04, 2013, 02:06:25 PM »
Error: bad list: "this is a test\r"ActiveX Server returned an
error: The object invoked has disconnected from its clients

any clue what this means?

andrew_nao

  • Guest
Re: lisp to read ms word documents
« Reply #16 on: March 05, 2013, 03:40:22 PM »
does anyone know how i can read a header or footer?
i just am not seeing where in the property of the document the header is called out.


irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: lisp to read ms word documents
« Reply #17 on: March 06, 2013, 12:35:19 AM »
You might need to "select" something in the document. It seems the header / footer collection is only obtainable through the selection object:
http://msdn.microsoft.com/en-us/library/office/aa172758%28v=office.11%29.aspx
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

andrew_nao

  • Guest
Re: lisp to read ms word documents
« Reply #18 on: March 06, 2013, 08:45:56 AM »
im prety sure ive been thru all the properties and the properties of properties of the document and i didnt see anything remotely close to being a header but i will go thru it again now that ive had some sleep and im fresh to go.

andrew_nao

  • Guest
Re: lisp to read ms word documents
« Reply #19 on: March 06, 2013, 09:08:54 AM »
found it

Command: (vlax-dump-object (vlax-get-property (vlax-get-property
(vlax-get-property(vlax-get-property wa 'ActiveDocument)'sections) 'First)
'Headers))
; HeadersFooters: nil
; Property values:
;   Application (RO) = #<VLA-OBJECT _Application 0000000021ef4af8>
;   Count (RO) = 3
;   Creator (RO) = 1297307460
;   Parent (RO) = #<VLA-OBJECT Section 00000000381311e8>
;   _NewEnum (RO) = #<IUnknown 0000000033591a20>
T

now how to get the text inside the header?

ronjonp

  • Needs a day job
  • Posts: 7529
Re: lisp to read ms word documents
« Reply #20 on: March 06, 2013, 09:41:40 AM »
Use vlax-for or vla-item.

Code: [Select]
;; Gets all the text and appends them to list 'l'
(vlax-for x (setq headers (vlax-get (vlax-get (vlax-get doc 'sections) 'first) 'headers))
  (setq l (cons (vlax-get (vlax-get x 'range) 'text) l))
)
;; Gets the item at index 1 <-- Use this to drill down and find properties and methods in the VLIDE
(vla-item headers 1)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

andrew_nao

  • Guest
Re: lisp to read ms word documents
« Reply #21 on: March 06, 2013, 10:01:09 AM »
Use vlax-for or vla-item.

ahhh i was missing something..

i was using

 (setq wrds (cons (vlax-get (vlax-get word 'range) 'text) wrds))
but was getting
; error: bad argument type: VLA-OBJECT nil

thanks for the heads up