Page 1 of 1

Insert Pic to fit selected cell

Posted: Sat Oct 20, 2012 7:11 pm
by PedjaZ
This is the VBA code and I try something in Basic but I'm stuck.

Code: Select all

Private Sub CommandButton1_Click()
    Dim szPicFileName As String
    Dim rFirstRow As Long, rLastRow As Long
    Dim cFirstColumn As Integer, cLastColumn As Integer
     
     '   Get the filename & location of the picture
    szPicFileName = Application.GetOpenFilename()
     
    On Error Resume Next
    Set pic = ActiveSheet.Pictures.Insert(szPicFileName)
    On Error GoTo 0
    If Not pic Is Nothing Then 'Found it!'
         
        rFirstRow = ActiveCell.Row
        rLastRow = ActiveCell.Row + 13
        cFirstColumn = ActiveCell.Column
        cLastColumn = ActiveCell.Column + 4
        
        Set rng = Range(Cells(rFirstRow, cFirstColumn), Cells(rLastRow, cLastColumn))
        With pic
            .Height = rng.Height
            .Width = rng.Width
            .Left = rng.Left
            .Top = rng.Top
        End With
    End If
End Sub

This is what I try:

Code: Select all

Option VBASupport 1

Sub Insert 
   Dim szPicFileName As String 
   Dim URL As String, sGraphicService As String 
   Dim oDrawPages As Object, oDrawPage As Object 
   Dim TheSize As New com.sun.star.awt.Size
   Dim aPosition As New com.sun.star.awt.Point
 
   Doc = ThisComponent 
   Sheet = Doc.Sheets.getByIndex(0) 
   sGraphicService = "com.sun.star.drawing.GraphicObjectShape" 
   oDrawPage = Sheet.getDrawPage() 
   szPicFileName = Application.GetOpenFilename()
   
   url=ConvertToURL(szPicFileName)
   larg=9150 
   alt=6250 
   posX=280 
   posY=31860 
   sGraphicService = "com.sun.star.drawing.GraphicObjectShape" 
   oDrawPage = Sheet.getDrawPage() 
   oGraphic = Doc.createInstance(sGraphicService) 
   oGraphic.GraphicURL = URL 
   oDrawPage.add(oGraphic) 
   REM Size the object 
   TheSize.width=larg 
   TheSize.height=alt 
   oGraphic.setsize(TheSize) 
   REM Position the object 
   aPosition.X = posX 
   aPosition.Y = posY 
   oGraphic.setposition(aPosition) 
 end sub
First thing that I don't like is that images are linked, so on other computers pictures are not visible.
Second is that I don't know x, y of selected cell

Thank you for helping

Re: Insert Pic to fit selected cell

Posted: Sun Oct 21, 2012 6:57 pm
by JohnSUN-Pensioner
A few good examples I've seen in this topic