#1
|
|||
|
|||
Just Two Questions on PDF Files
Hello
I am getting Bad File Name Error when Clicked on Listbox of Files. How to get rid of this Error After getting rid of Error Q1. How Can I get No of Pages of Each PDF File in textbox when cliked on listbox of files ? Q2. Any ideas How i can view contents of Each Page no in Userform Image Box ? FYI PDF are opened in MS-Edge. Code:
Option Explicit Dim xRg As Range Dim xStr As String Dim xFd As FileDialog Dim xFdItem As Variant Dim xFileName As String, xFolderPath As String, xFileExtnFilter As String, xStrData As String Dim xFileNum As Long Dim RegExp As Object Dim xWdApp Dim xWd Public Sub DirPathNames() Set xFd = Application.FileDialog(msoFileDialogFolderPicker) With xFd .Title = "Select the Folder..." If .Show = -1 Then xFdItem = xFd.SelectedItems(1) txtFolderPath.Text = xFdItem Else MsgBox "No Folder Path Selected" Exit Sub End If End With xFolderPath = txtFolderPath.Text xFileExtnFilter = "*.PDF" If Dir(xFolderPath & "\" & xFileExtnFilter) = "" Then MsgBox "There are no files of the type:" & vbCrLf & xFolderPath & "\" & xFileExtnFilter Exit Sub Else xFileName = Dir(xFolderPath & "\" & xFileExtnFilter) Do While xFileName <> "" LstfileBx.AddItem xFolderPath & "\" & xFileName xFileName = Dir Loop End If End Sub Private Sub LstfileBx_Click() txtFolderPath.Text = LstfileBx.Text Open txtFolderPath.Text For Binary As #xFileNum xStrData = Space(LOF(xFileNum)) Get #xFileNum, , xStrData Close #xFileNum txtNoOfPAgesPDF.Text = RegExp.Execute(xStrData).Count End Sub 65 |
#2
|
||||
|
||||
What program are you running the code in - Is this Word 2013?
If you used Word to run the code, do you want Word to open the PDF and does it matter if this changes the pagination? What is the point of the PDF view function - what are you going to do with the page?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Thanks Guessed
First of all Let me tell you. I've been able to rectifcy the Error as i missed some important syntaxes and now i am able get the Page nos correctly in textbox Correction on Bad File Number is rectified as below Code:
Private Sub LstfileBx_Click() Set RegExp = CreateObject("VBscript.RegExp") RegExp.Global = True RegExp.Pattern = "/Type\s*/Page[^s]" xFileNum = FreeFile txtFolderPath.Text = LstfileBx.Text Open txtFolderPath.Text For Binary As #xFileNum xStrData = Space(LOF(xFileNum)) Get #xFileNum, , xStrData Close #xFileNum txtNoOfPAgesPDF.Text = RegExp.Execute(xStrData).Count End Sub Quote:
Quote:
Would Prefer for the original Pagination . But you can recommend Quote:
Thanks SamD |
#4
|
||||
|
||||
I never did figure out how to delete a message, but you appear to have fixed the missing lines in the code while I was investigating.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
|||
|
|||
Quote:
txtFolderPath.Text = LstfileBx.Text xStr = "" May i know Why Thank you SamD |
#6
|
||||
|
||||
Doing a google search to find code which can display PDFs in a vba userform revealed some hits but they appear to not be totally reliable so the performance might be hit or miss on your machines.
If you are using a Application.FileDialog(msoFileDialogFolderPicker) to build a list of PDFs, why don't you use the FilePicker instead and let Windows Preview take care of showing the preview of the PDF? See FileDialogFilters Collection | Microsoft Docs. This way you just return the chosen PDF to the userform (or avoid the userform completely if that was all it was for)
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#7
|
|||
|
|||
Andrew
Really No fun with below code as per your suggestion. As i tried Filepicker for First time. Pressing OK in Msg box Msgbox Disappears and File does not open. Code:
Public Sub DirPathNamesWithFilePicker() Set xFd = Application.FileDialog(msoFileDialogFilePicker) With xFd .Title = "Select the Folder..." .Filters.Clear .Filters.Add "PDF Files", "*.PDF", 1 If .Show = -1 Then For Each xFdItem In .SelectedItems MsgBox "Path Name : " & xFdItem Next xFdItem Else End If End With Set xFd = Nothing End Sub Quote:
As a common user or Data Entry operator for eg not Knowing what PDF is How would they perceive a PDF Image or cover with images with Stylish fonts BUT not Editable. If i were in their Place I would perceive as an Decorative Image with Text in a Rectangular Frame. By the way Bit scared to use the Imagebox1.Picture = LoadPicture(xFditem) and ActiveDocument.Pictures.insert(xFdItem) I dont have anythinng of Adobe installed. with MS-Edge quite happy to view PDF at least SamD |
#8
|
||||
|
||||
Your code isn't telling the file to open, it is just returning the name of the selected files. If you want to open it then you need to have a line of code that does that. The question is, how do you want to open it - in Word as an editable file vs in the default Acrobat viewer (like Reader or Edge). Based on your earlier post, I assume you want the viewer option - the easiest way is therefore to follow a hyperlink to the filepath.
Code:
Public Sub DirPathNamesWithFilePicker() Dim xFD As FileDialog, xFdItem As Variant Set xFD = Application.FileDialog(msoFileDialogFilePicker) With xFD .Title = "Select a file" .Filters.Clear .Filters.Add "Acrobat Files", "*.PDF", 1 If .Show = -1 Then For Each xFdItem In .SelectedItems Debug.Print "Path Name: " & xFdItem ActiveDocument.FollowHyperlink xFdItem Next xFdItem End If End With Set xFD = Nothing End Sub I would be surprised if LoadPicture works on a PDF. I would expect that it would want a graphic file (like a bmp or jpg)
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#9
|
|||
|
|||
Quote:
It would be better if i could open the PDF in MS-Word as Editable rather than MS-Edge File. I tried your code which is added with Debug.Print "Path Name: " & xFdItem ActiveDocument.FollowHyperlink xFdItem I am afraid that nothing happened although an easier way to follow Hyperlink Using the syntax ActiveDocument.FollowHyperlink is for first time. Please bear on my misunderstanding if result of PDF file being opened as per your coding But does not open Quote:
SamD |
#10
|
||||
|
||||
SamD
I'm having trouble understanding your English. If you open the chosen PDF as editable in Word then the pagination and layout is likely to be different to what was in the PDF. Converting a PDF into Word format is rarely perfect. To convert the PDF to Word and open the file you could use Documents.Open FileName:=xFdItem, Format:=wdOpenFormatAuto To answer your second question, I need to understand what you are actually trying to do. Your initial code was for a vba userform and you were asking how to select a file from a list in that userform. The code I have pointed you towards provides an alternative method to view and select a file. Potentially this code completely removes the need for the userform - but I don't know what else you needed the userform to do. If the only reason you needed the userform was to give the user a way to select a file then you no longer need the userform. However, if you are doing OTHER things with the userform then your filepicker might store the selected filename in a text control on the userform and then when the user closes the userform, the actual file conversion/open should happen.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#11
|
|||
|
|||
Quote:
Quote:
Quote:
Sir Andrew In the following thread from MrExcel all the references are for Adobe Acrobat..... and i could not execute the same and see the desired result specifically in MS-Excel because Nothing of Adobe is installed MrExcel Thread How to open & select PDF data and paste it in excel file using VBA code ? | MrExcel Message Board Could you help me for what tool>References that i need to tick for MS-Edge So that i could explore on MS-Word and if required in MS-Excel BTW are Tool>References same in MS-Word and MS-Excel SamD |
#12
|
||||
|
||||
SamD
This won't be solved by you asking for pieces of information. We need to understand what you are trying to do - not just one piece of the puzzle, the entire puzzle. Post your document containing the code you already have. Also post a sample PDF file that you would pick. Explain what the user should see, click and have happen. Your original question was to see a screen preview of a PDF. This is not practical because the suggested solutions vary with versions of Windows, MS Word, Adobe Acrobat, Web Browsers and configurations. The suggested alternative of using the preview in FilePicker avoids some of these configuration issues but not all.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#13
|
|||
|
|||
Andrew Sir,
Quote:
Quote:
My userform consists of One Listbox to view path and Filename, Textbox to show the Folder Path and another Textbox to show no of pages as post #1 and Image box (If possible to View the PDF Page Which is not practical) with the above objects in userform and PDF if you have and if you would like to share What is the possible practical outcome ? Will definately love to go through your sample. Else Forget it Whichever forums i explored all the threads were with Acrobat references. Since i did not have on my PC the Acrobat reader, PRO etc it became rather difficult for me therefore I thought of asking for MS-Edge reference Infact I Should have posted a new thread for MS-Edge reference.This was another mistake. SamD |
#14
|
||||
|
||||
I don't believe that Edge is relevant to this conversation - we aren't using it to display anything.
This thread includes code which is fast enough that it could be adapted to return the page count on multiple pdfs. See excel - How to get the number of pages in a pdf document using VBA? - Stack Overflow
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
PPT VBA Questions | davidmorse | PowerPoint | 0 | 03-06-2018 09:35 PM |
Questions | S_jt_C | Word | 1 | 12-05-2015 04:35 PM |
Converting 'comments' into text files (2 questions) | Paul Lance | Word | 1 | 04-29-2013 06:00 PM |
Few questions...needs some help! | Wolf.Stalker | Outlook | 1 | 02-21-2012 09:15 PM |
onenote offline cache files questions | mdc8989 | OneNote | 1 | 02-04-2011 11:47 AM |