Question : Using sp_OAMethod for refresh/update excel pivot sheet without open it

Hi,
I'm developing a stored procedure to update data from an excel sheet (which contains a pivot table) without opening the file itself.

I can not find the right method to execute the command Update/Refresh on the Excel sheet.

Do you have any suggestions ?

Thanks
Sergio
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
/* Create stored procedure */
CREATE PROCEDURE [dbo].[CS_SP_DMO_RefreshExcel] 
AS
BEGIN
 
  declare @hr int,
          @objExcel int,
          @objWorkBooks int,
          @objWorkSheet int,
          @WorksheetIndex int,
          @filename varchar(512),
          @strErrorMessage varchar(255),
          @FindFile int, /* 0=False, -1=True */
          @T int          
 
  set @filename = 'C:\Temp\Refresh.xls'
  set @WorksheetIndex = 1
 
  exec @hr = sp_OACreate 'Excel.Application', @objExcel out
 
  if (@hr = 0)
    select @strErrorMessage = 'Returning WorkBooks object '
 
  IF (@hr = 0)
    EXEC @hr = sp_OAMethod @objExcel, 
                           'WorkBooks.Open', 
                           @objWorkBooks OUT, 
                           @filename
 
  IF (@hr = 0)
  BEGIN
    Set @FindFile = -1
    
    EXEC @hr = sp_OAMethod @objWorkBooks, 
                           'Worksheets.Item', 
                           @objWorkSheet OUT, 
                           @WorksheetIndex
    IF (@hr = 0)
      PRINT ('Selection Worksheet successful')
    ELSE                                   
      PRINT ('Selection Worksheet ended with errors')
 
    IF (@hr = 0)
      EXEC @hr = sp_OAMethod @objWorkSheet, 'Activate'
 
    IF (@hr = 0)
      PRINT ('Worksheet successful activation')
    ELSE                                   
      PRINT ('Activation Worksheet ended with errors')
 
    -- Refresh WorkSheet
    IF (@hr = 0)
      --EXEC @hr = sp_OAMethod @objWorkSheet, 'Refresh' -- ??
      EXEC @hr = sp_OAMethod @objWorkSheet, 'Update'  -- ??
 
    IF (@hr = 0)
      PRINT ('Refresh Worksheet successful')
    ELSE
      PRINT ('Refresh Worksheet ended with errors: ' + str(@hr))
 
    EXEC @hr = sp_OAMethod @objExcel, 'Workbooks.Close'
 
    EXEC sp_OAMethod @objExcel, 'Close'
  END
 
  -- Free memory
  EXEC sp_OADestroy @objExcel
  EXEC sp_OADestroy @objWorkSheet
  EXEC sp_OADestroy @objWorkBooks
END
Open in New Window Select All

Answer : Using sp_OAMethod for refresh/update excel pivot sheet without open it

Have not had a huge amount of success with the OA methods - typically end up using a linked server of some description and end up posting the results directly rather than have Excel dynamically link to a data source.

You could write a macro in Excel to update itself. Which also leads us into the area of interest for your question...

To answer your specific question, open up excel, go to tools, macro, visual basic editor then when the popup window displays, go View then object browser (or F2) then scroll down to pivot objects and you will see the various objects. If you press the F1 button you can then get help on the call arguments.

Good luch with this, and would love to see your code if you get it happening properly via the OA methods... Oh, and be very careful about shared documents - you can hang pretty easily in SQL while it goes  ballistic for a short while.
Random Solutions  
 
programming4us programming4us