【发布时间】:2018-08-10 04:43:29
【问题描述】:
这让我发疯:我在 powerpoint vba 中有一个 sub 和一个函数。
sub 首先允许我选择一个目录。从 sub 调用的函数在 dir 中找到一个文件。我希望它作为 sub 之外的功能,因为我需要多次使用它。
sub 仍在开发中,所以做的不多,但可以工作。如果我给它做点什么,该函数也可以工作——比如打开找到的文件(即在下面的代码中取消注释该行)——但我不能终生让它将 filePath 返回给 sub。请帮忙!
子:
Sub ManagementSummaryMerge()
Dim folderPath As String
'select dir
Dim FldrPicker As FileDialog
Set pptApp = CreateObject("PowerPoint.Application")
pptApp.Visible = True
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
folderPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
folderPath = folderPath
If folderPath = "" Then GoTo EndOfSub
'set _Main <= string I want to look for
Dim v As String
v = "_Main"
Dim fullFilePathIWantToSet As String
'set value of fullFilePathIWantToSet from findFile function
fullFilePathIWantToSet = findFile(folderPath, v)
'when I test, this MsgBox appears, but blank
MsgBox fullFilePathIWantToSet
'If I can get this working properly, I want to be able to do something like this:
'objFSO.CopyFile fullFilePathIWantToSet, duplicateFilePath
'Presentations.Open (duplicateFilePath)
'numSlides = ActivePresentation.Slides.Count
'etc
EndOfSub:
'let the sub end
End Sub
功能:
Function findFile(ByRef folderPath As String, ByVal v As String) As String
Dim fileName As String
Dim fullFilePath As String
Dim duplicateFilePath As String
Dim numFolders As Long
Dim numSlides As Integer
Dim folders() As String
Dim i As Long
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
ileName = Dir(folderPath & "*.*", vbDirectory)
While Len(fileName) <> 0
If Left(fileName, 1) <> "." Then
fullFilePath = folderPath & fileName
duplicateFilePath = folderPath & "duplicate " & fileName
If (GetAttr(fullFilePath) And vbDirectory) = vbDirectory Then
ReDim Preserve folders(0 To numFolders) As String
folders(numFolders) = fullFilePath
numFolders = numFolders + 1
Else
'if true, the it matches the string we are looking for
If InStr(10, fullFilePath, v) > 0 Then
'if true, then it isn't in a dir called P/previous, which I want to avoid
If InStr(1, fullFilePath, "evious") < 1 Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set f = objFSO.GetFile(fullFilePath)
'If true, then it isn't one of those funny duplicate files that microsoft makes, that has the ~ at the beginning of the file name
If f.Size > 5000 Then GoTo ReturnSettings
' if we're here then we have found the one single file that we want! Go ahead and do our thing
findFile = fullFilePath
Exit Function
End If
End If
End If
End If
End If
fileName = Dir()
Wend
For i = 0 To numFolders - 1
findFile folders(i), v
Next i
End Function
我完全是 VBA 菜鸟,所以刚刚 pva 从我可以在网上找到的东西把它粘在一起。它是否因为 findFile 循环返回一个数组而不是字符串而不起作用?我认为“退出函数”调用会解决这个问题。
请原谅递归的 if 语句 - 我正在这样做的人没有完全标准的方式来存储他们的 ppt,但这会磨练我想要的 ppt。当 sub 完成后,它本身会循环遍历所选目录的 130 个子目录,并且在每个子目录中,它将从六个不同的 ppts 中抓取各种幻灯片并将它们合并为一个,即将 780 ppts 中的数据合并为 130 -我绝对想自动化的东西!
这是我在堆栈溢出上发布的第一个问题,所以我希望我已经清楚正确地提出了它。我已经广泛搜索了这个问题的解决方案。我希望解决方案弹出给你!非常感谢。
【问题讨论】:
标签: vba powerpoint