用于打开和编辑多个 Powerpoint 文件的 VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7550840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA for opening and editing multiple Powerpoint files
提问by Kang Kyu Choi
I have a folder with over 200 Powerpoint files and I have been have been struggling with a Macro that opens each of these files, edits them, saves them and closes them in a loop.
I have managed to create code for the editing part, however I can't manage to create a code that picks each of the files in the folder. Using "*.pptx"
doesn't seem to work and writing code with a specific filename for each of these files is very inefficient.
我有一个包含 200 多个 Powerpoint 文件的文件夹,我一直在努力使用一个宏来打开每个文件,编辑它们,保存它们并循环关闭它们。我设法为编辑部分创建了代码,但是我无法创建一个代码来选择文件夹中的每个文件。使用"*.pptx"
似乎不起作用,并且为这些文件中的每一个编写具有特定文件名的代码非常低效。
Does anyone have a solution to this?
有没有人有解决方案?
Sub SaveNotesText()
Dim oPres As Presentation
Dim oSlides As Slides
Dim oSlide As Slide
Dim oShapes As Shapes
Dim oSh As Shape
Dim NotesText As String
Dim FileNum As Integer
Dim PathSep As String
#If Mac Then
PathSep = ":"
#Else
PathSep = "\"
#End If
Set oPres = ActivePresentation
Set oSlides = oPres.Slides
For Each oSlide In oSlides
NotesText = NotesText & "Slide " & oSlide.SlideIndex & vbCrLf
Set oShapes = oSlide.NotesPage.Shapes
For Each oSh In oShapes
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
NotesText = NotesText & oSh.TextFrame.TextRange.Text
End If
End If
Next oSh
NotesText = NotesText & vbCrLf
Next oSlide
FileNum = FreeFile
Open oPres.Path & PathSep & "NotesText.TXT" For Output As FileNum
Print #FileNum, NotesText
Close FileNum
End Sub
回答by brettdj
You can use Dir to loop through all the "#.ppt#" files in a folder, ie
您可以使用 Dir 循环遍历文件夹中的所有“#.ppt#”文件,即
Public Sub DoFiles()
Dim strFileName As String
Dim strFolderName As String
Dim PP As Presentation
'set default directory here if needed
strFolderName = "C:\temp"
strFileName = Dir(strFolderName & "\*.ppt*")
Do While Len(strFileName) > 0
Set PP = Presentations.Open(strFolderName & "\" & strFileName)
'your code
PP.Close
strFileName = Dir
Loop
End Sub