method 'publish' of object 'PublishObject' failed

clares

Well-known Member
Joined
Mar 14, 2002
Messages
557
Hi All

I come across code (from a link from the message board) for sending a range in Excel via outlook. Although it said just paste into a module, it gives me the above error. The code is here and its the following line that it bombs out of:

ActiveWorkbook.PublishObjects.Add(4, strTempFilePath, _
rngeSend.Parent.Name, rngeSend.Address, 0, "This is a test", "").Publish True

Code:
Private Sub CommandButton1_Click()
'Sends a specified range in an Outlook message and retains Excel formatting
'Written by Daniel Klann '2002

'Dimension variables
    Dim oOutlookApp As Object, oOutlookMessage As Object
    Dim oFSObj As Object, oFSTextStream As Object
    Dim rngeSend As Range, strHTMLBody As String, strTempFilePath As String
        
    'Select the range to be sent
    On Error Resume Next
    Set rngeSend = Application.InputBox("Please select range you wish to send.", , , , , , , 8)
    If rngeSend Is Nothing Then Exit Sub    'User pressed Cancel
    On Error GoTo 0

    'Get the temp folder path
    Set oFSObj = CreateObject("Scripting.FilesystemObject")
    strTempFilePath = oFSObj.GetSpecialFolder(2)
    'strTempFilePath = "O:\Daily Sales Figures"
    strTempFilePath = strTempFilePath & "\DailySalesFigures.htm"


    'Now create the HTML file - NOTE! xlSourceRange and xlHtmlStatic have been replaced by their
    'numeric values due to a potential error (unexplained) noted by Ivan F Moala 15/5/03
        
    ActiveWorkbook.PublishObjects.Add(4, strTempFilePath, _
                    rngeSend.Parent.Name, rngeSend.Address, 0, "This is a test", "").Publish True
    
    'Create an instance of Outlook (or use existing instance if it already exists
    Set oOutlookApp = CreateObject("Outlook.Application")

    'Create a mail item
    Set oOutlookMessage = oOutlookApp.CreateItem(0)

    'Open the HTML file using the FilesystemObject into a TextStream object
    Set oFSTextStream = oFSObj.OpenTextFile(strTempFilePath, 1)
    
                                         
    'Now set the HTMLBody property of the message to the text contained in the TextStream object
    strHTMLBody = oFSTextStream.ReadAll
    
    'By default the range will be centred.  This line left aligns it and you can
    'comment it out if you want the range centred.
    strHTMLBody = Replace(strHTMLBody, "align=center", "align=left", , , vbTextCompare)

    oOutlookMessage.HTMLBody = strHTMLBody

    oOutlookMessage.Display

End Sub

Does anyone have a quick solution?

Kindest Regards

Peter
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Could you set a breakpoint at this line:

strTempFilePath = strTempFilePath & "\DailySalesFigures.htm"

and tell me what the variable strTempFilePath is equal to at this point in the code?


-Mike
 
Upvote 0
could be a reference issue in VBA, but not sure of the ones you need to use to use the outlook references. Browse through them in the VBA editor -> tools -> references.
Chad
 
Upvote 0
I've had this same problem for a while, and I now beleive it's something to do with what Excel has focus set to at time of publish.
I found setting the application to focus on range A1 (could be any range on the publish sheet I believe) prior to publish, stops the error message.
The code I added into my project was this one-liner just before the publish call:

ThisWorkbook.Sheets("PublishSheet").Range("A1").Select

After this focus has been set, it seems to allow the publish to run without error ;)
 
Upvote 0
My publish failed issue was due to the fact, that the file to which I tried to publish was locked... Make sure you have permissions to publish to location where you want to publish/ the file is properly closed in case you do some postpublishing processing with it.

Greets, Jiri
 
Upvote 0
Hello all,


I know it has been a long time since this discussion took place, but I ended up finding it just now and would like to post steps for a solution and an explanation (partially as I do not have full knowledge of the PublishObject and how its properties and methods behave)


I found out that using ActiveX Controls with .publish method from PublishObject fails when using that type of control. For example in the initial post from clares we can see:
Code:
Private Sub CommandButton1_Click()
This i think is a button created from the ActiveX control menu. Once the button is created, upon double clicking on it that piece of code is generated automatically inside the development environment in the same way as mentioned above. Now all the running code is within its scope and it runs ok if executed from the environment, but not if called from the button. Another user in this thread, Nephid, mentions that it has something to do with the focus that Excel has at the moment of execution which indeed seems to be the case. However upon testing the code only worked when the focus was "right", meaning in the sheet where the to be printed ranges existed, while executed through the run button (F5) in the development environment and did not execute through the ActiveX button regardless of focus.

However, I found out that by using Form controls button instead of the ActiveX one it works flawlessly. The solution was to create my code using the .publish method inside a regular sub, then creating a Form Control button, right clicking on it, selecting the "Assign macro".

So to summarize the solution would be like this:
- Check for references (as mentioned in previous posts) making sure that Outlook objects are enabled along with other needed libraries.
- Make sure that you have write access to the location in which you are creating the .htm file
- Last but not least and the one that after all the done tests seemed to be the "winner" use a form control with this method and not ActiveX.

Cheers and hope it helps in case someone else finds a similasr problem.

Kind regards,
Vlad
 
Upvote 0

Forum statistics

Threads
1,223,697
Messages
6,173,895
Members
452,536
Latest member
Chiz511

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