FELIXJM Posted November 18, 2011 Share Posted November 18, 2011 I need to make one word bold in the text in MSWord. Also I need to do paragraph alignment to complete the program I am converting from VBA to AutoLISP. I am willing to contribute as consulting fees for this information. FIXO (Oleg Fateev), I think only you can help me (if still alive). Thank you, FELIX. Quote Link to comment Share on other sites More sharing options...
FELIXJM Posted November 24, 2011 Author Share Posted November 24, 2011 As I had no response to my request I have tried to solve this problem myself I was very necessary and after many many attempts I got the solution and I'm also interested in showing to whom. ;;; (vl-load-com) (setq MSWORDAPP (vlax-get-or-create-object "Word.Application")) (vla-put-visible MSWORDAPP :vlax-true) (setq docs (vla-get-documents MSWORDAPP)) (SETQ doc (vlax-invoke-method docs 'Add)) (setq paragraphs (vlax-get-property doc 'Paragraphs)) (setq pg (vlax-get-property paragraphs 'last)) (setq range (vlax-get-property pg 'range)) (vlax-put-property (vlax-get-property range 'font) 'name "COURIER NEW") (vlax-put-property (vlax-get-property range 'font) 'size 15) ; (setq WDRANGE (vlax-get-property (vlax-get-property (vlax-get-property MSWORDAPP 'ActiveDocument) 'WORDS) 'last)) (vlax-invoke-method range 'InsertAfter "TEXTO NEGRITO, ") (vlax-put-property range 'bold 1) ; (setq WDRANGE (vlax-get-property (vlax-get-property (vlax-get-property MSWORDAPP 'ActiveDocument) 'WORDS) 'last)) (vlax-invoke-method range 'InsertAfter "TEXTO NORMAL, ") (vlax-put-property WDrange 'bold 0) ; (setq WDRANGE (vlax-get-property (vlax-get-property (vlax-get-property MSWORDAPP 'ActiveDocument) 'WORDS) 'last)) (vlax-invoke-method range 'InsertAfter "TEXTO NEGRITO.\n") (vlax-put-property WDrange 'bold 1) ;;; Thank you. OK. Quote Link to comment Share on other sites More sharing options...
fixo Posted November 24, 2011 Share Posted November 24, 2011 Sorry, FELIXJM I was busy Glad you've solved it yourself Regards, ~'J'~ Quote Link to comment Share on other sites More sharing options...
fixo Posted November 24, 2011 Share Posted November 24, 2011 In addition there is my suggestion To avoid repeating call everywhere this line: (setq WDRANGE (vlax-get-property (vlax-get-property (vlax-get-property MSWORDAPP 'ActiveDocument) 'WORDS) 'last)) You have to write instead: - right after this line (setq doc (vlax-invoke-method docs 'Add)) - to be sure that your doc is active add 2 lines: (vlax-invoke-method doc 'Activate);<-- optional for newly created doc though, but keep it (setq doc (vlax-get-property MSWORDAPP 'ActiveDocument) - then in the rest part of code it would be look shortly (setq WDRANGE (vlax-get-property (vlax-get-property doc 'WORDS) 'last)) etc.. To simplify your work use subfunctions or how they called local defuns, i.e. something like this, just change it for your needs: (defun appendline (wrddoc fontname fontsize strtxt linespace isbold isitalic / bold endmark endrange fonto ital parformat) (setq paragraphs (vlax-get-property doc 'Paragraphs)) (setq para (vlax-get-property paragraphs 'Last)) (setq endrange (vlax-get-property para 'Range)) (setq parformat (vlax-get-property endrange 'ParagraphFormat)) (vlax-put-property parformat 'SpaceAfter linespace) (setq fonto (vlax-get-property endrange 'Font)) (vlax-put-property fonto 'Name fontname) (vlax-put-property fonto 'Size fontsize) (if isbold (vlax-put-property fonto 'Bold :vlax-true) (vlax-put-property fonto 'Bold :vlax-false)) (if isitalic (vlax-put-property fonto 'Italic :vlax-true) (vlax-put-property fonto 'Italic :vlax-false)) ;; << you can add other properies here >> (vlax-invoke-method endrange 'InsertAfter strtxt) ) Then inside the code invoke it, e.g.: (appendline doc "Arial" 14 "Text not bold, not italic, font \"Arial\", size 14, space after 18\n" 18 nil nil) (appendline doc "Verdana" 10 "Text bold, italic, font \"Verdana\", size 10, space after 12\n" 12 t t) Quote Link to comment Share on other sites More sharing options...
FELIXJM Posted November 24, 2011 Author Share Posted November 24, 2011 Thank you for the return but the code I posted is a small example, in my entire program uses many FUNCTIONS to simplify the code. OK. Quote Link to comment Share on other sites More sharing options...
fixo Posted November 25, 2011 Share Posted November 25, 2011 Thank you for the return but the code I posted is a small example, in my entire program uses many FUNCTIONS to simplify the code. OK. Here you go (it's a scratch only) (vl-load-com) ;| initialize the word application, return vla-object|; (defun InitWordApp(/ wrdapp) (if (not (vl-catch-all-error-p (setq wrdapp (vl-catch-all-apply '(lambda()(vlax-get-or-create-object "Word.Application")))))) (vla-put-visible wrdapp :vlax-true)) (cond (wrdapp wrdapp) ) );call: (setq wrdapp (InitWordApp)) (defun QuitApp (app ) (cond ((not (vlax-object-released-p app)) (vl-catch-all-apply 'vlax-invoke-method (list app 'Quit)) (vl-catch-all-apply 'vlax-release-object (list app)) ) ) );call: (QuitApp wrdapp) (defun Release (obj) (cond ((not (vlax-object-released-p obj)) (vl-catch-all-apply 'vlax-release-object (list obj)) ) ) ) ;| open existing document (no error check if the file exists)|; (defun OpenDoc (wrdapp name readonly openform / doc docs) (setq docs (vla-get-documents wrdapp)) (if (not readonly) (setq doc (vlax-invoke-method docs 'Open name :vlax-false (if openform openform)));open for write (setq doc (vlax-invoke-method docs 'Open name :vlax-true (if openform openform)));open as readonly ) (vlax-invoke-method doc 'Activate) (setq doc (vlax-get-property wrdapp 'ActiveDocument)) doc ) ;| create a new document and return it |; (defun AddNewDoc (wrdapp / doc docs) (setq docs (vla-get-documents wrdapp)) (setq doc (vlax-invoke-method docs 'Add)) (vlax-invoke-method doc 'Activate) (setq doc (vlax-get-property wrdapp 'ActiveDocument)) doc ) ;| save document |; (defun SaveDoc (doc ) (vlax-invoke-method doc 'Activate) (vlax-invoke-method doc 'Save) (vlax-invoke-method doc 'Activate);<-- to suit ) ;| save document as |; (defun SaveDocAs (doc name docformat / ) (vlax-invoke-method doc 'Activate) (vlax-invoke-method doc 'SaveAs name docformat) (vlax-invoke-method doc 'Activate);<-- to suit doc ) ;| close document |; (defun CloseDoc (doc saveform) (vlax-invoke-method doc 'Activate) (if (not saveform) (vlax-invoke-method doc 'Close) (vlax-invoke-method doc 'Close saveform) ) ) ;| get last paragraph |; (defun LastLine(wrddoc / para pars) (setq pars (vlax-get-property wrddoc 'Paragraphs)) (setq para (vlax-get-property pars 'last)) para ) ;| get last paragraph range |; (defun LastRange(wrddoc / para pars rang) (setq pars (vlax-get-property wrddoc 'Paragraphs)) (setq para (vlax-get-property pars 'Last)) (setq rang (vlax-get-property para 'Range)) rang ) ; | change paragraph alignment (used last paragraph) |; (defun ApplyAlignmentLast (wrddoc typ / para parform parrange) ;usual types are: 0 1 2 (setq para (lastline wrddoc)) (setq parrange (vlax-get-property para 'Range)) (setq parform (vlax-get-property parrange 'ParagraphFormat)) (vlax-put-property parform 'Alignment (vlax-make-variant typ 3)) ) ; | change paragraph alignment |; (defun ApplyAlignment (para typ / parform parrange) ;usual types are: 0 1 2 (setq parform (vlax-get-property para 'ParagraphFormat)) (vlax-put-property parform 'Alignment (vlax-make-variant typ 3)) ) ;| append text using current text format |; (defun AppendText (wrddoc strtext); if this is not a first line, then the string must be starts with carret char, i.e.: "\nYour text here..." (setq emark (vla-item (vlax-get-property wrddoc 'bookmarks) "[url="file://\\endofdoc"]\\endofdoc[/url]")) (setq erange (vlax-get-property emark 'range)) (vlax-put-property erange 'Text strtext) ) (defun c:TW(/ doc wd) (setq wd (InitWordApp)) ;; add new document: (setq doc (AddNewDoc wd )) ;; for existing file: ;(setq doc (opendoc wd "C:\\Test\\felix.doc" nil 0));open for write (AppendText doc "\nWhat's next?") (setq rng (LastRange doc)) (ApplyAlignment rng 1) (AppendText doc "\nWhat's next?") (setq rng (LastRange doc)) (ApplyAlignment rng 2) (AppendText doc "\nWhat's next?") (ApplyAlignmentLast doc 0) (SaveDocAs doc"C:\\Test\\felix.doc" ;|wdFormatDocumentDefault =|; 16); or "C:\\Test\\felix.docx" ;(SaveDoc doc);for existing file (CloseDoc doc nil) (Release doc) (QuitApp wd) (gc) (gc) (princ) ) Quote Link to comment Share on other sites More sharing options...
FELIXJM Posted November 25, 2011 Author Share Posted November 25, 2011 FIXED Thanks, I'll save for consultation where necessary. OK. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.