General information
Category:
COM/DCOM and OLE Automation
>I have used the solution below to open an excel spreadsheet, disabling the macros.
>
>Please tell me there is a better way !!!!!!!
>
>
>
>*-- API decs
>DECLARE INTEGER FindWindow IN Win32API STRING, STRING
>DECLARE INTEGER SetForegroundWindow IN "User32.dll" INTEGER
>declare keybd_event in Win32API integer, integer, long, long
>
>*-- Declare objects
>LOCAL loExcel AS Excel.Application,loTimer
>
>*-- Instantiate our timer
>loTimer = NEWOBJECT("cTimer")
>
>*-- Instantiate our Excel instance
>loExcel = NEWOBJECT("Excel.Application")
>
>*-- Fire up the timer
>*-- It will keep looking for the Excel instance and once
>*-- found will disable the macros by "hitting enter"
>loTimer.Interval = 150
>
>*-- Open up the workbook
>loExcel.Workbooks.Open("d:\development\excel\test.xls")
>
>WAIT "Got past the opening and am ready for action" WINDOW
>
>RETURN
>
>
>DEFINE CLASS ctimer AS timer
> PROCEDURE Timer
>
> LOCAL lnHwnd
>
> *-- Get the windows handle of the excel sheet
> lnHWnd = FindWindow(0,"Microsoft Excel")
> IF lnHWnd <> 0
>
> *-- Make the excel sheet have the focus
> =SetForegroundWindow(lnHWnd)
> *-- Simluate hitting the enter key
> =keybd_event(13,0,0,0)
> *-- Simulate letting it up again !!!!
> =keybd_event(13,0,2,0)
> *-- Turn off this timer
> this.Interval = 0
> *-- Say bye bye
> this.Release()
> ENDIF
> ENDPROC
>
> PROCEDURE Release
> *-- Get rid of itself once its job is done
> RELEASE This
> ENDPROC
>
>ENDDEFINE
Something like:
objExcel.Application.CommandBars.ActiveMenuBar.Controls("Tools").Controls("Macro").Controls("Macros...").Enabled=.f.
However I am afraid that even you disable macros, it still can be used from another open document (if any).
One of the solution is to remove macro from the document,
and do not allow user to replace document when saving.
Something like:
x=objExcel.Application.VBE.ActiveVBProject.VBComponents
x.Remove(x.Item("Module2"))
Previous
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only