Page 1 of 1

How do I insert an Image into Word with ComatePlus

Posted: Tue Feb 02, 2021 6:40 am
by leodh
Hi,

I am trying to insert an Image into a Word document that I have created with ComatePlus.

I can get the image in via Copy and Paste but I can not locate the Image in the right spot.

I have also tried insert the image from a file , that also works but I can only put it at the top of the Doc.

Here is the simplified version of the code. You can use any of your own image files to test.

Code: Select all


UsePNGImageDecoder()
UsePNGImageEncoder()

IncludePath "C:\programs\" 
XIncludeFile "COMatePlus.pbi"

Global DirApp.s = "C:\DDLS Apps\_DEV\HISTO SRS\"

result = LoadImage(53, DirApp.s + "Images\2063 - Image.png") 

SetClipboardImage(53)

File$ = DirApp.s + "Images\2063 - Image.png"

Define.COMateObject oWord, oDoc, oRange, oPara0, oPara1, oPara2, oPara3
Define.COMateObject oPara4, oPara5, oPara6, oPara7

oWord = COMate_CreateObject("Word.Application") 
If oWord 
  If oWord\SetProperty("Visible = #true") = #S_OK 
    oDoc = oWord\GetObjectProperty("Documents\Add") 
    If oDoc         
      oRange = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
      If oRange
        oPara0 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
        If oPara0
          oPara0\SetProperty("Range\Font\Bold = #True")
          oPara0\SetProperty("Range\Font\Name = 'Arial'")
          oPara0\SetProperty("Range\Font\Size = 15")  
          oPara0\SetProperty("Range\Text = '"+
                             "Subject:              Special Stain/Re-cut Request  " + "'")
          oPara0\SetProperty("Format\SpaceAfter = 6")
          oPara0\SetProperty("Format\SpaceBefore = 0") 
          oPara0\Invoke("Range\InsertParagraphAfter()")
        EndIf
        oRange\Release()
      EndIf 
      
      String.s = #LFCR$ + "Hello Histology Lab" + #LFCR$ +
                 "Could you please arrange the following request." + #CR$ +
                 "Special Stain/Re-cut Request :" + #LFCR$ + "======================" 
      
      oRange = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
      If oRange
        oPara1 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
        If oPara1
          oPara1\SetProperty("Range\Font\Bold = #False")
          oPara1\SetProperty("Range\Font\Name = 'Arial'")
          oPara1\SetProperty("Range\Font\Size = 14")  
          oPara1\SetProperty("Range\Text = '" + String.s + "'")
          oPara1\SetProperty("Format\SpaceAfter = 0")
          oPara1\SetProperty("Format\SpaceBefore = 0") 
          oPara1\Invoke("Range\InsertParagraphAfter()")
        EndIf
        oRange\Release()
      EndIf 
      
      String.s = "*** URGENT REQUEST *** " + #CR$ + "======================" + #CR$
      oRange = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
        If oRange
          oPara2 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
          If oPara2
            oPara2\SetProperty("Range\Font\Bold = #False")
            oPara2\SetProperty("Range\Font\Name = 'Arial'")
            oPara2\SetProperty("Range\Font\Size = 14")  
            oPara2\SetProperty("Range\Text = '" + String.s + "'")
            oPara2\SetProperty("Format\SpaceAfter = 0")
            oPara2\SetProperty("Format\SpaceBefore = 0")             
          EndIf
          oRange\Release()
        EndIf
        
      String.s =  #CR$ + "-------------------------------------------------------------"   
      oRange = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
        If oRange
          oPara3 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
          If oPara3
            oPara3\SetProperty("Range\Font\Bold = #Fasle")
            oPara3\SetProperty("Range\Font\Name = 'Arial'")
            oPara3\SetProperty("Range\Font\Size = 14")  
            oPara3\SetProperty("Range\Text = '" + String.s + "'")
            oPara3\SetProperty("Format\SpaceAfter = 0")
            oPara3\SetProperty("Format\SpaceBefore = 0") 
            oPara3\Invoke("Range\InsertParagraphAfter()")
          EndIf
          oRange\Release()
        EndIf
        
      String.s = ""
      String.s = "Special Stain Request." + #CR$
      String.s = String.s + "Re Cut Request."  + #CR$
      String.s = String.s + "Re Cut and Stain Request." + #CR$
      String.s = String.s + "Re Scan Request." + #CR$
      String.s = String.s + "-------------------------------------------------------------" + #CR$
      oRange = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
        If oRange
          oPara4 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
          If oPara4
            oPara4\SetProperty("Range\Font\Bold = #False")
            oPara4\SetProperty("Range\Font\Name = 'Arial'")
            oPara4\SetProperty("Range\Font\Size = 14")  
            oPara4\SetProperty("Range\Text = '" + String.s + "'")
            oPara4\SetProperty("Format\SpaceAfter = 0")
            oPara4\SetProperty("Format\SpaceBefore = 0") 
            oPara4\Invoke("Range\InsertParagraphAfter()")
          EndIf
          oRange\Release()
        EndIf
        
       oRange = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
        If oRange
          oPara5 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
          If oPara5 
           oPara5\Invoke("Application\Selection\Endkey=6") ; } ( PASTE ) works if no more info and Endkey=6
           oPara5\Invoke("Application\Selection\Paste")       ; }
            
           ;oPara5\Invoke("Application\Selection\InlineShapes\AddPicture('" + file$ + "')")  ; } puts image at top of page           
            
          EndIf
          oRange\Release()
        EndIf
        
     String.s = #LFCR$ + "========================================" + #LFCR$  
                       
     oRange = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
       If oRange
         oPara6 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
         If oPara6
           ;oPara6\SetProperty("Range\Font\Size = 14")                 } comment out and it works
          ; oPara6\SetProperty("Range\Text = '" + String.s + "'")      }
         EndIf
         oRange\Release()
       EndIf    

      oDoc\Release()
    EndIf
    oWord\Release()
  EndIf    
EndIf 

If I comment out the oPara6\SetProperty lines it works but the picture can not have anymore text after it.

Does anybody know the correct syntax to use to put the Image anywhere I want.

Cheers
Leo

Re: How do I insert an Image into Word with ComatePlus

Posted: Tue Feb 02, 2021 6:22 pm
by Justin
Hi leodh,
before insering the image you have to set the position using, Selection.GoTo() method,
https://docs.microsoft.com/en-us/office ... ction.GoTo

This inserts the image at line 2, you will have to change the image file path.

Code: Select all

EnableExplicit

UsePNGImageDecoder()
UsePNGImageEncoder()
UseJPEG2000ImageDecoder()
UseJPEGImageDecoder()

;- Enum WdGoToItem
#wdGoToBookmark = -1
#wdGoToSection = 0
#wdGoToPage = 1
#wdGoToTable = 2
#wdGoToLine = 3
#wdGoToFootnote = 4
#wdGoToEndnote = 5
#wdGoToComment = 6
#wdGoToField = 7
#wdGoToGraphic = 8
#wdGoToObject = 9
#wdGoToEquation = 10
#wdGoToHeading = 11
#wdGoToPercent = 12
#wdGoToSpellingError = 13
#wdGoToGrammaticalError = 14
#wdGoToProofreadingError = 15

;- Enum WdGoToDirection
#wdGoToFirst = 1
#wdGoToLast = -1
#wdGoToNext = 2
#wdGoToRelative = 2
#wdGoToPrevious = 3
#wdGoToAbsolute = 1


; IncludePath "C:\programs\"
XIncludeFile "COMatePLUS\COMatePlus.pbi"

Define.COMateObject oWord, oDoc, oRange, oPara0, oPara1, oPara2, oPara3
Define.COMateObject oPara4, oPara5, oPara6, oPara7, oSelection
Define.i result, line
Define.s file$, string

Global DirApp.s = "C:\DDLS Apps\_DEV\HISTO SRS\"

result = LoadImage(53, "image.JPG")

SetClipboardImage(53)

File$ = "F:\purebasic\image.JPG"


oWord = COMate_CreateObject("Word.Application")
If oWord
  If oWord\SetProperty("Visible = #true") = #S_OK
    oDoc = oWord\GetObjectProperty("Documents\Add")
    If oDoc         
      oRange = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
      If oRange
        oPara0 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
        If oPara0
          oPara0\SetProperty("Range\Font\Bold = #True")
          oPara0\SetProperty("Range\Font\Name = 'Arial'")
          oPara0\SetProperty("Range\Font\Size = 15") 
          oPara0\SetProperty("Range\Text = '"+
                             "Subject:              Special Stain/Re-cut Request  " + "'")
          oPara0\SetProperty("Format\SpaceAfter = 6")
          oPara0\SetProperty("Format\SpaceBefore = 0")
          oPara0\Invoke("Range\InsertParagraphAfter()")
        EndIf
        oRange\Release()
      EndIf
     
      String.s = #LFCR$ + "Hello Histology Lab" + #LFCR$ +
                 "Could you please arrange the following request." + #CR$ +
                 "Special Stain/Re-cut Request :" + #LFCR$ + "======================"
     
      oRange = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
      If oRange
        oPara1 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
        If oPara1
          oPara1\SetProperty("Range\Font\Bold = #False")
          oPara1\SetProperty("Range\Font\Name = 'Arial'")
          oPara1\SetProperty("Range\Font\Size = 14") 
          oPara1\SetProperty("Range\Text = '" + String.s + "'")
          oPara1\SetProperty("Format\SpaceAfter = 0")
          oPara1\SetProperty("Format\SpaceBefore = 0")
          oPara1\Invoke("Range\InsertParagraphAfter()")
        EndIf
        oRange\Release()
      EndIf
     
      String.s = "*** URGENT REQUEST *** " + #CR$ + "======================" + #CR$
      oRange = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
        If oRange
          oPara2 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
          If oPara2
            oPara2\SetProperty("Range\Font\Bold = #False")
            oPara2\SetProperty("Range\Font\Name = 'Arial'")
            oPara2\SetProperty("Range\Font\Size = 14") 
            oPara2\SetProperty("Range\Text = '" + String.s + "'")
            oPara2\SetProperty("Format\SpaceAfter = 0")
            oPara2\SetProperty("Format\SpaceBefore = 0")             
          EndIf
          oRange\Release()
        EndIf
       
      String.s =  #CR$ + "-------------------------------------------------------------"   
      oRange = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
        If oRange
          oPara3 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
          If oPara3
            oPara3\SetProperty("Range\Font\Bold = #Fasle")
            oPara3\SetProperty("Range\Font\Name = 'Arial'")
            oPara3\SetProperty("Range\Font\Size = 14") 
            oPara3\SetProperty("Range\Text = '" + String.s + "'")
            oPara3\SetProperty("Format\SpaceAfter = 0")
            oPara3\SetProperty("Format\SpaceBefore = 0")
            oPara3\Invoke("Range\InsertParagraphAfter()")
          EndIf
          oRange\Release()
        EndIf
       
      String.s = ""
      String.s = "Special Stain Request." + #CR$
      String.s = String.s + "Re Cut Request."  + #CR$
      String.s = String.s + "Re Cut and Stain Request." + #CR$
      String.s = String.s + "Re Scan Request." + #CR$
      String.s = String.s + "-------------------------------------------------------------" + #CR$
      oRange = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
        If oRange
          oPara4 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
          If oPara4
            oPara4\SetProperty("Range\Font\Bold = #False")
            oPara4\SetProperty("Range\Font\Name = 'Arial'")
            oPara4\SetProperty("Range\Font\Size = 14") 
            oPara4\SetProperty("Range\Text = '" + String.s + "'")
            oPara4\SetProperty("Format\SpaceAfter = 0")
            oPara4\SetProperty("Format\SpaceBefore = 0")
            oPara4\Invoke("Range\InsertParagraphAfter()")
          EndIf
          oRange\Release()
        EndIf
       
       oRange = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
        If oRange
          oPara5 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
          If oPara5
					
					;Insert at line 2
					line = 2
          oPara5\Invoke("Application\Selection\Goto(" + Str(#wdGoToLine) + ", " + Str(#wdGoToAbsolute) + ", " + Str(line) + ", #Empty)")    ; }

; 				Debug COMate_GetLastErrorDescription() 

           oPara5\Invoke("Application\Selection\InlineShapes\AddPicture('" + file$ + "')")  ; } puts image at top of page           
           
          EndIf
          oRange\Release()
        EndIf
       
     String.s = #LFCR$ + "========================================" + #LFCR$ 
                       
     oRange = oDoc\GetObjectProperty("Bookmarks('\endofdoc')\Range")
       If oRange
         oPara6 = oDoc\GetObjectProperty("Content\Paragraphs\Add")
         If oPara6
           ;oPara6\SetProperty("Range\Font\Size = 14")                 } comment out and it works
          ; oPara6\SetProperty("Range\Text = '" + String.s + "'")      }
         EndIf
         oRange\Release()
       EndIf   

      oDoc\Release()
    EndIf
    oWord\Release()
  EndIf   
EndIf

Re: How do I insert an Image into Word with ComatePlus

Posted: Wed Feb 03, 2021 5:29 am
by leodh
@ Justin,

Thanks, I have it working now.

Your help was much appreciated.

Cheers
Leo