Insert Pic to fit selected cell

Creating a macro - Writing a Script - Using the API

Insert Pic to fit selected cell

Postby PedjaZ » Sat Oct 20, 2012 7:11 pm

This is the VBA code and I try something in Basic but I'm stuck.

Code: Select all   Expand viewCollapse view
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   Expand viewCollapse view
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
LibreOffice 3.5.6.2 on XP & Ubuntu
PedjaZ
 
Posts: 1
Joined: Sat Oct 20, 2012 6:35 pm

Re: Insert Pic to fit selected cell

Postby JohnSUN-Pensioner » Sun Oct 21, 2012 6:57 pm

A few good examples I've seen in this topic
I may not have a lot to give but what I got I'll give to you...
Apache OpenOffice 4.1.4, LibreOffice 5.4.2.2 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
User avatar
JohnSUN-Pensioner
Volunteer
 
Posts: 761
Joined: Fri Jan 14, 2011 1:21 pm
Location: Kyiv, Ukraine


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 5 guests