示例:将文本字符串从 MText 对象复制到 Microsoft Word 文档 (AutoLISP/ActiveX) 
以下示例创建一个与 Microsoft Word 2010 配合使用的 AutoLISP 应用程序和一个包含 MText 的 AutoCAD 图形。 
注意:AutoLISP 中的 ActiveX 支持仅限于 Windows。 
创建新的 AutoLISP 程序并加载 ActiveX 支持函数 
		 
		- 在 Visual LISP 中,单击“文件
 新建文件”。 
- 单击“文件
 另存为”。 
- 在“另存为”对话框中,
- 指定新 AutoLISP 源文件的位置。
 
- 在“文件名”框中,输入 copyMText2Word。
 
- 在“另存为类型”下拉列表中,选择“Lisp 源文件”。
 
- 点击保存。
 
 
 
		   
- 在新的 Visual LISP 文本编辑器窗口中,输入 (vl-load-com)。
 
 
 
	  
获取 AutoCAD 应用程序和当前 ModelSpace 对象并存储其指针 
		 
		
 
	  
导入 Microsoft Word 类型库 
		 
		更改以下代码中的 :tlb-filename 参数,以指向系统上的 msword.olb 文件。 
 
		- 在 Visual LISP 文本编辑器窗口中,输入
  (if (equal nil mswc-wd140Words) ; check for a Word constant
    (vlax-import-type-library
      :tlb-filename " C:/Program Files/Microsoft Office/Office14/msword.olb"
      :methods-prefix "mswm-"
      :properties-prefix "mswp-"
      :constants-prefix "mswc-"
    )
  )
 
			 此代码首先检查是否使用值定义了已知的 Microsoft Word 常量。如果常量具有值,则假定 Word 类型库已导入,无需进一步操作。如果变量为 ,则调用。nilvlax-import-type-library 
 
		   
 
 
	  
建立与 Microsoft Word 应用程序的连接 
		 
		- 在 Visual LISP 文本编辑器窗口中,输入
  (setq msw (vlax-get-object "Word.Application.14"))
  (if (equal nil msw)
    (progn
      ; Word is not running. Start it.
      (setq msw (vlax-create-object "Word.Application.14"))
      (vla-put-visible msw :vlax-true)
    )
  )
 
			 代码发出,以建立与正在运行的 Microsoft Word 应用程序的连接。(在此示例中,指定了版本 14(Word 2010);如果省略了 14 个版本,则将接受任何 Word 实例。如果没有正在运行的 Word 实例,则发出以启动一个实例。vlax-get-objectvlax-create-object 
 
		   
 
 
	  
在模型空间中处理 MText 对象 
		 
		- 在 Visual LISP 文本编辑器窗口中,输入
  (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
 
		   
 
 
	  
 
		加载并运行代码  
		 
		- 在 Visual LISP 中,单击 “Tools 
  ”> “在编辑器中加载文本”。 
- 在 AutoCAD 命令提示符下,输入 copymtext2word。
 
 
 
	  
    
 |