Jump to content
Montinho

Control Word with visual lisp

Recommended Posts

Montinho

Hi,

can anyone help me with this?

i want to write in certain place in the middle of a microsoft word document.

 

:cry:

 

Thanks

Share this post


Link to post
Share on other sites
MSasu

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,

Share this post


Link to post
Share on other sites
fixo
Hi,

can anyone help me with this?

i want to write in certain place in the middle of a microsoft word document.

 

:cry:

 

Thanks

 

Lisp or VBA?

 

~'J'~

Share this post


Link to post
Share on other sites
Montinho

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.

Share this post


Link to post
Share on other sites
fixo
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'~

Share this post


Link to post
Share on other sites
Montinho

fixo, you're the man with the knowledge

 

i want write a word in a certain place.

Share this post


Link to post
Share on other sites
fixo
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

Share this post


Link to post
Share on other sites
Montinho

FIXO, you the man!!!

 

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

Share this post


Link to post
Share on other sites
fixo
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'~

Share this post


Link to post
Share on other sites
Montinho

Fixo,

 

How do you convert vba code in lisp?

Share this post


Link to post
Share on other sites
Lee Mac
Fixo,

 

How do you convert vba code in lisp?

 

Most of the methods/properties accessed in VBA are applicable in LISP :)

Share this post


Link to post
Share on other sites
fixo
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'~

Share this post


Link to post
Share on other sites
Montinho

Just one more question:

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

Share this post


Link to post
Share on other sites
fixo
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

Share this post


Link to post
Share on other sites
Montinho

Thanks fixo, your help has been crucial in my work.

 

Thank you very very much

Share this post


Link to post
Share on other sites
fixo

You're welcome

Glad to help

Cheers :)

 

~'J'~

Share this post


Link to post
Share on other sites
masoud_123
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

Share this post


Link to post
Share on other sites
Reinaldo Sampaio
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.

Share this post


Link to post
Share on other sites
Reinaldo Sampaio

Oh, I forgot about creating margins on MS Word document.

Thanks.

Share this post


Link to post
Share on other sites
BIGAL

Have you looked at what happens when you simply copy and paste mtext from Autocad to Word ?

Share this post


Link to post
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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...