ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Running a macro outside of Excel (https://www.excelbanter.com/excel-programming/333607-running-macro-outside-excel.html)

Zakynthos

Running a macro outside of Excel
 
I want to automatically copy paste individual cells one at a time into
another program, as I cannot block paste columns from one program to the
other.

Is there any way I can configure a macro to operate outside Excel, copy a
cell, paste into a second program's spreadsheet and repeat this operation for
a column of figures?



Rich_z[_19_]

Running a macro outside of Excel
 

Yes you can, although the language that you are using needs to be OLE
enabled.

As an example here is a procedure that manipulates an Excel spreadsheet
without the user even seeing Excel at all:


Code:
--------------------

Form Create_Spreadsheet.
*
Constants: c_start_row type XlRow value 7.
*
Data: Excel Type Ole2_Object, " Application
W_Book Type Ole2_Object, " Work book
w_Sheet Type Ole2_Object, " Work Sheet
*
w_ba_count type i, " Max # Business area
w_row type xlRow, " Current ss row.
w_start type XlRangeAddress, " Global format
w_end type XlRangeAddress,
w_range type XlRangeAddress,
w_last type i,
w_last_col type XlColumn.
*
* The number of business areas determines the number of cols in
* the work sheet.
*
Describe table t_tgsb lines w_ba_count.
*
Create Object Excel 'EXCEL.APPLICATION'.
Call Method Of Excel 'WORKBOOKS' = W_Book.
Call Method of w_Book 'Add'.
If sy-subrc = 0.
*
* There may be more than one sheet here - get rid of any other
* sheets
*
Call Method of Excel 'Worksheets' = w_sheet Exporting #1 = 2.
While sy-subrc = 0.
Set Property Of Excel 'DisplayAlerts' = XlFalse.
Call Method of w_Sheet 'Delete'.
Set Property Of Excel 'DisplayAlerts' = XlTrue.
Call Method of Excel 'Worksheets' = w_sheet
Exporting #1 = 2.
EndWhile.
Call Method Of Excel 'Sheets' = w_Sheet
Exporting #1 = 'Sheet1'.
Call Method Of W_sheet 'Activate'.
Set Property of W_Sheet 'Name' = 'Draft Accounts'.
Perform Set_Column_Widths using w_sheet w_ba_count.
Perform SpreadSheet_Headers using w_sheet w_ba_count.
*
* Do some global formatting
*
Describe table t_spreadsheet lines w_row.
Create_Address c_start_row 'B' w_start.
Add 6 to w_row. " For additional totals
Compute_Last_Column w_last w_last w_ba_count w_last_col.
Create_Address w_row w_last_col w_end.
Concatenate w_start w_end into w_range separated by ':'.
Perform Set_Font using w_sheet w_range 'Arial' 7 'Bold'.

--------------------


And so lower level code from one of the procedures:


Code:
--------------------

Form Set_Single_Border Using pu_sheet Type Ole2_Object
pu_range Type XlRangeAddress
pu_LineStyle Type XlLineStyle
pu_Weight Type XlLineWeight
pu_Colour Type XlLineColour
pu_border Type XlBorder.
*
Data: Range type Ole2_Object,
Borders type Ole2_Object.
*
Call Method of pu_Sheet 'Range' = Range Exporting #1 = pu_range.
Call Method of Range 'Borders' = Borders
Exporting #1 = pu_border.
Set Property of Borders: 'Linestyle' = pu_Linestyle,
'Weight' = pu_Weight,
'ColorIndex' = pu_Colour.
*
Free object Borders.
Free object Range.
EndForm.

--------------------



What language are you trying to do this in ? I can perhaps give you
some pointers.

Regards

Rich


--
Rich_z
------------------------------------------------------------------------
Rich_z's Profile: http://www.excelforum.com/member.php...o&userid=24737
View this thread: http://www.excelforum.com/showthread...hreadid=384471



All times are GMT +1. The time now is 03:38 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com