PublishObjects Calculating All Open Workbooks

furt0414

New Member
Joined
Jul 17, 2013
Messages
20
Hi All,

I am in desperate need of assistance. I have a macro that sends out ~200 reports on a daily basis, but it runs very slow because it calculates every time it hits the .Publish (True) line in Ron de Bruin's RangetoHTML function. Is there something I can add so that it will only calculate the active sheet?

Here's the code for reference:
Code:
Function RangetoHTML(rng As Range)' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2013
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook


    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    Application.Calculation = xlCalculationManual
    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With


    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With


    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")


    'Close TempWB
    TempWB.Close savechanges:=False


    'Delete the htm file we used in this function
    Kill TempFile


    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function

Any help is appreciated!!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Yes it can be changed. Do you still need help?


Syoodo,

Thanks for the response, I am still in need of help. For some reason it is no longer running as slow as it was when I originally posted, however anything I can do to speed up the code is a good thing! What tips do you have?
 
Upvote 0
I believe I have some code that will help you out. To prevent any loss of data due to unforseen errors, test this code in a copy of your workbook. Replace the 'Publish the sheet to a htm file block of code with this code

Code:
'disable calculations on all sheets
For CounterVAR = 1 To ActiveWorkbook.Sheets.Count
    Sheets(CounterVAR).EnableCalculation = False
Next

'enable calculations on current sheet then calculate
ActiveSheet.EnableCalculation = True
ActiveSheet.Calculate 'you may not need this line

'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

'enable calculations on all sheets
For CounterVAR = 1 To ActiveWorkbook.Sheets.Count
    Sheets(CounterVAR).EnableCalculation = True
Next

I would also declare the counter variable by adding
Code:
Dim CounterVAR As Integer
to the other declared variables.

I believe this will work but I have not been able to fully test the code with your macro. I look forward to hearing about the results of your testing.
 
Upvote 0

Forum statistics

Threads
1,216,004
Messages
6,128,218
Members
449,435
Latest member
Jahmia0616

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top