publish object error 1004

aspiringnerd

New Member
Joined
Apr 22, 2022
Messages
39
Office Version
  1. 365
Platform
  1. Windows
I'm getting an error when I change the sheet reference from this:
VBA Code:
Sub ExportHtml()
    Dim ws As Worksheet, lr As Long, rng As Range
    Set ws = Sheets("Sheets1")
    lr = ws.Range("a" & ws.Rows.Count).End(xlUp).Row
    Set rng = ws.Range("a1:h" & lr)
    
    file1 = "C:\Desktop\" + Cells(ActiveCell.Row, "E").Value + ".html"

    ActiveWorkbook.PublishObjects.Add( _
    SourceType:=xlSourceRange, _
    Filename:=file1, _
    Sheet:=rng.Worksheet.Name, _
    Source:=rng.Address, _
    HtmlType:=xlHtmlStatic).Publish

End Sub

to this:

VBA Code:
Sub ExportHtml()
    Dim ws As Worksheet, lr As Long, rng As Range
    Set ws = Sheets("Sheets2")
    lr = ws.Range("a" & ws.Rows.Count).End(xlUp).Row
    Set rng = ws.Range("a1:h" & lr)
    
    file1 = "C:\Desktop\" + Cells(ActiveCell.Row, "E").Value + ".html"

    ActiveWorkbook.PublishObjects.Add( _
    SourceType:=xlSourceRange, _
    Filename:=file1, _
    Sheet:=rng.Worksheet.Name, _
    Source:=rng.Address, _
    HtmlType:=xlHtmlStatic).Publish

End Sub

Sheet2 is hidden, I call this macro with a button on sheet1, it's only when I change sheet1 to sheet2 where I get error 1004, I'm not sure how to fix this one.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Have you tried it with sheet 2 visible ??
I don't have Excel at the moment so can't try your code.
 
Upvote 0
Where does your code reside? ThisWorkbook, Sheet1,Sheet2 or Module?
 
Upvote 0
Where does your code reside? ThisWorkbook, Sheet1,Sheet2 or Module?
I've tried it with sheet2 visible with no luck, the code lives in a module.

When I debug, it highlights:

VBA Code:
    ActiveWorkbook.PublishObjects.Add( _
    SourceType:=xlSourceRange, _
    Filename:=file1, _
    Sheet:=rng.Worksheet.Name, _
    Source:=rng.Address, _
    HtmlType:=xlHtmlStatic).Publish
 
Upvote 0
Try changing the reference. You can't have an ActiveCell on a Hidden Sheet.
VBA Code:
file1 = "C:\Desktop\" + Cells(ActiveCell.Row, "E").Value + ".html"
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,741
Members
449,050
Latest member
excelknuckles

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