Microsoft Objects

Just starting out? Need help? Post your questions and find answers here.
bberenbach
New User
New User
Posts: 1
Joined: Sun Dec 08, 2024 9:05 pm

Microsoft Objects

Post by bberenbach »

Can PureBasic interface to Excel and be used with forms to input data to Excel?
plouf
Enthusiast
Enthusiast
Posts: 281
Joined: Fri Apr 25, 2003 6:35 pm
Location: Athens,Greece

Re: Microsoft Objects

Post by plouf »

no

or at least is very complicated

there are some plugins to import/export xls sheets thought, may be a workaround in what you are looking for
Christos
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Microsoft Objects

Post by jacdelad »

It can, search for COMate Plus. But that's not something for beginners.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
mk-soft
Always Here
Always Here
Posts: 6201
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Microsoft Objects

Post by mk-soft »

Or with VB-Script with Module ActiveScript
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Microsoft Objects

Post by Shardik »

You may take a look into Falko's collection of a lot of PureBasic procedures for Excel.

Download-Link for COMatePLUS

The following example demonstrates how to write the contents of a ListIconGadget into an Excel sheet:

Code: Select all

EnableExplicit

#xlDoNotSaveChanges = 2

IncludePath "Your/Path/To/COMatePLUS-Folder"
XIncludeFile "COMatePLUS.pbi"

Define ClassID.CLSID
Define InstallationState.I

Procedure.I WriteListIconContentsIntoExcelSheet(ListIconID.I)
  Protected Cells.COMateObject
  Protected Column.I
  Protected ColumnsTotal.I
  Protected Excel.COMateObject
  Protected Result.I
  Protected Row.I
  Protected RowsTotal.I
  Protected Statement.I
  Protected Value.I
  Protected WorkBook.COMateObject
  Protected WorkSheet.COMateObject

  Excel = COMate_CreateObject("Excel.Application")
  
  If Excel = 0
    MessageRequester("Error",
      "The initialisation of the Excel application failed!",
      #MB_ICONERROR)
  Else
    If Excel\SetProperty("Visible = #True") <> #S_OK
      MessageRequester("Error",
        "The activation of the Excel application failed!",
        #MB_ICONERROR)
    Else
      WorkBook = Excel\GetObjectProperty("Workbooks\Add")
      
      If WorkBook = 0
        MessageRequester("Error",
          "The creation of the Excel workbook failed!",
          #MB_ICONERROR)
      Else
        RowsTotal = CountGadgetItems(ListIconID) + 1
        ColumnsTotal = GetGadgetAttribute(0, #PB_ListIcon_ColumnCount)
        Statement = COMate_PrepareStatement("Cells(" + Str(@Row) +
          " As Long BYREF," + Str(@Column) + " As Long BYREF) = " +
          Str(@Value) + " As String BYREF")

        If Statement = 0
          MessageRequester("Error",
            "The creation of the PrepareStatement in COMatePLUS failed!" +
            #CRLF$ + #CRLF$ + "COMatePLUS error: " +
            COMate_GetLastErrorDescription(),
            #MB_ICONERROR)
        Else
          For Row = 1 To RowsTotal
            For Column = 1 To ColumnsTotal
              Value = COMate_MakeBSTR(GetGadgetItemText(0, Row - 2,
                Column - 1))
              Excel\SetProperty("", Statement)
              SysFreeString_(value)
            Next Column
          Next Row

          ; ----- Set optimal column width
          
          WorkSheet = WorkBook\GetObjectProperty("ActiveSheet")
          Cells = WorkSheet\GetObjectProperty("Cells")
          Cells\SetProperty("EntireColumn\AutoFit = #True")
          
          COMate_FreeStatementHandle(Statement)
        EndIf

        WorkBook\Release()
      EndIf
    EndIf

    Excel\Invoke("Quit(" + #xlDoNotSaveChanges + ")")
    Excel\Release()
  EndIf

  ProcedureReturn Result
EndProcedure

CoInitialize_(0)
InstallationState = CLSIDFromProgID_(@"Excel.Application", @ClassID)
CoUninitialize_()

If InstallationState <> #S_OK
  MessageRequester("Program terminated",
    "Microsoft Excel is not installed!",
    #MB_ICONERROR)
  End
EndIf

OpenWindow(0, 200, 100, 350, 89, "ListIconGadget")
ListIconGadget(0, 5, 5, 340, 79, "Name", 100, #PB_ListIcon_GridLines)
AddGadgetColumn(0, 1, "Address", 236)
AddGadgetItem(0, -1, "Harry Rannit" + #LF$ +
  "12 Parliament Way, Battle Street, By the Bay")
AddGadgetItem(0, -1, "Ginger Brokeit" + #LF$ +
  "130 PureBasic Road, BigTown, CodeCity")
AddGadgetItem(0, -1, "Didi Findit" + #LF$ +
  "321 Logo Drive, Mouse House, Downtown")

If WriteListIconContentsIntoExcelSheet(0) <> -1
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Post Reply