Author Topic: Looking for a quick way to launch and write contents into MS word  (Read 6157 times)

0 Members and 1 Guest are viewing this topic.

TheRubens

  • Guest
Hi guys.

I am currently using MS word API to write contents into it. So I have tried functions like 'mswm-InsertAfter'. It works but it takes a bit long for the whole document to be completed. (It takes around 3 mins for a 6 pages document. I can see the MS word writing row and row and it is not very quick). I am wondering if there is any way to make this process quicker? Also. is there any way to hide the MS Word when it is launching?

Thank you.

BIGAL

  • Swamp Rat
  • Posts: 1398
  • 40 + years of using Autocad
Re: Looking for a quick way to launch and write contents into MS word
« Reply #1 on: July 08, 2019, 12:38:43 AM »
Why not Crtl A Ctrl C then Ctrl V in mtext its instant.
A man who never made a mistake never made anything

TheRubens

  • Guest
Re: Looking for a quick way to launch and write contents into MS word
« Reply #2 on: July 08, 2019, 05:04:03 AM »
Why not Crtl A Ctrl C then Ctrl V in mtext its instant.

Hi BIGAL, Thanks for the reply.
I want to print many lists as tables in MS word and I don't think copy and paste can achieve this. I just find methods like 'mswm-InsertAfter' is really slow. If I have a 20 pages doc to be printed it will take me 10 mins or more.

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: Looking for a quick way to launch and write contents into MS word
« Reply #3 on: July 08, 2019, 06:09:45 AM »
does it have to be word?

If not you could easily create a html file which you can view and print to pdf or hard copy as required? It would be a simple matter of setting up some simple html header markup, add you list data to table elements in a loop then add to closing markup and open in a browser.
I'd be happy to help out if you like.
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

TheRubens

  • Guest
Re: Looking for a quick way to launch and write contents into MS word
« Reply #4 on: July 08, 2019, 08:42:16 PM »
does it have to be word?

If not you could easily create a html file which you can view and print to pdf or hard copy as required? It would be a simple matter of setting up some simple html header markup, add you list data to table elements in a loop then add to closing markup and open in a browser.
I'd be happy to help out if you like.

Hi MickD, Thanks for your reply.
My preference is MS word (I wonder how other software can print as word that quick). I don't mind html and then to PDF as long as it is quciker though. Can you please give me some example of html. (I know nothing about html)
Thank you very much.

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: Looking for a quick way to launch and write contents into MS word
« Reply #5 on: July 08, 2019, 11:20:53 PM »
Something along these lines will work, this is NOT WORKING and is only a rough outline (my Lisp Fu is weak :) )

Code - Auto/Visual Lisp: [Select]
  1. ;; DATATOHTML --------------------------------------------------------- ;;
  2. ;; params:
  3. ;;    data - a list of records to add to tablet
  4. ;;    htmlFile - ptr to html file to save data to e.g. "C:\\datatest.html"
  5. ;; -------------------------------------------------------------------- ;;
  6. (defun dataToHtml ( data htmlFile / hdr tblhdr tbldatarow tblftr )
  7.   ;; create the html file pointer:
  8.   (setq file (open htmlFile "w"))
  9.  
  10.   ;; create the html5 header portion:
  11.   (setq hdr (strcat
  12.       "<!DOCTYPE html>"
  13.       "\n<html lang=\"en\">"
  14.       "\n<head>"          
  15.       "\n\t<meta charset=\"UTF-8\">"
  16.       "\n\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
  17.       "\n\t<meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\">"
  18.       "\n\n\t<title>Document</title>"
  19.       "\n</head>"
  20.       "\n\t<body>"
  21.       "\n\t\t<table>"   ;; create/open table tag
  22.       "\n\t\t\t<tr>"))  ;; create/open row tag
  23.   (write-line hdr file)
  24.        
  25.   ;; create the html table header titles:
  26.   (setq tblhdr (strcat
  27.       "\n\t\t\t\t<th>" "Name" "</th>"           ;; add column header
  28.       "\n\t\t\t\t<th>" "Description" "</th>"    ;; add column header
  29.       "\n\t\t\t\t<th>" "Length" "</th>"         ;; add column header
  30.       "\n\t\t\t\t<th>" "Qty" "</th>"            ;; add column header
  31.       "\n\t\t\t</tr>"))                         ;; close row tag
  32.   (write-line tblhdr file)  
  33.  
  34.   ;; loop through data records adding html table rows:
  35.   ;; something like(foreach val (data
  36.     (setq tbldatarow (strcat
  37.       "\n\t\t\t</tr>"))                       ;; open row tag
  38.       "\n\t\t\t\t<td>" name-val "</td>"           ;; add name column data
  39.       "\n\t\t\t\t<td>" desc-val "</td>"           ;; add desc column data
  40.       "\n\t\t\t\t<td>" length-val "</td>"         ;; add length column data
  41.       "\n\t\t\t\t<td>" qty-val "</td>"          ;; add qty column data
  42.       "\n\t\t\t</tr>"))                       ;; close row tag
  43.     (write-line tbldatarow file)
  44.     ;;(princ val)))
  45.  
  46.   ;; close off table and add html footer to close all html tags:
  47.   (setq tblftr (strcat
  48.     "\n\t\t\t</table>"  ;; close table tag
  49.     "\n\t\t\t\t<th>"    ;; close body tag
  50.     "\n\t\t\t\t<th>"))  ;; close html tag
  51.    
  52.   (write-line tblftr file)
  53.  
  54.   ;; close the file:
  55.   (close file)
  56.  
  57.   ;; open the file in the default web browser:
  58.   ;; TODO
  59.  
  60.   (princ)
  61. )
  62.  

Aside:
Is there a way to create 'literal' strings like using the '@' in C#??
I could see way of creating templates for this sort of thing if so ;)
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

TheRubens

  • Guest
Re: Looking for a quick way to launch and write contents into MS word
« Reply #6 on: July 09, 2019, 12:42:27 AM »
Something along these lines will work, this is NOT WORKING and is only a rough outline (my Lisp Fu is weak :) )

Code - Auto/Visual Lisp: [Select]
  1. ;; DATATOHTML --------------------------------------------------------- ;;
  2. ;; params:
  3. ;;    data - a list of records to add to tablet
  4. ;;    htmlFile - ptr to html file to save data to e.g. "C:\\datatest.html"
  5. ;; -------------------------------------------------------------------- ;;
  6. (defun dataToHtml ( data htmlFile / hdr tblhdr tbldatarow tblftr )
  7.   ;; create the html file pointer:
  8.   (setq file (open htmlFile "w"))
  9.  
  10.   ;; create the html5 header portion:
  11.   (setq hdr (strcat
  12.       "<!DOCTYPE html>"
  13.       "\n<html lang=\"en\">"
  14.       "\n<head>"          
  15.       "\n\t<meta charset=\"UTF-8\">"
  16.       "\n\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
  17.       "\n\t<meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\">"
  18.       "\n\n\t<title>Document</title>"
  19.       "\n</head>"
  20.       "\n\t<body>"
  21.       "\n\t\t<table>"   ;; create/open table tag
  22.       "\n\t\t\t<tr>"))  ;; create/open row tag
  23.   (write-line hdr file)
  24.        
  25.   ;; create the html table header titles:
  26.   (setq tblhdr (strcat
  27.       "\n\t\t\t\t<th>" "Name" "</th>"           ;; add column header
  28.       "\n\t\t\t\t<th>" "Description" "</th>"    ;; add column header
  29.       "\n\t\t\t\t<th>" "Length" "</th>"         ;; add column header
  30.       "\n\t\t\t\t<th>" "Qty" "</th>"            ;; add column header
  31.       "\n\t\t\t</tr>"))                         ;; close row tag
  32.   (write-line tblhdr file)  
  33.  
  34.   ;; loop through data records adding html table rows:
  35.   ;; something like(foreach val (data
  36.     (setq tbldatarow (strcat
  37.       "\n\t\t\t</tr>"))                       ;; open row tag
  38.       "\n\t\t\t\t<td>" name-val "</td>"           ;; add name column data
  39.       "\n\t\t\t\t<td>" desc-val "</td>"           ;; add desc column data
  40.       "\n\t\t\t\t<td>" length-val "</td>"         ;; add length column data
  41.       "\n\t\t\t\t<td>" qty-val "</td>"          ;; add qty column data
  42.       "\n\t\t\t</tr>"))                       ;; close row tag
  43.     (write-line tbldatarow file)
  44.     ;;(princ val)))
  45.  
  46.   ;; close off table and add html footer to close all html tags:
  47.   (setq tblftr (strcat
  48.     "\n\t\t\t</table>"  ;; close table tag
  49.     "\n\t\t\t\t<th>"    ;; close body tag
  50.     "\n\t\t\t\t<th>"))  ;; close html tag
  51.    
  52.   (write-line tblftr file)
  53.  
  54.   ;; close the file:
  55.   (close file)
  56.  
  57.   ;; open the file in the default web browser:
  58.   ;; TODO
  59.  
  60.   (princ)
  61. )
  62.  

Aside:
Is there a way to create 'literal' strings like using the '@' in C#??
I could see way of creating templates for this sort of thing if so ;)


Thank you very much MickD. I will have a look. Just wondering if there's any way to transfer HTML to PDF using script instead of doing it manually?
(Sorry but I am not familiar with C#)

MickD

  • King Gator
  • Posts: 3619
  • (x-in)->[process]->(y-out) ... simples!
Re: Looking for a quick way to launch and write contents into MS word
« Reply #7 on: July 09, 2019, 01:35:28 AM »
Something like this might come in handy -> https://wkhtmltopdf.org/

Also note that you may want to style your tables and other output but it's really not that hard once you get going and you can embed the css styling in the html using <style> tags rather than importing a css file into the html but either way will work fine. It might sound like a bit of work but this way will be much more configurable than trying to automate styling etc through word and creating text files is pretty quick.

A simpler way may be to use Chrome as 'headless':
Quote
C:\Program Files\Google\Chrome\Application> chrome.exe --headless --disable-gpu --print-to-pdf=file1.pdf http://www.example.com/
"Short cuts make long delays,' argued Pippin.”
J.R.R. Tolkien

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: Looking for a quick way to launch and write contents into MS word
« Reply #8 on: July 09, 2019, 02:36:14 AM »
It takes around 3 mins for a 6 pages document.
this is too much, i'm sure something is wrong with you code
we have not seen it and we can only guess
have you set "ScreenUpdating" property to false?

MickD's suggestion about html is quite good
i personally do it that way
yes it is very very fast, hundreds of pages are generated almost instantly

there's no need to convert html to pdf
just save you html file with 'doc' extension, msword will open it without questions

msword also supports 'XML Document' format
i use it for some 'sophisticated' documents, when html is not enough

TheRubens

  • Guest
Re: Looking for a quick way to launch and write contents into MS word
« Reply #9 on: July 09, 2019, 04:58:54 AM »
Something like this might come in handy -> https://wkhtmltopdf.org/

Also note that you may want to style your tables and other output but it's really not that hard once you get going and you can embed the css styling in the html using <style> tags rather than importing a css file into the html but either way will work fine. It might sound like a bit of work but this way will be much more configurable than trying to automate styling etc through word and creating text files is pretty quick.

A simpler way may be to use Chrome as 'headless':
Quote
C:\Program Files\Google\Chrome\Application> chrome.exe --headless --disable-gpu --print-to-pdf=file1.pdf http://www.example.com/

Thank you. I am new to html. I will spend sometime understanding this. Thanks again :)

TheRubens

  • Guest
Re: Looking for a quick way to launch and write contents into MS word
« Reply #10 on: July 09, 2019, 05:01:42 AM »
It takes around 3 mins for a 6 pages document.
this is too much, i'm sure something is wrong with you code
we have not seen it and we can only guess
have you set "ScreenUpdating" property to false?

MickD's suggestion about html is quite good
i personally do it that way
yes it is very very fast, hundreds of pages are generated almost instantly

there's no need to convert html to pdf
just save you html file with 'doc' extension, msword will open it without questions

msword also supports 'XML Document' format
i use it for some 'sophisticated' documents, when html is not enough

Hi VovKa, Thanks for your reply.
I tried to set the property screenupdating and visible to be false. It did help save some time but still not very ideal. (still takes 3-4 seconds per page). Not sure if it's my scripts' problem or it is normal when using API. I will try html anyway. Thank you.

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: Looking for a quick way to launch and write contents into MS word
« Reply #11 on: July 09, 2019, 05:48:58 AM »
Not sure if it's my scripts' problem or it is normal when using API.
google some info on Word API performance
something like this -> https://wordmvp.com/FAQs/TblsFldsFms/FastTables.htm

dgpuertas

  • Newt
  • Posts: 80
Re: Looking for a quick way to launch and write contents into MS word
« Reply #12 on: July 09, 2019, 05:58:21 PM »
I use Word and it's faster than you say.
1-2 pages per second
I also use tables and combine cells and many different (sophisticated) methods and its more slowly.
Use range object and write and collapse.. write and collapse... and so on.
I use styles to format the document and the most frecuenty function is:

Code: [Select]
(defun word_escribir_texto_con_estilo (lis)
  (foreach a lis
     (mswm-Collapse *range* mswc-wdCollapseEnd)
     (mswp-put-style *range* (car a))
     (mswm-InsertAfter *range* (cadr a))
  )
)

from a list of list
Code: [Select]
(setq lis (list
  (list "Normal" "Normal text")
  (list "Tittle 1" "In a tittle")
))
(word_escribir_texto_con_estilo lis)



To create a new document from a template (dot) use:
Code: [Select]
(defun carga_word (dot / nombre)

   
  (setq *msw* (vlax-get-or-create-object (vl-registry-read "HKEY_CLASSES_ROOT\\Word.Application\\CurVer")))
 

  (if (null mswc-wd24hourclock)
  (vlax-import-type-library
    :tlb-filename                   (findfile (strcat (vla-get-path *msw*) "\\MSWORD.OLB")) ;posible error
    :methods-prefix "mswm-"
    :properties-prefix "mswp-"
    :constants-prefix "mswc-")
 )
 
 (vla-put-visible *msw* :vlax-true)  ;Cambiar por True

 (setq  *docs* (vla-get-documents *msw*)
        *doc* (mswm-add *docs* dot ) ;Plantilla proyecto
        *paragraphs* (mswp-get-paragraphs *doc*)
*pg* (mswp-get-last *paragraphs*)
*range* (mswp-get-range *pg*)
)

 
)

and
*range* is a global variable to write in the document.



It's one of the best things I've done to automate engineering reports.
« Last Edit: July 09, 2019, 06:01:58 PM by dgpuertas »

TheRubens

  • Guest
Re: Looking for a quick way to launch and write contents into MS word
« Reply #13 on: July 10, 2019, 03:50:34 AM »
Not sure if it's my scripts' problem or it is normal when using API.
google some info on Word API performance
something like this -> https://wordmvp.com/FAQs/TblsFldsFms/FastTables.htm

Thank you. very helpful.

TheRubens

  • Guest
Re: Looking for a quick way to launch and write contents into MS word
« Reply #14 on: July 10, 2019, 03:53:33 AM »
I use Word and it's faster than you say.
1-2 pages per second
I also use tables and combine cells and many different (sophisticated) methods and its more slowly.
Use range object and write and collapse.. write and collapse... and so on.
I use styles to format the document and the most frecuenty function is:

Code: [Select]
(defun word_escribir_texto_con_estilo (lis)
  (foreach a lis
     (mswm-Collapse *range* mswc-wdCollapseEnd)
     (mswp-put-style *range* (car a))
     (mswm-InsertAfter *range* (cadr a))
  )
)

from a list of list
Code: [Select]
(setq lis (list
  (list "Normal" "Normal text")
  (list "Tittle 1" "In a tittle")
))
(word_escribir_texto_con_estilo lis)



To create a new document from a template (dot) use:
Code: [Select]
(defun carga_word (dot / nombre)

   
  (setq *msw* (vlax-get-or-create-object (vl-registry-read "HKEY_CLASSES_ROOT\\Word.Application\\CurVer")))
 

  (if (null mswc-wd24hourclock)
  (vlax-import-type-library
    :tlb-filename                   (findfile (strcat (vla-get-path *msw*) "\\MSWORD.OLB")) ;posible error
    :methods-prefix "mswm-"
    :properties-prefix "mswp-"
    :constants-prefix "mswc-")
 )
 
 (vla-put-visible *msw* :vlax-true)  ;Cambiar por True

 (setq  *docs* (vla-get-documents *msw*)
        *doc* (mswm-add *docs* dot ) ;Plantilla proyecto
        *paragraphs* (mswp-get-paragraphs *doc*)
*pg* (mswp-get-last *paragraphs*)
*range* (mswp-get-range *pg*)
)

 
)

and
*range* is a global variable to write in the document.



It's one of the best things I've done to automate engineering reports.


Thanks for your reply and the codes dgpuertas .
It runs faster after I turned off the visible and screenupdating. My script looks similar to yours but I did some formatting for the Table objects. I guess that's why makes it slow.

ronjonp

  • Needs a day job
  • Posts: 7526
Re: Looking for a quick way to launch and write contents into MS word
« Reply #15 on: July 10, 2019, 12:25:20 PM »
Are these table objects you're formatting in CAD ? If so look into vla-put-RegenerateTableSuppressed and set it to true before making changes.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

TheRubens

  • Guest
Re: Looking for a quick way to launch and write contents into MS word
« Reply #16 on: July 12, 2019, 01:21:18 AM »
Are these table objects you're formatting in CAD ? If so look into vla-put-RegenerateTableSuppressed and set it to true before making changes.

Hi ronjonp, Thanks for your reply. It is not the table objects in CAD but the objects in MS word.