Author Topic: insert an image in MS Word file through Visual Lisp  (Read 1712 times)

0 Members and 1 Guest are viewing this topic.

Vandyck

  • Newt
  • Posts: 24
insert an image in MS Word file through Visual Lisp
« on: February 20, 2017, 07:27:44 AM »
I have to insert a picture in the LAST page of a MS Word file, but in my code there's something wrong: the image is inserted at the beginning of the FIRST page.

This is the code:
Code - Auto/Visual Lisp: [Select]
  1. ;; init
  2.  (setq app (vlax-get-or-create-object "Word.Application"))
  3.  (vla-put-visible app :vlax-true)
  4.  (vlax-put-property app 'ScreenUpdating :vlax-true)
  5.  (setq docs (vlax-get-property app 'Documents))
  6. ;; open test file
  7.  (vlax-invoke-method docs 'Open "c:\\test.doc" :vlax-false)
  8.  (setq doc (vlax-get-property app 'Activedocument))
  9.  (vlax-invoke-method doc 'Activate)
  10.  ;;
  11.  (setq selection (vlax-get-property app 'Selection ))
  12.  
  13.  ;; set cursor in last page [here's the error ???]
  14.  (vlax-invoke-method selection 'Endkey)
  15. ;; get image obiect
  16.  (setq img (vlax-get-property selection 'InlineShapes))
  17. ;; insert  image
  18.  (setq pic(vlax-invoke-method img 'AddPicture "c:\\test.gif"))
  19.  
  20. ; closing..
  21.  
  22.  (vlax-invoke-method doc 'Saveas  "c:\\test.doc")
  23.  
the image is insert in first page :-(

Some idea ?
Thanx

dgpuertas

  • Newt
  • Posts: 80
Re: insert an image in MS Word file through Visual Lisp
« Reply #1 on: February 20, 2017, 07:42:16 AM »
I think that before insert imagen must be collapse the range.
Invoke method Collapse

(mswm-Collapse *range* mswc-wdCollapseEnd)
or

(vlax-invoke-method range 'Collapse  mswc-wdCollapseEnd)


ChrisCarlson

  • Guest
Re: insert an image in MS Word file through Visual Lisp
« Reply #2 on: February 20, 2017, 07:43:38 AM »
Try it manually, hitting the end key does not do as your intending.

Vandyck

  • Newt
  • Posts: 24
Re: insert an image in MS Word file through Visual Lisp
« Reply #3 on: February 20, 2017, 09:02:52 AM »
I think that before insert imagen must be collapse the range.
Invoke method Collapse

(mswm-Collapse *range* mswc-wdCollapseEnd)
or

(vlax-invoke-method range 'Collapse  mswc-wdCollapseEnd)

I have modified my code with your suggestion but it does not work.
The collapse is inserted in the right position?
This is the new code

Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ()
  2.  (setq app (vlax-get-or-create-object "Word.Application"))
  3.  (vla-put-visible app :vlax-true)
  4.  (vlax-put-property app 'ScreenUpdating :vlax-true)
  5.  (setq docs (vlax-get-property app 'Documents))
  6. ;; open test file
  7.  (vlax-invoke-method docs 'Open "c:\\test.doc" :vlax-false)
  8.  (setq doc (vlax-get-property app 'Activedocument))
  9.  (vlax-invoke-method doc 'Activate)
  10.  (setq paragraphs (vlax-get-property doc 'paragraphs ))
  11.  (setq para(vlax-get-property paragraphs 'last ))
  12.  (setq range (vlax-get-property para 'range ))
  13.  ;;
  14.  (setq selection (vlax-get-property app 'Selection ))
  15.  
  16.  ;; set cursor in last page [here's the error ???]
  17.  (vlax-invoke-method selection 'Endkey)
  18.  (vlax-invoke-method range 'Collapse  0) ; wdCollapseEnd costant value = 0
  19. ;; get image obiect
  20.  (setq img (vlax-get-property selection 'InlineShapes))
  21. ;; insert  image
  22.  (setq pic(vlax-invoke-method img 'AddPicture "c:\\test.gif"))
  23.  
  24. ; closing..
  25.  
  26.  (vlax-invoke-method doc 'Saveas  "c:\\test.doc")
  27. )

Vandyck

  • Newt
  • Posts: 24
Re: insert an image in MS Word file through Visual Lisp
« Reply #4 on: February 20, 2017, 09:35:48 AM »
ok guys...
I solved the problem. I forgot to put a constant in:

(vlax-invoke-method selection 'Endkey )

the right version is:
(vlax-invoke-method selection 'Endkey 6)
 6 is the value of wdstory costant
Thanx to all