VBA重复PDF文件(VBA duplicate PDF file)

我真的需要帮助。 我需要一个VBA函数来复制单个PDF文件。 例如,一个带有引用/名称1的文件,我需要一个x的副本,比如1到10.为了避免复制和粘贴9次并手动重命名,我确信必须有一个函数来完成这项工作。 我对VBA非常基础,所以任何帮助都会非常感激。

非常感谢

I really need help. I need a VBA function to make copies of a single PDF file. For example a file with the reference/name 1, I would need an amount x of copies lets say 1 to 10. In order to avoid coping and paste 9 times and renaming them manually I am sure there must be a function to do this job. I am very basic with VBA so any help would be much appreciated.

Many Thanks

最满意答案

首先,您需要在VBA编辑器中添加对Microsoft Scripting Runtime的引用。 然后以下将工作......

Public Sub Test() CopyFile "C:\Users\randrews\Desktop\1.gif", "C:\Users\randrews\Desktop", 10 End Sub Public Sub CopyFile(OriginalPath As String, DestinationFolderPath, Copies As Integer) Dim fs As New FileSystemObject For i = 1 To Copies OrigName = fs.GetFileName(OriginalPath) 'file name with extention e.g. 1.gif OrigNumber = CInt(Left(OrigName, Len(OrigName) - 4)) 'file name converted to a number - this will crash if the file name contains any non numeric chars DestName = OrigNumber + i & "." & fs.GetExtensionName(OriginalPath) 'new file name = original number + i + the file extension fs.CopyFile OriginalPath, DestinationFolderPath & "\" & DestName Next i End Sub

First you will need to add a reference to Microsoft Scripting Runtime in the VBA editor. Then the following will work...

Public Sub Test() CopyFile "C:\Users\randrews\Desktop\1.gif", "C:\Users\randrews\Desktop", 10 End Sub Public Sub CopyFile(OriginalPath As String, DestinationFolderPath, Copies As Integer) Dim fs As New FileSystemObject For i = 1 To Copies OrigName = fs.GetFileName(OriginalPath) 'file name with extention e.g. 1.gif OrigNumber = CInt(Left(OrigName, Len(OrigName) - 4)) 'file name converted to a number - this will crash if the file name contains any non numeric chars DestName = OrigNumber + i & "." & fs.GetExtensionName(OriginalPath) 'new file name = original number + i + the file extension fs.CopyFile OriginalPath, DestinationFolderPath & "\" & DestName Next i End Sub

更多推荐