通过多个文件循环VBA
问题描述:
下面我有一些代码找到并替换某个单词。但是,我对VBA的了解有限,所以我不知道如何通过文件夹中的多个Powerpoint文件来循环此代码并保存它们。另外它只需要在第一张纸上写下文字,我不知道这是怎么回事?通过多个文件循环VBA
Sub DemoFindReplace()
Dim sld As Slide
Set sld = ActivePresentation.Slides(1)
Dim shp As Shape
For Each shp In sld.Shapes
If shp.HasTextFrame Then
If shp.TextFrame.HasText Then
shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "TEST", "REPLACE")
End If
End If
Next shp
End Sub
答
这不是你问真正清楚耐磨,但如果你通过一些文件要循环下面的代码将有助于:
Dim MyFile, MyPath, MyName As String
' Returns "WIN.INI" if it exists.
MyFile = Dir("C:\WINDOWS\WIN.INI")
' Returns filename with specified extension. If more than one *.INI
' file exists, the first file found is returned.
MyFile = Dir("C:\WINDOWS\*.INI")
' Call Dir again without arguments to return the next *.INI file in the
' same directory.
MyFile = Dir()
' Return first *.TXT file, including files with a set hidden attribute.
MyFile = Dir("*.TXT", vbHidden)
' Display the names in C:\ that represent directories.
MyPath = "c:\" ' Set the path.
MyName = Dir(MyPath, vbDirectory) ' Retrieve the first entry.
Do While MyName <> "" ' Start the loop.
' Use bitwise comparison to make sure MyName is a directory.
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
' Display entry only if it's a directory.
MsgBox(MyName)
End If
MyName = Dir() ' Get next entry.
Loop
来源:https://msdn.microsoft.com/en-us/library/dk008ty4(v=vs.90).aspx
请注意你的代码将打破所有文本框的格式,除非它们在字体,粗体等方面没有内部差异。 – Jbjstam