2016-09-29 88 views
0

下面我有一些代碼找到並替換某個單詞。但是,我對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 
+0

請注意你的代碼將打破所有文本框的格式,除非它們在字體,粗體等方面沒有內部差異。 – Jbjstam

回答

0

這不是你問真正清楚耐磨,但如果你通過一些文件要循環下面的代碼將有助於:

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