Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Open xlsm and run macro

Author  Topic 

Bill_C
Constraint Violating Yak Guru

299 Posts

Posted - 2011-02-08 : 08:10:12
Can anyone tell me how to open an xlsm file (excel macro enabled) I have one that does some cleansing on csv's I need to automate from sql server.

Bill_C
Constraint Violating Yak Guru

299 Posts

Posted - 2011-02-08 : 08:15:08
Forgot to put the code I have so far:-

Outlook is installed on the server (server is secure, only we access it)
This code seems to open the file, but does not run the auto macro I have in it.



declare @xlApp integer, @rs integer,@xlWorkbooks integer
,@xlWorkbook integer,@xlWorkSheet integer,@FileName varchar(100)
,@sql varchar(4000),@filepath varchar(250), @sd varchar(30), @ed varchar(30), @cd varchar(30), @cmd varchar(30)
,@itable varchar(150), @mtable varchar(150),@srchstring varchar(200),@esrdp varchar(30),@esrdp2 varchar(6)

execute @rs = dbo.sp_OACreate 'Excel.Application', @xlApp OUTPUT
--turn off screen updating and displayalerts
--execute @rs = master.dbo.sp_OASetProperty @xlApp, 'ScreenUpdating', 'False'
--execute @rs = master.dbo.sp_OASetProperty @xlApp, 'DisplayAlerts', 'False'
--open workbook
--declare @xlWorkbooks integer
execute @rs = master.dbo.sp_OAMethod @xlApp, 'Workbooks', @xlWorkbooks OUTPUT
--declare @xlWorkbook integer
execute @rs = master.dbo.sp_OAMethod @xlWorkbooks, 'Open', @xlWorkbook OUTPUT, 'N:\ExcelMacro1.xlsm'
--open worksheet
--declare @xlWorkSheet integer
execute @rs = master.dbo.sp_OAMethod @xlWorkbook, 'ActiveSheet', @xlWorkSheet OUTPUT
--clean up
execute @rs = master.dbo.sp_OAMethod @xlWorkbook, 'Close'
execute @rs = master.dbo.sp_OAMethod @xlApp, 'Quit'
--finally destroy all pointers
execute @rs = master.dbo.sp_OADestroy @xlWorkSheet
execute @rs = master.dbo.sp_OADestroy @xlWorkbook
execute @rs = master.dbo.sp_OADestroy @xlWorkbooks
execute @rs = master.dbo.sp_OADestroy @xlApp




How do i get the macro inside the workbook to run?

Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-02-08 : 09:25:01
Do you have the macro set to run inside the xlsm files OnOpen event?]

Are you sure macros are enabled when the file opens?




Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page

Bill_C
Constraint Violating Yak Guru

299 Posts

Posted - 2011-02-09 : 00:52:10
quote:
Originally posted by dataguru1971

Do you have the macro set to run inside the xlsm files OnOpen event?]

Are you sure macros are enabled when the file opens?




Poor planning on your part does not constitute an emergency on my part.




Yes, macros are enabled,I can open the workbook manually (double click it whilst logged directly on to the server through Remote Desktop Connection) and it runs the 'On Open' macro that is inside it, plus all the others I have set up.

But when I try to fire it up via the code above from within sql server, I get an instance of Excel showing as running in Task Manager, and also I can see in the folder that holds the workbook, the ~tmp file for the workbook is open (which shows the file is open?), but the macros do not run.

Any ideas?
Is this a restriction from within sql server itself?
Go to Top of Page

dataguru1971
Master Smack Fu Yak Hacker

1464 Posts

Posted - 2011-02-09 : 03:09:26
No, it is an excel issue..probably security inside of excel. Check this article :http://office.microsoft.com/en-us/excel-help/change-macro-security-settings-in-excel-HP010096919.aspx





Poor planning on your part does not constitute an emergency on my part.
Go to Top of Page

Bill_C
Constraint Violating Yak Guru

299 Posts

Posted - 2011-02-09 : 05:38:00
quote:
Originally posted by dataguru1971

No, it is an excel issue..probably security inside of excel. Check this article :http://office.microsoft.com/en-us/excel-help/change-macro-security-settings-in-excel-HP010096919.aspx





Poor planning on your part does not constitute an emergency on my part.




Thanks - Checked out that and it doesn't give me anything I haven't done yet.

I Can't undestand why it's not working, I've complied with everything in that document and when i open the xlsm file by double clicking it, it runs the macros OK,
yet when I open it programatically through sql server, it doesn't run the macros!

This is really confusing.
Go to Top of Page
   

- Advertisement -