Announcement

Collapse
No announcement yet.

How to open and contol other programs?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to open and contol other programs?

    Hello one and all,

    I would like to use PB Win to open and control various Window applications.
    For example assme we are going to use PB to create text files using Microsoft Word.

    How does one use PB to go execute the following simple steps:

    1 - Open MS Word.
    2 - Open a new file.
    3 - Enter some text.
    4 - Save the new file.

    I chose a simple task knowing full well that the easiest way would be to write PB code to Open a file, Print text to the file and then Close the file.


    Cordially, Ned Mullen

  • #2
    There is some sample code for this in the Samples directory of PBWin I believe. I've done this once. I'll see if I can get time to post it. Jose Roca has a lot on this at his web site, I believe.

    Bob Mechler

    Comment


    • #3
      It takes several seconds for anything to happen after you click the Yes in the MsgBox(), so be patient...

      Code:
      #Compile Exe
      #Include "Win32Api.inc"
      #Include "oword.inc"
      
      Function PBMain() As Long
        Register i As Long
        Dim oWord As WordApplication
        Dim oWordDoc As WordDocument
        Dim oWordSel As WordSelection
        Dim oFont As WordFont
        Dim vBool As Variant, vVnt As Variant
        Dim vText As Variant
      
        
        If MsgBox("Do You Wish To Open Word?", %MB_YESNO, "We Need To Know!")=%IDYES Then
           Set oWord = New WordApplication In "Word.Application"
           Let vBool = 1
           Object Let oWord.Visible = vBool
           Object Call oWord.Documents.Add To vVnt
           Set oWordDoc = vVnt
           Object Get oWord.Selection To vVnt
           Set oWordSel = vVnt
           Let vText = "Here Is Some Text"
           Object Call oWordSel.TypeText(vText)
           Object Call oWordSel.TypeParagraph
           Object Get oWordSel.Font To vVnt
           Set oFont = vVnt
           Object Get oFont.Size To vVnt
           vText="oFont.Size = " & Str$(Variant#(vVnt))
           Object Call oWordSel.TypeText(vText)
           Object Call oWordSel.TypeParagraph
           Object Get oFont.Name To vVnt
           vText="oFont.Name="& Variant$(vVnt)
           Object Call oWordSel.TypeText(vText)
           Object Call oWordSel.TypeParagraph
           Object Call oWordSel.TypeParagraph
           For i=4 To 30
             Let vVnt=CSng(i)
             Object Let oFont.Size=vVnt
             Let vText = "Text With Font Size = " & Str$(i)
             Object Call oWordSel.TypeText(vText)
             Object Call oWordSel.TypeParagraph
           Next i
           Set oWordSel = Nothing
           Set oWordDoc = Nothing
           Set oWord = Nothing
        End If
      
        PBMain=0
      End Function
      Next question:

      Where do you get OWord.inc?

      Answer:

      Read your PB COM documentation.
      Fred
      "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

      Comment


      • #4
        Thanks for your help.

        To Fred and Bob,

        My apologies for the delay in replying to your answers. The doctors insisted on making some changes to my tummy. This was another of the fringe benefits one gets for making it beyond the age of 16.

        In any case, many thanks for your kind and useful help.

        Cordially, Ned Mullen

        Comment


        • #5
          Originally posted by Ned Mullen View Post
          Hello one and all,

          I would like to use PB Win to open and control various Window applications.
          For example assme we are going to use PB to create text files using Microsoft Word.

          How does one use PB to go execute the following simple steps:

          1 - Open MS Word.
          2 - Open a new file.
          3 - Enter some text.
          4 - Save the new file.

          I chose a simple task knowing full well that the easiest way would be to write PB code to Open a file, Print text to the file and then Close the file.


          Cordially, Ned Mullen
          :secret: The absolute simplest answer to your question is in the manual and the online help and the samples files in PBWIN.

          In the PB printed manual there is a sample program on page 53-4 that does exactly what you ask in Excel. I suggest you read pp. 43-55 on COM programming to understand what it is doing. The same article is available in the online help from the table of contents/programming reference/COM programming. One of the best things about PB is that it runs as advertised!

          The Excel sample demos the simplest way to do it. The Word sample is your direct answer.

          Stan


          this is the sample from the sample file in your PBWIN/samples directory:
          Code:
          '
          '  oWord.bas example for PowerBASIC for Windows
          '  Copyright (c) 2002-2005 PowerBASIC, Inc.
          '  All Rights Reserved.
          '
          '  Run a short demonstration of sending data to Microsoft Word.
          '
          '  * Tested with MS Word v9.00 (Office 2000) *
          '
          '==============================================================================
          
          
          #COMPILE EXE
          #DIM ALL
          #INCLUDE "oWord.inc"
          
          DECLARE SUB DisplayResult(msg AS STRING)
          
          
          '------------------------------------------------------------------------------
          ' Compiler-independent helper function to display feedback and errors
          '
          SUB DisplayResult(msg AS STRING)
          
              #IF %DEF(%PB_CC32)
                  PRINT msg
                  PRINT "Press a key...";
                  WAITKEY$
                  LOCATE ,1
              #ELSE
                  MSGBOX msg, &H00001000& ' %MB_SYSTEMMODAL
              #ENDIF
          
          END SUB
          
          
          '------------------------------------------------------------------------------
          ' Main application entry point...
          '
          FUNCTION PBMAIN () AS LONG
          
              DIM oWordApp     AS WordApplication ' Application Interface
              DIM oWordDoc     AS WordDocument    ' Document Interface
              DIM oWordSel     AS WordSelection   ' Selection
              DIM sProgID_Word AS STRING
          
              DIM vBool     AS VARIANT
              DIM vText     AS VARIANT
              DIM vFile     AS VARIANT
              DIM vFileFmt  AS VARIANT
              DIM vVnt      AS VARIANT
              DIM Result    AS STRING
          
              DisplayResult "About to Open MSWORD..."
              '----------------------------------------------------------------
              ' Note: For some components, the full version needs to be used,
              '       but else a version independent part of it is enough.
              sProgID_Word = PROGID$(CLSID$("Word.Application"))
              IF LEN(sProgID_Word) = 0 THEN sProgID_Word = "Word.Application"
              '----------------------------------------------------------------
          
              ' Open an instance of MSWORD
              SET oWordApp = WordApplication IN sProgID_Word
              IF ISFALSE ISOBJECT(oWordApp) THEN _
                  SET oWordApp = NEW WordApplication IN sProgID_Word
          
              ' Could MSWORD be opened? If not, terminate this app
              IF ISFALSE ISOBJECT(oWordApp) THEN
                  DisplayResult "Unable to open or start MSWORD!"
                  EXIT FUNCTION
              END IF
          
              ' Make MSWORD visible in for example normal show state
              LET vBool = %wdWindowStateNormal   ' or %wdWindowStateMaximize
              OBJECT LET oWordApp.WindowState = vBool
              LET vBool = 1
              OBJECT LET oWordApp.Visible = vBool
              '----------------------------------------------------------------
          
              ' Get the current document count from the "documents collection"
              DisplayResult "About to request the MSWORD document count..."
              LET vVnt = 0
              OBJECT GET oWordApp.Documents.Count TO vVnt
              DisplayResult "MSWORD has " & FORMAT$(VARIANT#(vVnt)) & " File(s) loaded!"
          
              ' Add a document to the "documents collection", and obtain a reference
              ' to the document Interface for the new document
              DisplayResult "Adding a new document to MSWORD..."
              LET vVnt = 0
              OBJECT CALL oWordApp.Documents.ADD TO vVnt
              IF OBJRESULT OR ERR THEN
                  DisplayResult "MSWORD was not able to start a new document." & $CR & _
                                "Please ensure MSWord and VBA are installed."
                  GOTO Terminate
              END IF
              SET oWordDoc = vVnt
          
              ' Get the current document count from the "documents collection"
              ' This should be one value higher than before
              DisplayResult "Checking the MSWORD document count again..."
              LET vVnt = 0
              OBJECT GET oWordApp.Documents.Count TO vVnt
              DisplayResult "MSWORD has " & FORMAT$(VARIANT#(vVnt)) & " File(s) loaded!"
          
              ' Get the Interface to the currect selection of the document
              DisplayResult "About to obtain an interface to the current selection point..."
              OBJECT GET oWordApp.Selection TO vVnt
              SET oWordSel = vVnt
          
              ' Enter some text into the document at the selection position
              DisplayResult "..and placing some text at that selection point..."
              LET vText = "PowerBASIC controls Microsoft Word!"
              OBJECT CALL oWordSel.TypeText(vText)
          
              ' Set a paragraph marker (end of paragraph)
              OBJECT CALL oWordSel.TypeParagraph
          
              ' Another paragraph
              LET vText = "COM client controls a COM server!"
              OBJECT CALL oWordSel.TypeText(vText)
              OBJECT CALL oWordSel.TypeParagraph
          
              ' Save the new document to disk
              DisplayResult "About to save the document and close MSWORD..."
              LET vFile    = "Test.doc"
              LET vFileFmt = %wdFormatDocument
              OBJECT CALL oWordDoc.SaveAs(vFile, vFileFmt)
          
              DisplayResult "All done! The COM automation stuff is pretty cool!"
              '----------------------------------------------------------------
          
          Terminate:
              ' Close the current document and then close MSWORD completely
              OBJECT CALL oWordApp.ActiveWindow.CLOSE
              OBJECT CALL oWordApp.Quit
          
              ' Close all of the Interfaces
              SET oWordSel  = NOTHING
              SET oWordDoc  = NOTHING
              SET oWordApp  = NOTHING
          
          END FUNCTION
          this is the sample for Excel from the manual.
          Code:
          'Requires Excel 97  or later to be installed.
           #COMPILE EXE
           #INCLUDE "ExcelApp.inc"
            
           FUNCTION PBMAIN
             ' Object Variables
             DIM oExcelApp       AS ExcelApplication
             DIM oExcelWorkbook  AS ExcelWorkbook
             DIM oExcelWorkSheet AS ExcelWorkSheet
             DIM oExcelChart     AS ExcelChart
            
             ' General Object param variables
             DIM vBool           AS VARIANT
             DIM vVnt            AS VARIANT
             DIM oVnt            AS VARIANT
             DIM vText           AS VARIANT
             DIM vRange          AS VARIANT
             DIM vX              AS VARIANT
             DIM vY              AS VARIANT
            
             ' Chart Object param variables
             DIM vGallery        AS VARIANT
             DIM vFormat         AS VARIANT
             DIM vPlotBy         AS VARIANT
             DIM vCatLabels      AS VARIANT
             DIM vSerLabels      AS VARIANT
             DIM vHasLegend      AS VARIANT
             DIM vTitle          AS VARIANT
            
             ' SaveAs Object param variables
             DIM vFile           AS VARIANT
             DIM vFileFmt        AS VARIANT
            
             ' Open an instance of EXCEL
             LET oExcelApp = ExcelApplication IN _
               $PROGID_ExcelApplication8
             IF ISFALSE ISOBJECT(oExcelApp) THEN _
               LET oExcelApp = NEW ExcelApplication IN  $PROGID_ExcelApplication8
            
             ' Could EXCEL be opened? If not, terminate this app
             IF ISFALSE ISOBJECT(oExcelApp) THEN EXIT FUNCTION
            
             ' Create a new workbook in EXCEL
             OBJECT CALL oExcelApp.WorkBooks.Add TO oVnt
            
             ' Copy the Interface reference into an Object Variable
             LET oExcelWorkbook = oVnt
            
             ' Create a new worksheet in the workbook and use this
             ' worksheet reference to pump data into EXCEL
             OBJECT CALL oExcelWorkBook.WorkSheets.Add TO oVnt
            
             ' Copy the Interface reference into an Object Variable
             LET oExcelWorkSheet = oVnt
            
             ' Format and send data for cells A1:L1
             LET vRange = "A1:G1"
            
             ' Create a Day of the Week array for A1:G1
             DIM a$(1 TO 7)
             a$(1) = "Monday"    : a$(2) = "Tuesday"
             a$(3) = "Wednesday" : a$(4) = "Thursday"
             a$(5) = "Friday"    : a$(6) = "Saturday"
             a$(7) = "Sunday"
            
             LET vText = a$()
             OBJECT LET oExcelWorkSheet.Range(vRange).Value = vText
            
            ' Center the column title text
             LET vVnt = %xlHAlignCenter
             OBJECT LET  oExcelWorkSheet.Range(vRange).HorizontalAlignment = vVnt
            
             ' Set the font attributes and border for this header  line
             OBJECT LET oExcelWorkSheet.Range(vRange).Font.Bold =  vBool
            
             ' Color the text Blue
             LET vVnt = RGB(0,0,255)
             OBJECT LET oExcelWorkSheet.Range(vRange).Font.Color =  vVnt
            
             ' Set the column width to approx 11 characters
             LET vVnt = 11
             OBJECT LET oExcelWorkSheet.Range(vRange).ColumnWidth =  vVnt
            
             ' Enclose the table with a border
             LET vRange = "A1:G2"
             LET vVnt = %xlMedium        ' Medium density border
             OBJECT CALL  oExcelWorkSheet.Range(vRange).BorderAround(Weight = vVnt)
            
             ' Format and send data for cells A2:G2
             LET vRange = "A2:G2"
            
             ' Create a sales value array for each day of the week
             DIM [email protected](1 TO 7)
             RANDOMIZE TIMER
             FOR y& = 1 TO 7
               [email protected](y&) = RND(1,100000)
             NEXT y&
             LET vVnt = [email protected]()
             OBJECT LET oExcelWorkSheet.Range(vRange).Value = vVnt
            
             ' Format these cells in money (currency) format, ie:  "$#.##"
             LET vText = "Currency"
             OBJECT LET oExcelWorkSheet.Range(vRange).Style = vText
            
            ' Now we can make EXCEL visible to the user
             LET vBool = 1
             OBJECT LET oExcelApp.Visible = vBool
            
             ' Prepare the chart
             OBJECT GET oExcelApp.Charts.Add TO oVnt
            
             ' Copy the Interface reference into an Object Variable
             LET oExcelChart = oVnt
            
             ' Set the range of cells to use in the table. Here it's  7x2
             LET vRange = "A1:G2"
             OBJECT GET oExcelWorkSheet.Range(vRange) TO vRange
            
             ' Set the Chart parameters
             LET vGallery   = %xl3DPie
             LET vFormat    = 7
             LET vPlotBy    = %xlRows
             LET vCatLabels = 1
             LET vSerLabels = 1
             LET vHasLegend = 2
             LET vTitle     = "Sales percentages"
            
             ' Launch the Chart using named parameters
             OBJECT CALL oExcelChart.ChartWizard( _
               Source          = vRange, _
               Gallery         = vGallery, _
               Format          = vFormat, _
               PlotBy          = vPlotBy, _
               CategoryLabels  = vCatLabels, _
               SeriesLabels    = vSerLabels, _
               HasLegend       = vHasLegend, _
               Title           = vTitle)
            
             #IF %DEF(%PB_CC32)
               PRINT "Press a key to continue the demo"
               WAITKEY$
             #ELSE
               MSGBOX "Click to continue the demo", &H00002000&  ' = %MB_TASKMODAL
             #ENDIF
            
             ' un-Rem the next line for a Print Preview of the  table
             '  Object call oExcelWorksheet.PrintPreview
            
             ' un-REM next line to stop "save workbook" prompting
             '  OBJECT LET oExcelWorkBook.Saved = vBool
            
             ' Save the Worksheet to disk - may trigger an  "Overwrite?"
             ' prompt if the file already exists.  We could either
             ' delete the file ahead of time or supply a unique  name
             LET vFile = "Test.Xls"
             OBJECT CALL oExcelWorkSheet.SaveAs(vFile)
            
             ' Close the current Worksheet and close EXCEL
             OBJECT CALL oExcelApp.ActiveWindow.Close
             OBJECT CALL oExcelApp.Quit
            
             ' Release the Interfaces.  We could omit this since  the
             ' app is about to close, but "best practice" states we
             ' should clean our house before moving out.
             LET oExcelApp       = NOTHING
             LET oExcelWorkbook  = NOTHING
             LET oExcelWorkSheet = NOTHING
             LET oExcelChart     = NOTHING
           END FUNCTION
            
           'See Also
           'The PowerBASIC COM  Browser
           'COM Programming Introduction
          Last edited by StanHelton; 13 May 2008, 09:06 AM. Reason: add code
          Do not go quiet into that good night,
          ... Rage, rage against the dark.

          Comment


          • #6
            One other approach

            If you have something you need to control and COM is not an option take a look at AutoIT. It is a freeware scripting system that can "read" screens, send keystrokes, move the mouse, click buttons, etc. There are lots of add on features like the ability to read/write SQLite DB's etc.

            I can't say it is the ideal solution but it does have merit under some circumstances.
            Mark Strickland, CISSP, CEH
            SimplyBASICsecurity.com

            Comment

            Working...
            X