程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 Excel VBA 将许多图像转换为一个 PDF 文件并将每个图像适合一页大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 Excel VBA 将许多图像转换为一个 PDF 文件并将每个图像适合一页?

开发过程中遇到使用 Excel VBA 将许多图像转换为一个 PDF 文件并将每个图像适合一页的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 Excel VBA 将许多图像转换为一个 PDF 文件并将每个图像适合一页的解决方法建议,希望对你解决使用 Excel VBA 将许多图像转换为一个 PDF 文件并将每个图像适合一页有所启发或帮助;

我有很多文件夹,每个文件夹里面可以有很多 .jpg 文件,以及其他类型的文件。

我需要的是使用 Excel vba,我想将所有 .jpg 文件一个接一个地合并到一个 pdf 文件中,该文件在 Excel 中具有特定的单元格文本,并为每个图像指定页码并存储它在 Excel 中的特定列中。

我找到了这段代码,但它有两个问题

  1. 图片在 pdf 文件中占一页以上
  2. 它只用随机的pdf文件转换一张图像

这是代码

Sub Jpg_pdf()
Application.Screenupdating = false
'Declare variables
Dim file As String
Dim path As String
path = "E:\pics\02-09-2015"
file = Dir(path & "\PC_20150902_145901.jpg")
Sheet1.Activate
'Start loop
do while file <> ""
'Insert picturE into Excel
Sheet1.Pictures.Insert (path & "\" & filE)
ActiveSheet.Pictures(ActiveSheet.Pictures.Count).name = "A Picture"
ChDir "E:\pics\02-09-2015"
ActiveSheet.ExportAsFixedFormat Type:=xlTypepdf,filename:=file,_
Quality:=xlQualityStandard,_
IncludeDocPropertIEs:=True,IgnorePrintAreas:=false,OpenAfterPublish:= _
false
ActiveSheet.Shapes.Range(Array("A Picture")).delete
file = Dir()
Loop
Sheet2.Activate
Application.Screenupdating = True
End Sub

解决方法

以下要求您将 Acrobat 和 Windows 脚本宿主对象模型添加到您的参中。

使用 Excel VBA 将许多图像转换为一个 PDF 文件并将每个图像适合一页

使用 Excel VBA 将许多图像转换为一个 PDF 文件并将每个图像适合一页

Public Sub PDFsomePics()
  Dim objFS As FileSystemObject,objFolder As Folder,objFile As File
  Dim objPDFout As AcroPDDoc,objPDFpage As AcroPDDoc
  Dim strFileType As String,strPathtoFolders As String,strPDFFilename As String
  Dim i As Long
  Dim numMaxHeight As Single,numMaxWidth As Single

  'Optional
  Sheet1.UsedRange = "" 'Clears the picture list

  'Set up page parameters
  numMaxWidth = 8.5 - 0.5 - 0.5 - 0.5 '0.5" buffer
  numMaxHeight = 11 - 0.5 - 0.5 - 0.5 '0.5" buffer

  With Sheet2.PageSetup
    .CenterHorizontally = True
    .CenterVertically = True
    .RightMargin = 0.5
    .LeftMargin = 0.5
    .TopMargin = 0.5
    .bottomMargin = 0.5
    .orientation = xlPorTrait 'xlLandscape '
  End With

  'Initialize PDFs and file system
  Set objPDFout = New Acrobat.AcroPDDoc
  Set objPDFpage = New Acrobat.AcroPDDoc
  Set objFS = New FileSystemObject
  
  strFileType = "JPG File"
  strPathtoFolders = "drive:\path\to\folders\"
  strPDFFilename = "output.pdf"
  i = 1
  
  objPDFout.Create
  
  Set objFolder = objFs.GetFolder(strPathtoFolders)

  'Go through every subfolder of the target folder and PDFify then combine PDFs
  'For Each objFolder In objFs.GetFolder(strPathtoFolders).SubFolders
      For Each objFile In objFolder.Files
          If objFile.Type = strFileType Then
              'Record filename,save link to file,and record page number
              Sheet1.Cells(i,1) = objFile.Name
              Sheet1.Hyperlinks.Add Sheet1.Cells(i,1),objFile.Path
              Sheet1.Cells(i,2) = i
              i = i + 1 'increment page counter
              
              'Insert and resize picture
              With Sheet2.Pictures.Insert(objFile.Path)
                  With .ShapeRange
                      .LockAspectRatio = True
                      
                      'Set width while locked ratio
                      .Width = numMaxWidth * 72 '72 points per inch
                      
                      'If height went over the page height,then set height instead
                      If .Height > numMaxHeight * 72 Then .Height = numMaxHeight * 72
                  End With
                  
                  'Place the picture in the top-left most cell
                  .Left = Sheet2.Cells(1,1).Left
                  .Top = Sheet2.Cells(1,1).Top
                  .Placement = 1
              End With
              
              'Export sheet to PDF
              Sheet2.ExportAsFixedFormat Type:=xlTypePDF,Filename:=strPathtoFolders & i,_
                  Quality:=xlQualityStandard,IncludeDocProperties:=True,_
                  IgnorePrintAreas:=True,OpenAfterPublish:=false
             
              'Get rid of picture from sheet
              Sheet2.Pictures(1).delete
            
              'Open PDF with picture,append to output PDF,then close and delete PDF with picture
              objPDFpage.open strPathtoFolders & i & ".pdf"
              objPDFout.InsertPages objPDFout.GetNumPages - 1,objPDFpage,objPDFpage.GetNumPages,True
              objPDFpage.Close
              objFs.deleteFile strPathtoFolders & i & ".pdf"
          End If
      Next
  'Next

  'Save final PDF
  objPDFout.Save 1,strPathtoFolders & strPDFFilename
End Sub
,

这是将提供给我的两个代码组合后的代码 https://stackoverflow.com/users/13302/marc-s https://stackoverflow.com/users/2497009/usncahill 是否有任何方法或方法可以缩短此代码并使用户通过打开文件对话框窗口选择文件夹 我想要超链接打开pdf文件并转到PDF文件中的选定图像的另一件事 最后一个要求是,对于每个图像,我想生成一个 3 列的表,第一列保存图像的名称,第二列保存以结果 PDF 文件中的图像位置为标题的超链接,第三列保存对所有组合图像重复的 PDF 文件的标题此 PDF 文件

  Dim objFS As FileSystemObject,objFile As file
  Dim objPDFout As AcroPDDoc,numMaxWidth As Single

  'Optional
  Sheet1.UsedRange = "" 'Clears the picture list

  'Set up page parameters
  numMaxWidth = 8.5 - 0.5 - 0.5 - 0.5 '0.5" buffer
  numMaxHeight = 11 - 0.5 - 0.5 - 0.5 '0.5" buffer

  With Sheet2.PageSetup
    .CenterHorizontally = True
    .CenterVertically = True
    .RightMargin = 0.5
    .LeftMargin = 0.5
    .TopMargin = 0.5
    .bottomMargin = 0.5
    .orientation = xlPorTrait 'xlLandscape '
  End With

  'Initialize PDFs and file system
  Set objPDFout = New Acrobat.AcroPDDoc
  Set objPDFpage = New Acrobat.AcroPDDoc
  Set objFS = New FileSystemObject
  
  strFileType = "JPG File"
  strPathtoFolders = "C:\Users\Attorney\Desktop\"
  strPDFFilename = "output.pdf"
  i = 1
  
  objPDFout.Create
  
  Set objFolder = objFs.GetFolder(strPathtoFolders)

  'Go through every subfolder of the target folder and PDFify then combine PDFs
  'For Each objFolder In objFs.GetFolder(strPathtoFolders).SubFolders
      For Each objFile In objFolder.Files
          If objFile.Type = strFileType Then
              'Record filename,objFile.path
              Sheet1.Cells(i,2) = i
              i = i + 1 'increment page counter
              
              'Insert and resize picture
              With Sheet2.Pictures.Insert(objFile.path)
                  With .ShapeRange
                      .LockAspectRatio = True
                      
                      'Set width while locked ratio
                      .Width = numMaxWidth * 72 '72 points per inch
                      
                      'If height went over the page height,True
              objPDFpage.Close
              objFs.deleteFile strPathtoFolders & i & ".pdf"
          End If
      Next
  'Next
  
  On Error GoTo Skip
    For Each objFolder In objFs.GetFolder(strPathtoFolders).SubFolders
        For Each objFile In objFolder.Files
            If objFile.Type = strFileType Then
              Sheet1.Cells(i,True
              objPDFpage.Close
              objFs.deleteFile strPathtoFolders & i & ".pdf"
          End If
        Next
    Next
Skip:
  'Save final PDF
  objPDFout.Save 1,strPathtoFolders & strPDFFilename
End Sub```

大佬总结

以上是大佬教程为你收集整理的使用 Excel VBA 将许多图像转换为一个 PDF 文件并将每个图像适合一页全部内容,希望文章能够帮你解决使用 Excel VBA 将许多图像转换为一个 PDF 文件并将每个图像适合一页所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:使用