Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1680|回复: 7

[已解决]请教

[复制链接]
发表于 2013-6-8 14:30 | 显示全部楼层 |阅读模式
在一个文件夹下很多表 怎么才能实现批量打印??
最佳答案
2013-6-10 09:29
  1. Sub Main()
  2.     Dim strPath As String
  3.     Dim strFilename As String
  4.     Dim iCount As Integer
  5.     Dim arrFilename(1 To 1024) As String
  6.     With Application.FileDialog(msoFileDialogFolderPicker)
  7.         .AllowMultiSelect = False
  8.         If .Show = -1 Then
  9.             strPath = .SelectedItems(1)
  10.         Else
  11.             MsgBox "没有选择文件夹" & vbCrLf & "点 确定 后代码结束", vbCritical + vbOKOnly
  12.             Exit Sub
  13.         End If
  14.     End With
  15.    
  16.     If Not Right(strPath, 1) Like "" Then strPath = strPath & Application.PathSeparator

  17.     strFilename = Dir(strPath & "*.xls")
  18.     Do While Len(strFilename) > 0 And strFilename <> ThisWorkbook.Name
  19.         Debug.Print strFilename
  20.         iCount = iCount + 1
  21.         arrFilename(iCount) = strPath & strFilename
  22.         strFilename = Dir
  23.     Loop
  24.     '所有的完全文件名都在数组arrFilename中
  25.     '另外需要注意的是,当文件夹内文件数量大于1024时,就会出错。数组固定了。
  26. End Sub
复制代码
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-6-8 15:00 | 显示全部楼层
写代码,循环处理就可以实现了

要不你自己手工操作也可以实现

再不就按排别人去手工操作,这三个方法都可以
回复

使用道具 举报

 楼主| 发表于 2013-6-9 09:22 | 显示全部楼层
无聊的疯子 发表于 2013-6-8 15:00
写代码,循环处理就可以实现了

要不你自己手工操作也可以实现

晕  无聊的回答啊
肯定用高效的办法啊
回复

使用道具 举报

发表于 2013-6-9 12:42 | 显示全部楼层
看是否合适?

一键打印.zip

25.26 KB, 下载次数: 6

评分

参与人数 1 +6 收起 理由
liyh67 + 6 很给力!

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2013-6-10 08:28 | 显示全部楼层
无聊的疯子 发表于 2013-6-8 15:00
写代码,循环处理就可以实现了

要不你自己手工操作也可以实现

疯子 麻烦你更我解决哈这个问题嘛 我想提取某个文件夹下的excel文档可以用图片的那种选择方式,把文件名放于数组中
测试.jpg
回复

使用道具 举报

发表于 2013-6-10 09:24 | 显示全部楼层
你这个对话框得用API才能实现。
插入一个模块,代码放在模块中。
  1. Private Type BROWSEINFO
  2.     hOwner As Long
  3.     pidlRoot As Long
  4.     pszDisplayName As String
  5.     lpszTitle As String
  6.     ulFlags As Long
  7.     lpfn As Long
  8.     lParam As Long
  9.     iImage As Long
  10. End Type

  11. Const BIF_RETURNONLYFSDIRS = &H1

  12. Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
  13. Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long

  14. Function ChooseFolder(Optional strTitle As String = "请选择文件夹") As String
  15.     Dim bi As BROWSEINFO
  16.     Dim pidl As Long
  17.     Dim strPath As String * 512
  18.     Dim lResult As Long
  19.     Dim iPos As Integer
  20.     With bi
  21.         .hOwner = 0&
  22.         .pidlRoot = 0&
  23.         .lpszTitle = strTitle
  24.         .ulFlags = BIF_RETURNONLYFSDIRS
  25.     End With

  26.     pidl = SHBrowseForFolder(bi)
  27.     strPath = Space$(512)
  28.     lResult = SHGetPathFromIDList(ByVal pidl&, ByVal strPath)
  29.     If lResult Then
  30.         iPos = InStr(strPath, Chr$(0))
  31.         ChooseFolder = Left(strPath, iPos - 1)
  32.     End If
  33. End Function

  34. Sub Main()
  35.     Dim strPath As String
  36.     Dim strFilename As String
  37.     Dim iCount As Integer
  38.     Dim arrFilename(1 To 1024) As String
  39.     strPath = ChooseFolder()
  40.     If Len(strPath) = 0 Then
  41.         MsgBox "没有选择文件夹" & vbCrLf & "点 确定 后代码结束", vbCritical + vbOKOnly
  42.         Exit Sub
  43.     End If
  44.     If Not Right(strPath, 1) Like "" Then strPath = strPath & Application.PathSeparator
  45.     strFilename = Dir(strPath & "*.xls")
  46.     Do While Len(strFilename) > 0 And strFilename <> ThisWorkbook.Name
  47.         Debug.Print strFilename
  48.         iCount = iCount + 1
  49.         arrFilename(iCount) = strPath & strFilename
  50.         strFilename = Dir
  51.     Loop
  52.     '所有的完全文件名都在数组arrFilename中
  53.    
  54. End Sub
复制代码
回复

使用道具 举报

发表于 2013-6-10 09:29 | 显示全部楼层    本楼为最佳答案   
  1. Sub Main()
  2.     Dim strPath As String
  3.     Dim strFilename As String
  4.     Dim iCount As Integer
  5.     Dim arrFilename(1 To 1024) As String
  6.     With Application.FileDialog(msoFileDialogFolderPicker)
  7.         .AllowMultiSelect = False
  8.         If .Show = -1 Then
  9.             strPath = .SelectedItems(1)
  10.         Else
  11.             MsgBox "没有选择文件夹" & vbCrLf & "点 确定 后代码结束", vbCritical + vbOKOnly
  12.             Exit Sub
  13.         End If
  14.     End With
  15.    
  16.     If Not Right(strPath, 1) Like "" Then strPath = strPath & Application.PathSeparator

  17.     strFilename = Dir(strPath & "*.xls")
  18.     Do While Len(strFilename) > 0 And strFilename <> ThisWorkbook.Name
  19.         Debug.Print strFilename
  20.         iCount = iCount + 1
  21.         arrFilename(iCount) = strPath & strFilename
  22.         strFilename = Dir
  23.     Loop
  24.     '所有的完全文件名都在数组arrFilename中
  25.     '另外需要注意的是,当文件夹内文件数量大于1024时,就会出错。数组固定了。
  26. End Sub
复制代码
回复

使用道具 举报

发表于 2013-6-10 09:32 | 显示全部楼层
还是用系统自带的好点,用API太麻烦了,除非是不得借助API才能完在的功能。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-20 11:17 , Processed in 0.374505 second(s), 15 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表