The following example creates an AutoLISP application that works with Microsoft Word 2016 and an AutoCAD drawing that contains MText.
New File.
Save As.
(defun c:copyMText2Word ( / ) (setq *AcadApp* (vlax-get-acad-object)) ; Get AutoCAD application (setq *ModelSpace* (vla-get-ModelSpace (vla-get-ActiveDocument *AcadApp*))) ; Get model space
Change the :tlb-filename argument in the following code to point to the msword.olb file on your system.
(if (equal nil mswc-wd160Words) ; check for a Word constant
(vlax-import-type-library
:tlb-filename "C:/Program Files (x86)/Microsoft Office/root/Office16/msword.olb"
:methods-prefix "mswm-"
:properties-prefix "mswp-"
:constants-prefix "mswc-"
)
)
This code first checks to see if a known Microsoft Word constant is defined with a value. If the constant has a value, it is assumed that the Word type library has already been imported and no further action is necessary. If the variable is nil, vlax-import-type-library is invoked.
(setq msw (vlax-get-object "Word.Application.16"))
(if (equal nil msw)
(progn
; Word is not running. Start it.
(setq msw (vlax-create-object "Word.Application.16"))
(vla-put-visible msw :vlax-true)
)
)
The code issues vlax-get-object to establish a connection to a running Microsoft Word application. (In this example, version 16—Word 2016—is specified; if the 16 were omitted, any instance of Word would be accepted.) If there is no running instance of Word, vlax-create-object is issued to start one.
(if (/= nil msw)
(progn
;; Get the document collection object.
(setq docs (vla-get-documents msw))
;; Add a new document
(setq doc (mswm-add docs))
;; Get the paragraphs of the document (to do some formatting)
(setq paragraphs (mswp-get-paragraphs doc))
;; Now iterate through the model space and export any mtext
;; every Mtext entity to Word.
(vlax-for ent *ModelSpace*
(if (equal (vla-get-ObjectName ent) "AcDbMText")
(progn
;; Get the following information from the Mtext entity:
;; o the text string
;; o the location of a corner of the text boundary
(setq text (vla-get-TextString ent)
textpos (vla-get-InsertionPoint ent)
arrayTextpos (vlax-variant-value textpos)
textinfo
(strcat
(rtos (vlax-safearray-get-element arrayTextpos 0) 2 2)
", "
(rtos (vlax-safearray-get-element arrayTextpos 1) 2 2)
", "
(rtos (vlax-safearray-get-element arrayTextpos 2) 2 2)
)
) ;_ end of setq
; Print some info (with formatting)
; Get the last paragraph in the document
(setq pg (mswp-get-last paragraphs))
; Obtain the range of the paragraph
(setq range (mswp-get-range pg))
; Do some formatting
(mswp-put-bold range 1) ;bold
(mswp-put-underline range mswc-wdUnderlineSingle) ;underline
; Insert info about the text at the end of the paragraph
(mswm-InsertAfter range
(strcat "AcDbMText at position " textinfo "\n"))
; Now show the text string (from the ACAD text entity)
(setq pg (mswp-get-last paragraphs))
(setq range (mswp-get-range pg))
(mswp-put-bold range 0)
(mswp-put-underline range mswc-wdUnderlineNone)
(mswm-InsertAfter range (strcat text "\n\n"))
) ;_ end of progn
) ;_ end of if AcDbMText
) ;_ end of vlax-for
) ;_ end of progn
(princ "\nNo Microsoft Word application found.\n")
)
) ;_ end of defun
Load Text in Editor.