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
   Dim aPosition As New

   Doc = ThisComponent
   Sheet = Doc.Sheets.getByIndex(0)
   sGraphicService = ""
   oDrawPage = Sheet.getDrawPage()
   szPicFileName = Application.GetOpenFilename()
   sGraphicService = ""
   oDrawPage = Sheet.getDrawPage()
   oGraphic = Doc.createInstance(sGraphicService)
   oGraphic.GraphicURL = URL
   REM Size the object
   REM Position the object
   aPosition.X = posX
   aPosition.Y = posY
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 on XP & Ubuntu
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 (x64) on Windows 7
If you think that I did not answer your question, make allowances for my imperfect English
User avatar
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