Jump to content

Save Text into a Libre office writer File


mhy3sx

Recommended Posts

Hi, I have this code to Save Text into a MS Word File. I use Libre office now and the code don't work because try to find MS Office. I change the follow lines in the code but is not working ,gives me this message. Can any one help ?

 

Can't create Word document. Microsoft Office must be installed

 

 

   (if (not (setq Word (vlax-get-object "Libre.Application")));;;is already open ?
    (setq Word (vlax-get-or-create-object "Libre.Application"));;; no open

 

 

 

;;; CADALYST 08/05	Tip 2053: SaveDoc.lsp	Text into a MS Word File (c) Andrzej Gumula


;;; This routine exports selected text to Word document
;;; Microsoft Office must be installed in Windows
;;; *********************************************************
;;; Andrzej Gumula
;;; ul. Modrzewiowa 19/54
;;; 40-171 Katowice POLAND
;;; email: a.gumula@wp.pl

(defun c:Save2Doc (/ TxtSet Word Docs NewDoc Paragraphs Range
                    OldTxtList NewTxt IdTxtList FontName Txt Count
		    Flag#1 Flag#2)

(defun Dxf (Index)
 (cdr (assoc Index (entget (ssname TxtSet Count))))
);end Dxf

(defun ClearMTFormat (Str / Item TLength Char New);;; clear mtext format in string
  (setq Item 1 TLength (strlen Str) New "")
  (while (<= Item TLength)
   (setq Char (substr Str Item 1))
   (if	(= Char "\\")				
     (progn
      (setq Item (1+ Item))		
      (setq Char (substr Str Item 1))
      (cond
       ((member Char '("\\" "f" "F" "C" "H" "S" "T" "Q" "W"))
        (while (and (/= Char ";") (<= Item TLength))
         (setq Item (1+ Item))
         (setq Char (substr Str Item 1))
        );end while
       )
       ((= Char "P")
        (setq New (strcat New "\n"))
       )
       ((member Char '("{" "}"))
	(setq New (strcat New Char))
       )
      );end cond
     );end progn
     (if (not (member Char '("{" "}")))
      (setq New (strcat New Char))			
     );end if
    );end if
    (setq Item (1+ Item))
   );end while
   (cond (New) (T ""))
);end ClearMTFormat

(defun GetOpenDocs (Docs / Item Names);;; list of open Word documents
  (repeat (setq Item (vla-get-count Docs))
   (setq Names (cons (strcase (findfile (vla-get-fullname (vla-item Docs Item)))) Names)) 
   (setq Item (1- Item))
  );end repeat
  Names
);end GetOpenDocs
  
(princ "\nSelect TEXT (MTEXT) to export to Word document: ")
(cond 
 ((setq TxtSet (ssget '((0 . "*TEXT"))))
  (cond
  ((setq File (getfiled "Select Word document" (strcat (vl-filename-base (getvar "dwgname")) ".doc") "doc" 1))
   (vl-load-com)
   (prompt "\nExport text to Word. Please wait...")
   (princ)
   (if (not (setq Word (vlax-get-object "Word.Application")));;;is already open ?
    (setq Word (vlax-get-or-create-object "Word.Application"));;; no open
    (setq Flag#1 T)
   );end if
   (cond
    (Word
    (if (not Flag#1) (vla-put-visible Word :vlax-false));;; hide window application
    (if (findfile File)
     (setq Flag#2 (member (strcase (findfile File)) (GetOpenDocs (vlax-get-property Word 'Documents)))
	   NewDoc (vlax-invoke-method (vlax-get-property Word 'Documents) 'Open File))
     (setq  NewDoc (vlax-invoke-method (vla-get-documents Word) 'add))
    );end if
    (setq Paragraphs (vlax-get-property NewDoc 'Paragraphs) Count 0)  
    (repeat (sslength TxtSet)
     (setq String (vla-get-TextString (vlax-ename->vla-object (ssname TxtSet Count))))
     (setq Range (vlax-get-property (vlax-get-property Paragraphs 'last) 'Range))
     (if (not (setq FontName (cdar (cdadr (assoc -3 (entget (tblobjname "STYLE" (Dxf 7)) '("ACAD")))))))
      (setq FontName (vl-filename-base (cdr (assoc 3 (tblsearch "STYLE" (Dxf 7))))))
     );end if
     (vlax-put-property (vlax-get-property Range 'Font) 'Name FontName)
     (if (= (Dxf 0) "MTEXT") (setq String (ClearMTFormat String)))
     (vlax-invoke-method Range 'InsertAfter (strcat String "\n"))
     (setq Count (1+ Count))
    );end repeat
    (if (vl-catch-all-error-p (vl-catch-all-apply 'vla-saveas (list NewDoc File)))
      (prompt "\nProbably selected file is read-only. Cannot export text to this file. ")
      (vla-saveas NewDoc File);;; save document
    );end if
    (cond
     ((not Flag#1)
      (vla-quit Word 0)
     )
     (T (if (not Flag#2) (vla-close NewDoc));;; close application
        (vla-put-visible Word :vlax-true);;; show application
     )
    );end cond
    (mapcar 'vlax-release-object (list Word NewDoc Paragraphs Range));;; objects release
    (mapcar '(lambda (x) (set x nil)) '(Word NewDoc Paragraphs Range));;; null all variables
    )
   (T (prompt "\nCan't create Word document. Microsoft Office must be installed. "))
  );end cond Word
  )
  (T (prompt "\nFile no selected. "))
 );end cond
 )
 (T (prompt "\nNothing selected. "));;; text no selected
);end cond
 (princ)
);end c:Save2Doc

(defun c:SD ()
  (c:Save2Doc)
);end c:SD

(prompt "\nLoaded new command Save2Doc [SD]. ")
(prompt "\n[c]2004 Andrzej Gumula. ")
(princ)

;************** in the future - maybe in next version ?
;(vlax-invoke-method Range 'InsertSymbol 176 nil)   ;degree%%d
;(vlax-invoke-method Range 'InsertSymbol 177 nil t) ;plus-minus%%p
;(vlax-invoke-method Range 'InsertSymbol 216 nil t) ;diameter%%c

 

 

Thanks

Link to comment
Share on other sites

To talk to libre office is very different to Excel & Word Microsoft products. This is the start. 

(setq oServiceManager (vlax-get-or-create-object "com.sun.star.ServiceManager"))

I have started to look at Libre making some functions similar to my Excel functions. It has a bit of a low priority unfortunately. There is code out there most of it is in VBA which is OK. 

 

Here is a lot of word functions do apologise as no Author name. Could use that as a start for more functionality.

 

Just Google it is a long search start by joining Libre Office forums can look for stuff there.

 

It may be easier to go other way open libre and run a macro that reads text. have a look at the Excel macros.

 

write text to word.lsp draw object xl acad.xlsm

Link to comment
Share on other sites

Like SLW210 the syntax calls to Libre are very different to Excel / Word so it takes time googling to find the correct answers. I have write to a cell in Libre Calc but that is as far as I have gone.

 

This will open an existing Libre odt file. Could be blank like a dwt.

 

(setq filename "d:\\acadtemp\\Test 1.odt")

(setq oServiceManager (vlax-get-or-create-object "com.sun.star.ServiceManager"))
    (not
      (vl-catch-all-error-p
	(setq
	  oDesktop (vl-catch-all-apply
		     'vlax-invoke-method
		     (list oServiceManager 'createInstance "com.sun.star.frame.Desktop")
		   )
	)
      )
    )
    (not
      (vl-catch-all-error-p
	(setq
	  oCalcDoc (vl-catch-all-apply
		     'vlax-invoke-method
		     (list oDesktop
			   'loadComponentFromURL
			   (strcat "file:///" (vl-string-translate "\\" "/" FileName))
			   "_blank"
			   0
			   (vlax-make-safearray vlax-vbObject (cons 0 0))
		     )
		   )
	)
      )
    )

 

End of story for now, no idea how to write lines of text.

 

 

Edited by BIGAL
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...