Looking for VB6 code to programmatically convert PDF to Excel. Experts Exchange. Code snippet will save to pdf so I'm thinking/hoping that if I can find the correct nType enumeration for an Excel file type, then it will create an Excel file. You want local (VB6) code, so an online tool won't do it for you, but there is a local.
PDF viewer component is an easy and reliable solution for the developers to embed pdf documents in a vb application.
With PDF viewer component, it's easy for the vb developers to embed an existing pdf files in a form then disable the print, save and copy functions.
At form load event the developers can use the LoadFile method to load pdf files from the hard drive or server and open in an area on the form. The PDF file can be read-only, full edited, or prohibited to save by different requirements. The developers can also change the pdf display options easily with the vb 6 component.
Click Here to Download PDF Viewer Component - Support vb6, vb.net project
Display PDF Files in VB 6 Form
In this part, we will show the detailed sample code on how to create and customize PDF document windows in VB 6 class. But before this, please make sure that you have embedded PDF Viewer Component into your created VB 6 desktop application.
If you haven't the pdfviewer.ocx file, you need to install the package firstly.
Start Visual Basic and create a new standard project. Form1 is created by default.
Right click on any of the tabs in your toolbox and click choose items.
In the pop up dialog, check the PDF Viewer Component.
Click the Ok button.
The PDF Viewer Component was added in the Toolbar window.
Switch to a VB 6 form, then add the component in it.
The component includes lots of methods, events and properties to customize the Adobe Reader window.
The developer can use the following code to open a pdf file in the Form_Load Event.
Private Sub OpenPDF_Click()
With CommonDialog1
.DefaultExt = 'pdf'
.Filter = 'PDF File Formats (*.pdf)|*.pdf|All Files (*.*) | *.* ||'
.FilterIndex = 1
End With
CommonDialog1.ShowOpen
PDFViewer1.LoadFile CommonDialog1.FileName
End Sub
With CommonDialog1
.DefaultExt = 'pdf'
.Filter = 'PDF File Formats (*.pdf)|*.pdf|All Files (*.*) | *.* ||'
.FilterIndex = 1
End With
CommonDialog1.ShowOpen
PDFViewer1.LoadFile CommonDialog1.FileName
End Sub
To protect the Excel worksheet from modification by the end user, the developer needs to add the following code in the DocumentOpened event.
Private Sub EDOffice_DocumentOpened()
EDOffice1.ProtectDoc 1 ' XlProtectTypeNormal
End Sub
EDOffice1.ProtectDoc 1 ' XlProtectTypeNormal
End Sub
There are some wrapped excel automation methods available to create or modify Excel data.
bool ExcelAddWorkSheet(long Index);
bool ExcelDeleteWorkSheet(long Index);
bool ExcelActivateWorkSheet(long Index);
long ExcelGetWorkSheetCount();
bool ExcelSetCellValue(long Column, long Row, BSTR Value);
BSTR ExcelGetCellValue(long Column, long Row);
bool ExcelSetRowHeight(long Row, double Height);
bool ExcelSetColumnWidth(long Column, double Width);
afx_msg bool ExcelDeleteRow(long Row);
bool ExcelDeleteColumn(long Column);
bool ExcelInsertRow(long Row);
bool ExcelInsertColumn(long Column);
bool ExcelInsertPageBreakInRow(long Row);
bool ExcelInsertPageBreakInColumn(long Column);
bool ExcelCopyToClipboard();
bool ExcelPasteStringToWorksheet(BSTR bstText);
bool ExcelDeleteWorkSheet(long Index);
bool ExcelActivateWorkSheet(long Index);
long ExcelGetWorkSheetCount();
bool ExcelSetCellValue(long Column, long Row, BSTR Value);
BSTR ExcelGetCellValue(long Column, long Row);
bool ExcelSetRowHeight(long Row, double Height);
bool ExcelSetColumnWidth(long Column, double Width);
afx_msg bool ExcelDeleteRow(long Row);
bool ExcelDeleteColumn(long Column);
bool ExcelInsertRow(long Row);
bool ExcelInsertColumn(long Column);
bool ExcelInsertPageBreakInRow(long Row);
bool ExcelInsertPageBreakInColumn(long Column);
bool ExcelCopyToClipboard();
bool ExcelPasteStringToWorksheet(BSTR bstText);
Disable Adobe Reader Edit Function from Visual Basic
In the code window for Form1, insert the following code:
Private Sub DisableAdobeReader_Click()
PDFViewer1.SetReadOnly
PDFViewer1.DisableHotKeyCopy
PDFViewer1.DisableHotKeyPrint
PDFViewer1.DisableHotKeySave
PDFViewer1.DisableHotKeySearch
PDFViewer1.DisableHotKeyShowBookMarks
PDFViewer1.DisableHotKeyShowThumnails
PDFViewer1.DisableHotKeyShowToolbars
End Sub
PDFViewer1.SetReadOnly
PDFViewer1.DisableHotKeyCopy
PDFViewer1.DisableHotKeyPrint
PDFViewer1.DisableHotKeySave
PDFViewer1.DisableHotKeySearch
PDFViewer1.DisableHotKeyShowBookMarks
PDFViewer1.DisableHotKeyShowThumnails
PDFViewer1.DisableHotKeyShowToolbars
End Sub
Press F5 to run the project. The pdf file will be open in the vb 6 window. The user can not changed or copy the pdf files.
With the PDF viewer component, you are not only able to view PDF documents, but also able to disable print, disable copy, disable edit and change the pdf window options.
I need some urgent help. I need someone with extensive knowledge of PDFs and vb to look at the following code and help me figure out why the merge is not happening. I am basically trying to merge pdf files in subdirectories and save the result in the individual subdirectory folders. That is, I need each subdirectory to merge all the pdf files in that subdirectory and produce a 'FinalMerged.pdf' which will be added to the list of PDF files already in there which we just merged. For example: C:reportbooksfinancialsNewYork has 5 pdf files, C:reportbooksfinancialsSt. Louis has 6 pdf files, C:reportbooksfinancialsSan Antonio has 4 pdf files. After the merge C:reportbooksfinancialsNewYork should have 6 pdf files (5+finalmerged.pdf), C:reportbooksfinancialsSt. Louis has 7 pdf files (6+finalmerged.pdf), C:reportbooksfinancialsSan Antonio has 5 pdf files (4+finalmerged.pdf).
Here is the code I have currently (There is a form and a module)
Form Code:
Private Sub Command1_Click()
' Declare Constants
Const SOURCE_PATH = 'C:Test'
Const DEST_PATH = 'c:ReportBooksFinancials'
Const FILE_EXT = 'pdf'
' Declare variables
Dim fso, objSourceFolder, objFile, strDestPath, strFinalPath, nPos, ePos
Dim vaPath, nEnd, nLoop, sPath, nCnt, strExt, strNewFileName, strNewFolderName
' Create Objects
Set fso = CreateObject('Scripting.FileSystemObject')
' Get the source folder
Set objSourceFolder = fso.GetFolder(SOURCE_PATH)
' Set the destination path
strDestPath = DEST_PATH
If (Right(strDestPath, 1) <> ') Then
' Make sure it ends in
strDestPath = strDestPath & '
End If
' Process each File in the Folder
For Each objFile In objSourceFolder.Files
' Check the Extension
strExt = fso.GetExtensionName(objFile.Name)
If Trim(strExt) = FILE_EXT Then
' Get the 'State'
Dim FileName
FileName = objFile.Name
nPos = InStr(FileName, '-')
ePos = InStr(FileName, ',')
strNewFolderName = Mid(FileName, nPos + 1, (ePos - 1) - nPos)
' Get the final path to store the files
strFinalPath = strDestPath & strNewFolderName & '
' Check to see if the directory exists
If Not fso.FolderExists(strFinalPath) Then
vaPath = Split(strFinalPath, ')
nEnd = UBound(vaPath) - 1
sPath = vaPath(0)
For nLoop = 1 To nEnd
sPath = sPath & ' & vaPath(nLoop)
If Not fso.FolderExists(sPath) Then
fso.CreateFolder sPath
End If
Next
End If
' Remove the state from the file name
nPos = InStr(FileName, '-')
strNewFileName = Left(FileName, nPos - 2) & '.' & strExt
' strFinalPath = strFinalPath & strNewFileName
' Copy/Move the file
objFile.Copy (strFinalPath)
'objFile.Move( strFinalPath )
End If
Next
' Pass input to Module
MergeFiles 'C:ReportBooksFinancials'
End Sub
Module Code:
Option Explicit
Public Function ListFiles( _
ByVal Path As String, _
Optional ByVal NestedDirs As Boolean = False, _
Optional ByVal strPattern As String = '*.*' _
) As String()
Dim fso As New Scripting.FileSystemObject
Dim fld As Scripting.Folder
Dim fileList As String
' get the starting folder
Set fld = fso.GetFolder(Path)
' let the private subroutine do all the work
fileList = ListFilesPriv(fld, NestedDirs, strPattern)
' convert to a string array
' (the first element will be a null string) - Fixed
If Len(fileList) > 0 Then
fileList = Right$(fileList, Len(fileList) - 1)
End If
ListFiles = Split(fileList, ';')
End Function
' private procedure that returns a file list as a comma-delimited list of files
Private Function ListFilesPriv(ByVal fld As Scripting.Folder, ByVal NestedDirs As Boolean, ByVal strPattern As String) As String
Dim fil As Scripting.File
Dim subfld As Scripting.Folder
' list all the files in this directory
For Each fil In fld.Files
If fil.Name Like strPattern Then
ListFilesPriv = ListFilesPriv & ';' & fil.Path
End If
Next
' if requested, search also subdirectories
If NestedDirs Then
For Each subfld In fld.SubFolders
ListFilesPriv = ListFilesPriv & ListFilesPriv(subfld, NestedDirs, strPattern)
Next
End If
End Function
Sub MergeFiles(ByRef strDirectory As String, Optional ByRef blnLog As Boolean = True, Optional ByRef strLog As String = 'AcroMerge.log')
Dim oAcroMain As Object
Dim oAcroSlave As Object
Dim sTargetFile As String
Dim vaSourceFiles As Variant
Dim sCommandLine As String
Dim slogfile As String
Dim bLog As Boolean
Dim lLoop As Long
Dim bGotMainFile
bLog = blnLog
slogfile = strLog
If bLog Then WriteLog slogfile, vbCrLf & 'AcroMerge starting...' & Date & ' ' & Time
If Right(strDirectory, 1) <> ' Then
strDirectory = strDirectory & '
End If
' Puts an array of filenames into vaSourceFiles to work with in rogs code
vaSourceFiles = ListFiles('c:reportbooksfinancials', True, '*.pdf')
If UBound(vaSourceFiles) = 0 Then
If bLog Then WriteLog slogfile, 'Fatal Error: No source document specified.'
Exit Sub
End If
' Start Merge Process
Set oAcroMain = CreateObject('ACROEXCH.PDDOC')
Set oAcroSlave = CreateObject('ACROEXCH.PDDOC')
lLoop = UBound(vaSourceFiles)
bGotMainFile = False
Dim aDist As New ACRODISTXLib.PdfDistiller
Dim AcroApp As CAcroApp
Dim PDDoc As CAcroPDDoc
Dim InsertPDDoc As CAcroPDDoc
Dim iNumberOfPagesToInsert As Integer, iLastPage As Integer
Do While lLoop > 0 And bGotMainFile = False
If bLog Then WriteLog slogfile, 'Opening: ' & vaSourceFiles(lLoop)
Set AcroApp = CreateObject('AcroExch.App')
Set PDDoc = CreateObject('AcroExch.PDDoc')
Set InsertPDDoc = CreateObject('AcroExch.PDDoc')
'/ Hide the Acrobat window
AcroApp.Hide
'/ Create a Blank PDDoc (The Merge File) .
If PDDoc.Create = False Then
MsgBox 'Creation not successful'
End
End If
InsertPDDoc.Open vaSourceFiles(lLoop) 'PDF File
iNumberOfPagesToInsert = InsertPDDoc.GetNumPages
iLastPage = PDDoc.GetNumPages
PDDoc.InsertPages iLastPage - 1, InsertPDDoc, 0, iNumberOfPagesToInsert, True
InsertPDDoc.Close
lLoop = lLoop - 1
Loop
'/ Save the PDF File Optimized
PDDoc.Save &H5, 'FinalMerged.pdf'
' Close the PDDoc
PDDoc.Close
' Close Acrobat Exchange
AcroApp.Exit
Screen.MousePointer = 0
Set AcroApp = Nothing
Set PDDoc = Nothing
Set InsertPDDoc = Nothing
oAcroMain.Close
Set oAcroMain = Nothing
Set oAcroSlave = Nothing
End Sub
Sub WriteLog(slogfile As String, sData As String)
Dim ifileno As Integer
ifileno = FreeFile()
Open slogfile For Append As ifileno
Print #ifileno, sData
Close #ifileno
End Sub
I have all the references in VB and the Components loaded. This is extremely urgent and I am giving very high points for this. I would like to make the existing code work.
Thanks!