Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-09-2012, 08:30 AM
tinfanide tinfanide is offline Error: Input past end of file Windows 7 64bit Error: Input past end of file Office 2010 32bit
Expert
Error: Input past end of file
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default Error: Input past end of file

Code:
Sub pdf()

Const sFile As String = "C:\test\1.pdf"
Const oFile As String = "C:\test\1.log"
Dim m, n As Integer
Dim str As String

m = FreeFile()
Open sFile For Binary As m
n = FreeFile()
Open oFile For Output As n
Do While Not EOF(m)
    Line Input #m, str
    Print #n, str
Loop

''' error
''' Input past end of file
Close m
Close n

End Sub
With the above codes, I want to write the binary data into a new log file for the purpose of counting the pages of the pdf file (m).
It runs as expected but fails before closing the n file (log file).
Reply With Quote
  #2  
Old 06-09-2012, 10:50 AM
tinfanide tinfanide is offline Error: Input past end of file Windows 7 64bit Error: Input past end of file Office 2010 32bit
Expert
Error: Input past end of file
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default

Code:
Sub pdf()

Const sFile As String = "C:\test\1.pdf"

Dim f As Integer
Dim byt As Byte
Dim str As String, str1 As String

f = FreeFile
Open sFile For Binary Access Read As #f

Do While Loc(f) < LOF(f)
    Get f, , byt
    str1 = str1 & byt & vbNewLine
Loop

Close m

''' str1
''' different from the output when I use C# StreamReader to extract the data from the pdf file

End Sub
I guess there are many problems to be resolved if I really want to use VBA to count the pages of a pdf file.

First, I bet there is something to do with Binary.
The error "Input past end of file" seems to be caused by reading binary files and I think using Loc() could have helped solve it.
Second, I declare a variable for the binary data. I think it works with the binary data extracted from the pdf file.

But third, the output (string str1) is still different from what I get from a C# code (which works well to count the pages of a pdf file).

Anyway, I think I still need to be working hard on it since even I get the page count from the C# code. It cannot be used in Excel.
Reply With Quote
  #3  
Old 06-11-2012, 09:31 PM
macropod's Avatar
macropod macropod is offline Error: Input past end of file Windows 7 64bit Error: Input past end of file Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi tinfanide,

If you have Adobe Acrobat Pro installed, you can automate Acrobat Pro for the processing:
Code:
Public Function ReadAcrobatDocument(strFileName As String, Optional iPage As Long) As String
'Note: A Reference to the Adobe Library must be set in Tools|References!
Dim AcroApp As CAcroApp, AcroAVDoc As CAcroAVDoc, AcroPDDoc As CAcroPDDoc
Dim AcroHiliteList As CAcroHiliteList, AcroTextSelect As CAcroPDTextSelect
Dim PageNumber, PageContent, Content, i, j
Set AcroApp = CreateObject("AcroExch.App")
Set AcroAVDoc = CreateObject("AcroExch.AVDoc")
If AcroAVDoc.Open(strFileName, vbNull) <> True Then Exit Function
' The following While-Wend loop shouldn't be necessary but timing issues may occur.
While AcroAVDoc Is Nothing
  Set AcroAVDoc = AcroApp.GetActiveDoc
Wend
Set AcroPDDoc = AcroAVDoc.GetPDDoc
If iPage <= AcroPDDoc.GetNumPages And iPage <> 0 Then
  Set PageNumber = AcroPDDoc.AcquirePage(iPage - 1)
  Set PageContent = CreateObject("AcroExch.HiliteList")
  If PageContent.Add(0, 9000) <> True Then Exit Function
  Set AcroTextSelect = PageNumber.CreatePageHilite(PageContent)
  ' The next line is needed to avoid errors with protected PDFs that can't be read
  On Error Resume Next
  For j = 0 To AcroTextSelect.GetNumText - 1
    Content = Content & AcroTextSelect.GetText(j)
  Next j
End If
If iPage = 99999 Then
  For i = 0 To AcroPDDoc.GetNumPages - 1
    Set PageNumber = AcroPDDoc.AcquirePage(i)
    Set PageContent = CreateObject("AcroExch.HiliteList")
    If PageContent.Add(0, 9000) <> True Then Exit Function
    Set AcroTextSelect = PageNumber.CreatePageHilite(PageContent)
    ' The next line is needed to avoid errors with protected PDFs that can't be read
    On Error Resume Next
    For j = 0 To AcroTextSelect.GetNumText - 1
      Content = Content & AcroTextSelect.GetText(j)
    Next j
  Next i
ElseIf iPage > AcroPDDoc.GetNumPages Then
  Content = "Error!: The PDF file has only " & AcroPDDoc.GetNumPages & " Pages."
End If
If iPage = 0 Then Content = AcroPDDoc.GetNumPages
ReadAcrobatDocument = Content
AcroAVDoc.Close True
AcroApp.Exit
Set AcroAVDoc = Nothing: Set AcroApp = Nothing
End Function
The above function takes two arguments:
• the PDF's full path & name; and
• optionally, the page number to be retrieved
If you:
• omit the second parameter or give it a 0 value, you'll get just the page count;
• specify 99999 as the second parameter, you'll get the text contents of the PDF;
• specify a valid page #, you'll get that page's text contents; or
• specify a valid page #, you'll get an error output.
You can call the function with a sub like:
Code:
Sub Demo()
Dim strPDF As String, strTmp As String, i As Integer
' The next ten lines and the last line in this sub can help if
' you get "ActiveX component can't create object" errors even
' though a Reference to Acrobat is set in Tools|References.
Dim bTask As Boolean
  bTask = True
If Tasks.Exists(Name:="Adobe Acrobat Professional") = False Then
  bTask = False
  Dim AdobePath As String, WshShell As Object
  Set WshShell = CreateObject("Wscript.shell")
  AdobePath = WshShell.RegRead("HKEY_CLASSES_ROOT\acrobat\shell\open\command\")
  AdobePath = Trim(Left(AdobePath, InStr(AdobePath, "/") - 1))
  Shell AdobePath, vbHide
End If
strPDF = ReadAcrobatDocument("C:\test\1.pdf", 0)
ActiveDocument.Range.InsertAfter strPDF
If bTask = False Then Tasks.Item("Adobe Acrobat Professional").Close
End Sub
Note1: A Reference to the Adobe Library must be set in Tools|References.
Note2: The sub is written for Word (eg ActiveDocument), but you can change that easily enough to work with Excel or any other app.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error: Input past end of file error: You are attempting to save a file format that is blocked by file block setting zatlas1 Office 8 04-28-2012 02:37 PM
Error: Input past end of file Error File in Word gleckie Word 2 02-02-2012 07:51 AM
Error: Input past end of file File Permission Error Pianoman 74 Word 3 01-26-2012 03:56 PM
File Conversion Error azii Word 0 01-04-2012 04:34 AM
Runtime error 5487 - Word cannot complete the save to to file permission error franferns Word 0 11-25-2009 05:35 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:44 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft