Input past end of file error (62)

db2020

New Member
Joined
Jun 6, 2021
Messages
21
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi,

I get an "Input past end of file" error with the code below after the step "readall = myfile.readall":

VBA Code:
Sub Paste_Pivot()
Sheets("Missing").Select
ActiveSheet.PivotTables("PivotTable1").PivotSelect "", xlDataAndLabel, True
Selection.Copy

Workbooks.Add
ActiveSheet.Paste
Dim newwb As Workbook
Set newwb = ActiveWorkbook

Dim fso As Scripting.FileSystemObject, readall As Variant
Dim myfile As Scripting.TextStream
Set fso = New FileSystemObject
fso.CreateTextFile ("Desktop")
newwb.PublishObjects.Add(xlSoureRange, "Desktop", "sheet1", newwb.Sheets(1).UsedRange.Address, xlHtmlStatic).Publish (True)
Set myfile = fso.OpenTextFile("Desktop")
readall = myfile.readall

Dim o As Outlook.Application
Set o = New Outlook.Application
Dim omail As Outlook.MailItem
Set omail = o.CreateItem(olMailItem)

omail.HTMLBody = "Hello" & "<Br>" & "<table align=left>" & readall & "</table"
omail.Display
End Sub

Any idea how this can be solved? Thanks in advance :)
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Here's what's happening...

fso.CreateTextFile ("Desktop") creates an empty file called "Desktop" in the current directory.

newwb.PublishObjects.Add(xlSoureRange, "Desktop", "sheet1", newwb.Sheets(1).UsedRange.Address, xlHtmlStatic).Publish (True) creates a file called "Desktop.mht" in the current directory.

Set myfile = fso.OpenTextFile("Desktop") opens the text file called "Desktop", not "Desktop.mht".

readall = myfile.readall tries to read the empty file "Desktop", not "Desktop.mht", and so you'll get that error.

So, first of all, you can get rid of this line...

fso.CreateTextFile ("Desktop")

It's not needed. Then you can replace...

VBA Code:
newwb.PublishObjects.Add(xlSoureRange, "Desktop", "sheet1", newwb.Sheets(1).UsedRange.Address, xlHtmlStatic).Publish (True)
Set myfile = fso.OpenTextFile("Desktop")

with

VBA Code:
newwb.PublishObjects.Add(xlSoureRange, "Desktop.mht", "sheet1", newwb.Sheets(1).UsedRange.Address, xlHtmlStatic).Publish (True)
Set myfile = fso.OpenTextFile("Desktop.mht")

And, lastly, you should close your text stream after you've read the contents of your file using the following line...

VBA Code:
myfile.Close

By the way, there's no need to create a new workbook in order to create your html document. Or do you need it for some other use?
 
Upvote 0
Hey Domenic, many thanks for y our feedback! Goal of the macro is to copy the pivot table into an outlook mail, a new workbook is not needed necessarily.

I have the macro working but in the email that is generated I get an error (See attached). How can I solve this?

Below this error the pivot is pasted. Is it possible to paste it with the formatting used in the excel file where its copied from?
 

Attachments

  • 2022-08-08 09_51_15-Window.png
    2022-08-08 09_51_15-Window.png
    64.1 KB · Views: 7
Upvote 0
I have the macro working but in the email that is generated I get an error (See attached). How can I solve this?
You can get rid of the initial part of the text from the file as follows...

VBA Code:
.htmlbody = "Hello" & "<br>" & Mid(readall , InStr(1, readall , "<html", vbTextCompare) - 1)

Is it possible to paste it with the formatting used in the excel file where its copied from?
An alternative would be to export a picture of the pivot table, and then embed it within your email. Have you considered this method?
 
Upvote 0
Unfortunately I get the same error, but if pasting as a picture solves it i am all ears :)
 
Upvote 0
The following macro first exports an image of your pivot table to your local temporary folder, then it creates a new email, then it attaches the exported image to the email, and then it embeds the image within the body of the email, and then it deletes the exported image from your local temporary folder.

Note that the code uses late binding, instead of early binding. So there's no need to set any references, such as the Microsoft Outlook Object Library. Also, note that the code assumes that the workbook running the code contains the pivot table. If you want the code to run based on the active workbook instead, replace...

VBA Code:
Set pt = ThisWorkbook.Worksheets("Missing").PivotTables("PivotTable1")

with

VBA Code:
Set pt = ActiveWorkbook.Worksheets("Missing").PivotTables("PivotTable1")

Here's the code...

VBA Code:
Option Explicit

Sub Email_Pivot()

    Dim pt As PivotTable
    Set pt = ThisWorkbook.Worksheets("Missing").PivotTables("PivotTable1")
 
    Dim tempPath As String
    tempPath = Environ("temp") & "\"
 
    Dim tempFile As String
    tempFile = pt.Name & ".png"
 
    tempFile = Replace(tempFile, " ", "_") 'replace any spaces with underscores
 
    ExportRangeToPNG tempPath & tempFile, pt.TableRange2
 
    Dim olApp As Object
    Set olApp = CreateObject("Outlook.Application")
 
    Dim olMailItem As Object
    Set olMailItem = olApp.createitem(0)
 
    With olMailItem
        .attachments.Add tempPath & tempFile
        .htmlbody = "Hello<br><br><img src=""cid:" & tempFile & """>"
        .display
    End With
 
    Kill tempPath & tempFile
 
End Sub

Public Sub ExportRangeToPNG(ByVal saveAsFilename As String, ByVal rangeToExport As Range)

    rangeToExport.CopyPicture Appearance:=xlScreen, Format:=xlPicture
 
    Dim tempWorksheet As Worksheet
    Set tempWorksheet = rangeToExport.Parent.Parent.Worksheets.Add
 
    With tempWorksheet.ChartObjects.Add(Left:=rangeToExport.Left, Top:=rangeToExport.Top, Width:=rangeToExport.Width + 2, Height:=rangeToExport.Height + 2)
        .Activate
        With .Chart
            Do While .SeriesCollection.Count > 0
                .SeriesCollection(1).Delete
            Loop
            .ChartArea.Format.Line.Visible = msoFalse
            .Paste
            With .Pictures(1)
                .Left = .Left + 2
                .Top = .Top + 2
            End With
            .Export Filename:=saveAsFilename, Filtername:="PNG"
        End With
        .Delete
    End With
 
    Application.DisplayAlerts = False
    tempWorksheet.Delete
    Application.DisplayAlerts = True

End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,789
Messages
6,121,605
Members
449,038
Latest member
Arbind kumar

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