Jump to content

Control Word with visual lisp


Montinho

Recommended Posts

AutoLISP / VisualLISP are languages supported by AutoCAD (and other CAD related software). To customize Word (or other Office tools) should pay attention to VBA (Visual Basic for Applications) language which is derived from Visual Basic with tools specific for each application where is implemented.

Use associated help with confidence since is very well structured and writhed or search for support on a related site/forum. Good luck!

 

Regards,

Link to comment
Share on other sites

I can do a lot of things inside excel, write cells, change color font, delete rows and columns, delete sheets, hide sheets, all this throught visual lisp, and all this i discovered mostly by myself, testing and reading a few bits of this and that site.

But i find it very weird that i can do this, but found little documentation on the subject.

When you start writing vlax-xxxxxxx, you get a lot of functions. Yes there are a few tutorials that have a few functions explained, but not all, but on this area of activex controls for visual lisp i never found anything.

On microsoft word i can write on a new document, or on this or that paragraph, but something is missing.

I'm sure someone knows about it, someone always does, please give that little help that i need.

Link to comment
Share on other sites

I can do a lot of things inside excel, write cells, change color font, delete rows and columns, delete sheets, hide sheets, all this throught visual lisp, and all this i discovered mostly by myself, testing and reading a few bits of this and that site.

But i find it very weird that i can do this, but found little documentation on the subject.

When you start writing vlax-xxxxxxx, you get a lot of functions. Yes there are a few tutorials that have a few functions explained, but not all, but on this area of activex controls for visual lisp i never found anything.

On microsoft word i can write on a new document, or on this or that paragraph, but something is missing.

I'm sure someone knows about it, someone always does, please give that little help that i need.

 

Let me expalin what I understand

Do you want to find particular word inside and

then replace it with other one?

Or you need to add word into the certain place

without finding for the match word?

Say add empty paragraph and the add the word

Post a picture and we can start working something out

 

~'J'~

Link to comment
Share on other sites

fixo, you're the man with the knowledge

 

i want write a word in a certain place.

 

Play around this one

You can easily write your own user defined function

I called them 'helpers'

 

;;demo.lsp
;; helpers
(defun write_to_end  (wrdapp text / endrange)
 (setq	endrange
 (vlax-get-property
   (vlax-get-property
     (vlax-get-property
       (vlax-get-property wrdapp 'ActiveDocument)
       'Paragraphs)
     'Last
     )
   'Range
   )
)
 (vlax-invoke-method endrange 'InsertAfter text)
 )

(defun write_to_place  (wrdapp num text / endrange)
 (setq	endrange
    (vla-item (vlax-get-property
       (vlax-get-property wrdapp 'ActiveDocument)
       'Sentences) num
	      )
)
(vlax-invoke-method endrange 'InsertAfter text)
)
;; main prog
(defun C:demo(/ docname  wrdapp wrddoc wrddocs wrdrange)
 (setq	docname (getfiled "Select Word Document"
		 (getvar "dwgprefix")
		 "doc;dot;*"
		 16))
 (alert "Wait For Closing Word Application")
 (setq wrdapp (vlax-get-or-create-object "Word.Application"))
 (vla-put-visible wrdapp :vlax-true)
 (vlax-put-property wrdapp 'ScreenUpdating :vlax-false)
 (setq wrddocs (vlax-get-property wrdapp 'Documents))
 (vlax-invoke-method wrddocs 'Open docname :vlax-false)
 (setq wrddoc (vlax-get-property wrdapp 'Activedocument))
 (vlax-invoke-method wrddoc 'Activate)
 (write_to_end wrdapp  "Now it is the last line in the document" )
 (write_to_place wrdapp  2 "Now it is the first sentense in the second line in the document" )
     (vlax-put-property wrdapp 'ScreenUpdating :vlax-true)
   (vlax-invoke-method
     wrddoc
     'Saveas
     docname
     )
   (vlax-invoke-method wrddoc 'Close)
   (vlax-invoke-method wrdapp 'Quit)
   (mapcar (function (lambda (x)
		(vl-catch-all-apply
		  (function (lambda ()
			      (progn
				(vlax-release-object x)
				(setq x nil)
				)
			      )
			    )
		  )
		)
	      )
    (list wrddoc wrddocs wrdapp)
    )
 (gc)
 (princ)
 )

(prompt
 "\n\t\t>>>\tType DEMO to execute\t<<<"
)
(prin1)

 

~'J'~

  • Like 1
Link to comment
Share on other sites

FIXO, you the man!!!

 

but how do you know all this, is there any book, tutorial, manual whatever with all this?

 

I use Macro->Write Macro in Word/Excel VBA then convert

VBA code on Lisp nothing else

For every object I get properties/methods with help of:

(vlax-dum-object obj T)

and also I spent a lot of time to know all these things :)

 

~'J'~

Link to comment
Share on other sites

Fixo,

 

How do you convert vba code in lisp?

 

Actually there are no methods that converts code directly

I only use the knowledge (though also not so good)

of both languages as AutoLisp (VisualLisp) and VBA :)

 

~'J'~

Link to comment
Share on other sites

  • 3 months later...
Just one more question:

How can write in the header and footnotes in microsoft word?

 

Still sick mate so I haven't have a time to help you

Here is quick and dirty code

;;demo2.lsp
;; helpers:

(defun goto_start (wrdapp / sel)
 (setq sel (vlax-get-property wrdapp 'Selection))
 (vlax-invoke-method sel 'WholeStory)
 (vlax-invoke-method sel 'Collapse 1)
 (vlax-get-property wrdapp 'Selection)
 )

(defun write_footer (wrdapp text / block blocks range sel start wbasic)
 (setq wbasic (vlax-get-property wrdapp 'WordBasic))
 (vlax-invoke-method wbasic 'ViewFooterOnly)
 (setq blocks    (vlax-get-property
   (vlax-variant-value
  (vlax-get-property
  (vlax-get-property wrdapp 'ActiveDocument)
  'AttachedTemplate))
  'BuildingBlockEntries))
(setq block (vlax-invoke-method blocks 'Item 1))
   (setq sel (vlax-get-property wrdapp 'Selection))
   (setq range (vlax-get-property sel 'Range))
   (vlax-invoke-method block 'Insert range :vlax-true)
   (vlax-put-property  range 'Text "")   
   (vlax-invoke-method sel 'TypeText (vlax-make-variant text 12))
   (vlax-release-object wbasic)
 )

(defun write_header  (wrdapp text / acwnd block blocks range view)
 (setq view
   (vlax-get-property
     (setq acwnd
     (vlax-get-property
       (vlax-get-property wrdapp 'ActiveDocument)
       'ActiveWindow))
     'View
     )
   )
(if (not (zerop (vlax-get-property view 'SplitSpecial)))
 (progn
   (setq panes (vlax-get-property acwnd 'Panes))
   (setq pane (vlax-get-property 'Items 1))
   (vlax-invoke-method pane 'Close))
 )
   (if (or (= 1 (vlax-get-property
    (vlax-get-property
    (vlax-get-property
    (vlax-get-property
       (vlax-get-property wrdapp 'ActiveDocument)
       'ActiveWindow)
    'ActivePane)
    'View)
       'Type)
    )
    (= 2 (vlax-get-property
    (vlax-get-property
    (vlax-get-property
    (vlax-get-property
       (vlax-get-property wrdapp 'ActiveDocument)
       'ActiveWindow)
    'ActivePane)
    'View)
       'Type)))

     (vlax-put-property
(vlax-get-property
(vlax-get-property
(vlax-get-property
    (vlax-get-property
       (vlax-get-property wrdapp 'ActiveDocument)
       'ActiveWindow)
    'ActivePane)
'View)
'Type)
     3
     )
   )
   (vlax-put-property
(vlax-get-property
(vlax-get-property
    (vlax-get-property
       (vlax-get-property wrdapp 'ActiveDocument)
       'ActiveWindow)
    'ActivePane)
'View)
'SeekView
     9
     )
(setq blocks    (vlax-get-property
   (vlax-variant-value
  (vlax-get-property
  (vlax-get-property wrdapp 'ActiveDocument)
  'AttachedTemplate))
  'BuildingBlockEntries))
(setq block (vlax-invoke-method blocks 'Item 1))

   (setq start
   (goto_start wrdapp)
)
   (vlax-invoke-method start 'Select)
   (setq range (vlax-get-property start 'Range))
   (vlax-invoke-method block 'Insert range :vlax-true)
   (vlax-put-property  range 'Text "")   
   (vlax-invoke-method start 'TypeText (vlax-make-variant text 12))
 )

(defun write_to_end  (wrdapp text / endrange)
 (setq endrange
 (vlax-get-property
   (vlax-get-property
     (vlax-get-property
       (vlax-get-property wrdapp 'ActiveDocument)
       'Paragraphs)
     'Last
     )
   'Range
   )
)
 (vlax-invoke-method endrange 'InsertAfter text)
 )

(defun write_to_place  (wrdapp num text / endrange)
 (setq endrange
    (vla-item (vlax-get-property
       (vlax-get-property wrdapp 'ActiveDocument)
       'Sentences) num
       )
)
(vlax-invoke-method endrange 'InsertAfter text)
)

;; main prog
(defun C:demo2(/ docname  wrdapp wrddoc wrddocs wrdrange)
 (setq docname (getfiled "Select Word Document"
   (getvar "dwgprefix")
   "doc;dot;*"
   16))
 (alert "Wait For Closing Word Application")
 (setq wrdapp (vlax-get-or-create-object "Word.Application"))
 (vla-put-visible wrdapp :vlax-true)
 (vlax-put-property wrdapp 'ScreenUpdating :vlax-false)
 (setq wrddocs (vlax-get-property wrdapp 'Documents))
 (vlax-invoke-method wrddocs 'Open docname :vlax-false)
 (setq wrddoc (vlax-get-property wrdapp 'Activedocument))
 (vlax-invoke-method wrddoc 'Activate)
 (setq sel (goto_start wrdapp))
 (write_header wrdapp "Type Header Text Here")
 (write_to_end wrdapp  "Now it is the last line in the document" )
 (write_to_place wrdapp  2 "Now it is the first sentense in the second line in the document" )
 (write_footer wrdapp "Type Footer Text Here")
     (vlax-put-property wrdapp 'ScreenUpdating :vlax-true)
   (vlax-invoke-method
     wrddoc
     'Saveas
     docname
     )
   (vlax-invoke-method wrddoc 'Close)
   (vlax-invoke-method wrdapp 'Quit)
   (mapcar (function (lambda (x)
  (vl-catch-all-apply
    (function (lambda ()
         (progn
    (vlax-release-object x)
    (setq x nil)
    )
         )
       )
    )
  )
       )
    (list wrddoc wrddocs wrdapp)
    )
 (gc)
 (princ)
 )
(prompt
 "\n\t\t>>>\tType DEMO2 to execute\t<<<"
)
(prin1)

 

~'J'~

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
  • 5 years later...
You're welcome

Glad to help

Cheers :)

 

~'J'~

 

Hi fixo

Could you please help me with typing none-english (like Russian or Arabic) characters in VLISP functions and then exporting to MS Word using (vlax-invoke-method 'TypeText)?

 

Regards

Link to comment
Share on other sites

  • 1 year later...
Lisp or VBA?

 

~'J'~

Hi, My regards. I am a brazilian man trying to leanr more about vlisp.

I need information about all the functions to control text transfering from Autocad

to MS World, like formatting text fonts, bold, italic, borders to the document, and control where to write in the document. May be tabs, row and columns,etc...

Where to find these information? Book, e-book, tutorials.

Thanks in advance for your help.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...