Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-24-2021, 05:41 PM
SamDsouza SamDsouza is offline Just Two Questions on PDF Files Windows 10 Just Two Questions on PDF Files Office 2013
Advanced Beginner
Just Two Questions on PDF Files
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default 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
SamD
65
Reply With Quote
  #2  
Old 05-24-2021, 08:38 PM
Guessed's Avatar
Guessed Guessed is online now Just Two Questions on PDF Files Windows 10 Just Two Questions on PDF Files Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,966
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 05-24-2021, 09:03 PM
SamDsouza SamDsouza is offline Just Two Questions on PDF Files Windows 10 Just Two Questions on PDF Files Office 2013
Advanced Beginner
Just Two Questions on PDF Files
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default

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:
What program are you running the code in - Is this Word 2013?
Yes Windows 10 and MS Office Home and Student 13 so word 13

Quote:
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?
I don't mind word to Open the PDF. But i don't want its effect as Online conversion like PDF to Word
Would Prefer for the original Pagination . But you can recommend

Quote:
What is the point of the PDF view function - what are you going to do with the page?
Rather than Opening Each individual PDF file thought of having minor replica of Each PDF Page in the image box
Thanks
SamD
Reply With Quote
  #4  
Old 05-24-2021, 09:22 PM
gmayor's Avatar
gmayor gmayor is offline Just Two Questions on PDF Files Windows 10 Just Two Questions on PDF Files Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #5  
Old 05-24-2021, 09:31 PM
SamDsouza SamDsouza is offline Just Two Questions on PDF Files Windows 10 Just Two Questions on PDF Files Office 2013
Advanced Beginner
Just Two Questions on PDF Files
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default

Quote:
Change your code as follows:
Code:
Private Sub LstfileBx_Click()
txtFolderPath.Text = LstfileBx.Text
xStrData = ""
Set RegExp = CreateObject("VBscript.RegExp")
RegExp.Global = True
RegExp.Pattern = "/Type\s*/Page[^s]"
xFileNum = FreeFile()
Open txtFolderPath.Text For Binary As #xFileNum

xStrData = Space(LOF(xFileNum))
Get #xFileNum, , xStrData
Close #xFileNum
txtNoOfPAgesPDF.Text = RegExp.Execute(xStrData).Count
End Sub
Rectified it as per your say. But you have taken the following syntaxes before
txtFolderPath.Text = LstfileBx.Text
xStr = ""
May i know Why
Thank you
SamD
Reply With Quote
  #6  
Old 05-25-2021, 03:38 AM
Guessed's Avatar
Guessed Guessed is online now Just Two Questions on PDF Files Windows 10 Just Two Questions on PDF Files Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,966
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #7  
Old 05-25-2021, 08:43 AM
SamDsouza SamDsouza is offline Just Two Questions on PDF Files Windows 10 Just Two Questions on PDF Files Office 2013
Advanced Beginner
Just Two Questions on PDF Files
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default

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:
This way you just return the chosen PDF to the userform (or avoid the userform completely if that was all it was for)
I should not have posted the thread if i had to select the file and open it.

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
Reply With Quote
  #8  
Old 05-25-2021, 05:21 PM
Guessed's Avatar
Guessed Guessed is online now Just Two Questions on PDF Files Windows 10 Just Two Questions on PDF Files Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,966
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
If you are using the userform, you might prefer to delay opening the PDF until you close the userform. In that case, write the value to a control on the userform instead so it can be used when ready.

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
Reply With Quote
  #9  
Old 05-26-2021, 04:17 AM
SamDsouza SamDsouza is offline Just Two Questions on PDF Files Windows 10 Just Two Questions on PDF Files Office 2013
Advanced Beginner
Just Two Questions on PDF Files
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default

Quote:
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.
I've gone through your message and i think Viewer Option is as Good as Opening Normal PDF file.
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:
If you are using the userform, you might prefer to delay opening the PDF until you close the userform. In that case, write the value to a control on the userform instead so it can be used when ready.
Kindly help me with your above statement as I did not understand
SamD
Reply With Quote
  #10  
Old 05-26-2021, 08:47 PM
Guessed's Avatar
Guessed Guessed is online now Just Two Questions on PDF Files Windows 10 Just Two Questions on PDF Files Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,966
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #11  
Old 05-26-2021, 11:21 PM
SamDsouza SamDsouza is offline Just Two Questions on PDF Files Windows 10 Just Two Questions on PDF Files Office 2013
Advanced Beginner
Just Two Questions on PDF Files
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default

Quote:
SamD
I'm having trouble understanding your English.
Was Completely confussed

Quote:
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
OK

Quote:
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.
Fine Now i've understood your point and probably What i was actually trying to do seems NOT practical or Rather NOT POSSIBLE for desired results that Userform to do.

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
Reply With Quote
  #12  
Old 05-27-2021, 01:14 AM
Guessed's Avatar
Guessed Guessed is online now Just Two Questions on PDF Files Windows 10 Just Two Questions on PDF Files Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,966
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #13  
Old 05-27-2021, 08:21 AM
SamDsouza SamDsouza is offline Just Two Questions on PDF Files Windows 10 Just Two Questions on PDF Files Office 2013
Advanced Beginner
Just Two Questions on PDF Files
 
Join Date: Aug 2019
Posts: 71
SamDsouza is on a distinguished road
Default

Andrew Sir,

Quote:
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.
As Earlier suggested by you I agree and conclude being solution being not practical.

Quote:
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.
This was to work on Basics PDF with VBA.
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
Reply With Quote
  #14  
Old 05-27-2021, 10:42 PM
Guessed's Avatar
Guessed Guessed is online now Just Two Questions on PDF Files Windows 10 Just Two Questions on PDF Files Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,966
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
PPT VBA Questions davidmorse PowerPoint 0 03-06-2018 09:35 PM
Just Two Questions on PDF Files 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
Just Two Questions on PDF Files onenote offline cache files questions mdc8989 OneNote 1 02-04-2011 11:47 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:02 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