Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-16-2014, 05:06 AM
QA_Compliance_Advisor QA_Compliance_Advisor is offline VBA Batch Find & Replace for all MSOffice extensions, to replace File Name and Content of the File Windows 7 32bit VBA Batch Find & Replace for all MSOffice extensions, to replace File Name and Content of the File Office 2007
Advanced Beginner
VBA Batch Find & Replace for all MSOffice extensions, to replace File Name and Content of the File
 
Join Date: Jul 2014
Posts: 44
QA_Compliance_Advisor is on a distinguished road
Default VBA Batch Find & Replace for all MSOffice extensions, to replace File Name and Content of the File

while I have read number of threads in varies forums, I am not too sure how to proceed with my issues. My department is going to be asked to replace 1000 + documents in Word, Excel, PowerPoint and Visio 2007 the following;


(1) Old Job Titles with New Job Titles ( 40+ with varies spellings)
(2) replace number of our documents, which
(2a) require to change the title of the file name,
(2b) require to change all document which may refer to that particular file name.

However, also another issue is not all files are in one folder, there is some folders which have subfolder, after subfolder (we control documents and history of each revision, obviously historic documents will not wont to be changed);
(1) I will need to change files within subfolders also.

I do plan to have all the files that need to changed in on location which is separate from any historic files.

the idea I would like is to automate this process with little manual/ human interference as possible.

does anyone have a solution how to do this, either creating a VB application to open and replace the old_text with the new_text and old_doc_number replaced with new_doc_number in a doc and save it with the new_doc_number + doc_title.
Reply With Quote
  #2  
Old 07-18-2014, 05:24 AM
gmaxey gmaxey is offline VBA Batch Find & Replace for all MSOffice extensions, to replace File Name and Content of the File Windows 7 32bit VBA Batch Find & Replace for all MSOffice extensions, to replace File Name and Content of the File Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

The forum is full of examples of batch processing files. For the dropdown piece, it would probably be easier to just clear the existing list and fill it with a new list. The following code assumes a content control named "Job Title" and a formfield bookmarked "JobTitle" the contents of each are cleared and filled with a new list.

Just pass your document to process to the macro.

HTML Code:
Sub RefillDD(oDoc As Word.Document)
'A basic Word macro coded by Greg Maxey
Dim arrLEs() As String
Dim lngIndex As Long
Dim lngPT As Long
Dim bProt As Boolean
  lngPT = oDoc.ProtectionType
  If lngPT <> wdNoProtection Then
    bProt = True
    oDoc.Unprotect
  End If
  On Error Resume Next
  arrLEs = Split("X,Y,Z", ",")
  With oDoc.SelectContentControlsByTitle("Job Title").Item(1)
    For lngIndex = .DropdownListEntries.Count To 1 Step -1
      .DropdownListEntries(lngIndex).Delete
    Next lngIndex
    For lngIndex = 0 To UBound(arrLEs)
      .DropdownListEntries.Add arrLEs(lngIndex), arrLEs(lngIndex)
    Next lngIndex
  End With
  With oDoc.FormFields("JobTitle")
    .DropDown.ListEntries.Clear
    For lngIndex = 0 To UBound(arrLEs)
      .DropDown.ListEntries.Add arrLEs(lngIndex)
    Next lngIndex
  End With
  If bProt = True Then
    Select Case lngPT
     Case 2
       oDoc.Protect 2, True
     Case Else
       oDoc.Protect lngPT
    End Select
  End If
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 07-18-2014, 06:04 AM
gmaxey gmaxey is offline VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Windows 7 32bit VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

This may help: http://gregmaxey.com/word_tip_pages/...der_addin.html
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #4  
Old 07-18-2014, 03:56 PM
macropod's Avatar
macropod macropod is offline VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Windows 7 32bit VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

QA_Compliance_Advisor: Please don't post the same question multiple times - once is enough and avoids confusion. I've merged your two threads.

Update: a 3rd thread found and merged ...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 07-30-2014, 12:22 AM
QA_Compliance_Advisor QA_Compliance_Advisor is offline VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Windows 7 32bit VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Office 2007
Advanced Beginner
VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File
 
Join Date: Jul 2014
Posts: 44
QA_Compliance_Advisor is on a distinguished road
Default

@Macropod, thanks - sorry wasn't too sure were to put it.

when doing a find and replace - is there a solution for the following
(1) can I do the same with text in headers & footers replace text?
(2) can I replace pictures/ graphic in a header?
Reply With Quote
  #6  
Old 08-14-2014, 10:32 PM
macropod's Avatar
macropod macropod is offline VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Windows 7 32bit VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by QA_Compliance_Advisor View Post
when doing a find and replace - is there a solution for the following
(1) can I do the same with text in headers & footers replace text?
(2) can I replace pictures/ graphic in a header?
there are plenty of code samples on this board for doing F/R in headers & footers. See, for example: https://www.msofficeforums.com/word-...html#post63175

As for replacing a picture/graphic, that can't be done with Find/Replace - you'd need to work with the Shapes, ShapeRange or InlineShapes collection. And, if there are multiple pictures/graphics in the range of interest, you have to include the logic for how the one of interest is to be identified.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 09-11-2014, 02:24 AM
QA_Compliance_Advisor QA_Compliance_Advisor is offline VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Windows 7 32bit VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Office 2007
Advanced Beginner
VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File
 
Join Date: Jul 2014
Posts: 44
QA_Compliance_Advisor is on a distinguished road
Default

@ Macropod, thank you for the advice and direction for the headers and footers.
Reply With Quote
  #8  
Old 09-11-2014, 02:28 AM
QA_Compliance_Advisor QA_Compliance_Advisor is offline VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Windows 7 32bit VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Office 2007
Advanced Beginner
VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File
 
Join Date: Jul 2014
Posts: 44
QA_Compliance_Advisor is on a distinguished road
Default VBA Code for Find and Replace, Body, Header and Footer from and Excel doc

an error is occurring, the Macro will not open and edit the Headers or Footers, but it does the body - could someone review and see if i have the correct code for editing Headers and Footers (in pink)


Private Sub FindAndReplace_docx(oFolder As String)
Application.ScreenUpdating = True
Dim strFolder As String, strFile As String, wdDoc As Document
Dim xlApp As Object, xlWkBk As Object, StrWkBkNm As String, StrWkSht As String
Dim bStrt As Boolean, iDataRow As Long, bFound As Boolean
Dim xlFList As String, xlRList As String, i As Long, Rslt
StrWkBkNm = "\\it17.local\root\UserData\munwil\My Documents\My Documents\Projects\IMS Structure Change\Find and Replace.xlsx"
StrWkSht = "Sheet1"
If Dir(StrWkBkNm) = "" Then
MsgBox "Cannot find the designated workbook: " & StrWkBkNm, vbExclamation
Exit Sub
End If
'Get the folder to process
strFolder = oFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
' Test whether Excel is already running.
On Error Resume Next
bStrt = False ' Flag to record if we start Excel, so we can close it later.
Set xlApp = GetObject(, "Excel.Application")
'Start Excel if it isn't running
If xlApp Is Nothing Then
Set xlApp = CreateObject("Excel.Application")
If xlApp Is Nothing Then
MsgBox "Can't start Excel.", vbExclamation
Exit Sub
End If
' Record that we've started Excel.
bStrt = True
End If
On Error GoTo 0
'Check if the workbook is open.
bFound = False
With xlApp
'Hide our Excel session
If bStrt = True Then .Visible = False
For Each xlWkBk In .Workbooks
If xlWkBk.FullName = StrWkBkNm Then ' It's open
Set xlWkBk = xlWkBk
bFound = True
Exit For
End If
Next
' If not open by the current user.
If bFound = False Then
' Check if another user has it open.
If IsFileLocked(StrWkBkNm) = True Then
' Report and exit if true
MsgBox "The Excel workbook is in use." & vbCr & "Please try again later.", vbExclamation, "File in use"
If bStrt = True Then .Quit
Exit Sub
End If
' The file is available, so open it.
Set xlWkBk = .Workbooks.Open(FileName:=StrWkBkNm)
If xlWkBk Is Nothing Then
MsgBox "Cannot open:" & vbCr & StrWkBkNm, vbExclamation
If bStrt = True Then .Quit
Exit Sub
End If
End If
' Process the workbook.
With xlWkBk.Worksheets(StrWkSht)
' Find the last-used row in column A.
' Add 1 to get the next row for data-entry.
iDataRow = .Cells(.Rows.Count, 1).End(-4162).Row ' -4162 = xlUp
' Output the captured data.
For i = 1 To iDataRow
' Skip over empty fields to preserve the underlying cell contents.
If Trim(.Range("A" & i)) <> vbNullString Then
xlFList = xlFList & "|" & Trim(.Range("A" & i))
xlRList = xlRList & "|" & Trim(.Range("B" & i))
End If
Next
End With
If bFound = False Then xlWkBk.Close False
If bStrt = True Then .Quit
End With
' Release Excel object memory
Set xlWkBk = Nothing: Set xlApp = Nothing
'Process each document in the folder
While strFile <> ""
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
'Process each word from the F/R List
For i = 1 To UBound(Split(xlFList, "|"))
With wdDoc.Range
Options.DefaultHighlightColorIndex = wdPink
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.MatchWholeWord = True
.MatchCase = True
.Wrap = wdFindStop
.Text = Split(xlFList, "|")(i)
.Replacement.Text = Split(xlRList, "|")(i)
.Execute Replace:=wdReplaceAll
End With
End With
Next
'process Headers with Find and Replace Function
For Each Sctn In wdDoc.Sections
For Each HdFt In Sctn.Headers
With HdFt
If .LinkToPrevious = False Then
'Process the header
With .Range.Find
'Find and Replace parameters for Headers
For i = 1 To UBound(Split(xlFList, "|"))
With wdDoc.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.MatchWholeWord = True
.MatchCase = True
.Wrap = wdFindStop
.Text = Split(xlFList, "|")(i)
.Replacement.Text = Split(xlRList, "|")(i)
.Execute Replace:=wdReplaceAll
End With
End With
Next
End With
For Each Shp In wdDoc.Shapes
With Shp.TextFrame
If .HasText Then
With .TextRange.Find
'Process each word from the F/R List within Headers which has Shapes
For i = 1 To UBound(Split(xlFList, "|"))
With wdDoc.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.MatchWholeWord = True
.MatchCase = True
.Wrap = wdFindStop
.Text = Split(xlFList, "|")(i)
.Replacement.Text = Split(xlRList, "|")(i)
.Execute Replace:=wdReplaceAll
End With
End With
Next
End With
End If
End With
Next
End If
End With
Next
Next
'Process Footers with Find and Replace Function
For Each Sctn In wdDoc.Sections
For Each HdFt In Sctn.Headers
With HdFt
If .LinkToPrevious = False Then
With .Range.Find
'Find and Replace parameters for Footers
For i = 1 To UBound(Split(xlFList, "|"))
With wdDoc.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.MatchWholeWord = True
.MatchCase = True
.Wrap = wdFindStop
.Text = Split(xlFList, "|")(i)
.Replacement.Text = Split(xlRList, "|")(i)
.Execute Replace:=wdReplaceAll
End With
End With
Next
End With
For Each Shp In wdDoc.Shapes
With Shp.TextFrame
If .HasText Then
With .TextRange.Find
'Process each word from the F/R List within Footers which has Shapes
For i = 1 To UBound(Split(xlFList, "|"))
With wdDoc.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.MatchWholeWord = True
.MatchCase = True
.Wrap = wdFindStop
.Text = Split(xlFList, "|")(i)
.Replacement.Text = Split(xlRList, "|")(i)
.Execute Replace:=wdReplaceAll
End With
End With
Next
End With
End If
End With
Next
End If
End With
Next
Next
'Close the document
wdDoc.Close SaveChanges:=True
'Get the next document
strFile = Dir()
Wend
Application.ScreenUpdating = True
End Sub

Last edited by QA_Compliance_Advisor; 09-11-2014 at 07:56 AM. Reason: Incorrect Error
Reply With Quote
  #9  
Old 09-11-2014, 03:41 AM
QA_Compliance_Advisor QA_Compliance_Advisor is offline VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Windows 7 32bit VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Office 2010 32bit
Advanced Beginner
VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File
 
Join Date: Jul 2014
Posts: 44
QA_Compliance_Advisor is on a distinguished road
Default Replace entry list in dropdown

Quote:
Originally Posted by gmaxey View Post
The forum is full of examples of batch processing files. For the dropdown piece, it would probably be easier to just clear the existing list and fill it with a new list. The following code assumes a content control named "Job Title" and a formfield bookmarked "JobTitle" the contents of each are cleared and filled with a new list.

Just pass your document to process to the macro.

HTML Code:
Sub RefillDD(oDoc As Word.Document)
...
 End Sub
tried this code however, i am getting a 'runtime error 424' and highlighting
lngPT = oDoc.ProtectionType.

can anyone assist.
Reply With Quote
  #10  
Old 09-11-2014, 05:35 AM
gmaxey gmaxey is offline VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Windows 7 32bit VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code runs without error here so I have no idea why you are getting an error. What is the error description?
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #11  
Old 09-11-2014, 07:51 AM
QA_Compliance_Advisor QA_Compliance_Advisor is offline VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Windows 7 32bit VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Office 2010 32bit
Advanced Beginner
VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File
 
Join Date: Jul 2014
Posts: 44
QA_Compliance_Advisor is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
Code runs without error here so I have no idea why you are getting an error. What is the error description?

'runtime error 424'
Reply With Quote
  #12  
Old 09-11-2014, 11:51 PM
QA_Compliance_Advisor QA_Compliance_Advisor is offline VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Windows 7 32bit VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Office 2010 32bit
Advanced Beginner
VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File
 
Join Date: Jul 2014
Posts: 44
QA_Compliance_Advisor is on a distinguished road
Default

Guys thanks for the Help the main issue which was to do find and replace has been solved.
Reply With Quote
Reply

Tags
drop down lists, find & replace, vba, vba find and replace, vba script

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and replace/insert HTML code into Master File using tags dave8555 Excel 2 02-23-2014 03:51 PM
VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File How to replace this word file using wildcard zhangzujin361 Word 1 01-18-2014 08:02 PM
VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM
VBA Batch Find &amp; Replace for all MSOffice extensions, to replace File Name and Content of the File Help with find and replace or query and replace shabbaranks Excel 4 03-19-2011 08:38 AM
MS word taking over file extensions jakes Word 0 10-22-2010 01:35 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:19 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft