VBA-makro: text ersetzen in word-Datei in alle Unterordner
Bin ich der Zusammenstellung ein VBA-makro, das:
1.liest einen Ordner
2.erstellt eine Auflistung aller Unterordner
3.Schleife über alle Unterordner, und finden Sie jedes word-Dokument endet .doc
4.in jedem .doc-Datei: ersetzen Sie ein wenig text ein und speichern und schließen Sie dann die Dokumente.
Dieses makro funktioniert nicht richtig: es bedeutet nicht, ersetzen Sie den text in einem beliebigen word-Dokumente in den Unterordnern.
Es spielt keine tatsächlichen öffnen Sie ein beliebiges word-Dokument, ich bin mir nicht sicher, ob Sie es öffnen sollte jedes word-doc eine nach der anderen oder, wenn es im hintergrund läuft.
Sub DoLangesNow()
Dim file
Dim path As String
Dim strFolder As String
Dim strSubFolder As String
Dim strFile As String
Dim colSubFolders As New Collection
Dim varItem As Variant
' Parent folder including trailing backslash
'YOU MUST EDIT THIS.
strFolder = "G:\2009\09771\Design\ESD\Commercial Tower KSD1\Green Star As Built\Round 1 Submission - Draft\02. Indoor Environment Quality"
' Loop through the subfolders and fill Collection object
strSubFolder = Dir(strFolder & "*", vbDirectory)
Do While Not strSubFolder = ""
Select Case strSubFolder
Case ".", ".."
' Current folder or parent folder - ignore
Case Else
' Add to collection
colSubFolders.Add Item:=strSubFolder, Key:=strSubFolder
End Select
' On to the next one
strSubFolder = Dir
Loop
' Loop through the collection
For Each varItem In colSubFolders
' Loop through word docs in subfolder
'YOU MUST EDIT THIS if you want to change the files extension
strFile = Dir(strFolder & varItem & "*.doc")
Do While strFile <> ""
Set file = Documents.Open(FileName:=strFolder & _
varItem & "\" & strFile)
' Start of macro 1replace text GS-XXXAB with GS-1624AB
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Application.WindowState = wdWindowStateNormal
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "GS-XXXAB "
.Replacement.Text = "GS-1624AB "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' End of macro 1
' Saves the file
ActiveDocument.Save
ActiveDocument.Close
' set file to next in Dir
strFile = Dir
Loop
Next varItem
End Sub
Du musst angemeldet sein, um einen Kommentar abzugeben.
Dein code war für mich mit unter änderungen
Zu
strFolder
Variable. Eg(strFolder = "C:\Users\Santosh\Desktop\tes\
")