View Full Version : Find/Replace After Merge
liquidace
03-11-2013, 03:06 AM
Hi
I'm wondering if you could help me please?
Background info:
We have a third party system which uses word to create documents. New documents can be created and keywords are inserted as well as calculations to help control the document . One of the problems we have is one of the calculations creates an additional paragraph marker (this has been noted to the third party system to fix but may take a while).
Problem:
I would like to create vba script when the document is being merged to remove the additional paragraph marker (^p). I have created a sub were the user must press a button to delete additional paragraph markers but this is increasing the end user time to process as user has to go into the word document to press the button. If there a way to place this "find and replace" vba into a document so it can be automatically run after the merge that requires to no end user involvement? I have tested to see if vba script can be run so I put a standard greeting message into a Document New() sub and this worked.
Current VBA:
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Hope this helps?
Thanks
Graeme
macropod
03-11-2013, 04:34 AM
Hi Graeme,
Getting that to work with your third party system would require adding the Document New() sub to the template from which the letters are created and not having the Document New() sub execute until after your third party system has finished updating the new document.
An alternative, pending a code fix, is to simply delete the existing paragraph break from the template. Then, when the third party system adds the unwanted one, you'll actually end up with the required number.
liquidace
03-11-2013, 08:35 AM
Hi Paul
Thanks for the reply. I was hoping if there was a way of getting the find and replace to fire after the merge in the document_new() sub?
With regards the deleting the paragraph marker I have tried that but in some of documents these calculations are nested within each other and does not work.
Thanks
Graeme
macropod
03-11-2013, 01:30 PM
Did you try putting your Find/Replace code into a Document New() sub in the template from which the letters are created?
liquidace
03-12-2013, 03:45 AM
I tried that but I think it fires off before it does the merge as the document has not changed. Is there way detecting when the merge has been run in a document new () sub?
macropod
03-12-2013, 03:56 AM
I tried that but I think it fires off before it does the merge as the document has not changed.
I suspected that might be the case but, not having a sutiable document-creating app, I'm not in a position to test it.
Is there way detecting when the merge has been run in a document new () sub?
Not really because, if the Document_New macro runs before the other code populates the document, all you would achieve by adding code to delay its execution would be to delay the document's population. Depending on what you're doing next with the document, though, you might implement an even-handler to drive a DocumentBeforePrint and/or DocumentBeforeSave macro to run the code. For details, see:
http://msdn.microsoft.com/en-us/library/office/aa140279(v=office.10).aspx
liquidace
03-18-2013, 02:26 AM
Sorry for not replying back until now, I have tested the "document before save" vba and got the example working. Unfortunately it seems to ignore the find and replave vba above. Is there another way of doing a find and replace that might work?
Thanks
macropod
03-18-2013, 04:10 AM
Without seeing your implementation, I can't really say why it might not work.
Have you confirmed that the DocumentBeforeSave macro is actually executing?
liquidace
03-18-2013, 04:38 AM
With the link that you posted I built the macro that captures the event handler into my template. This is fired in the document_new sub and when it saves it comes up with the greeting message and then it should run through the find and replace. Please see below:
Private Sub Document_New()
CreateEventHandler
End Sub
Module called modHandleEvents
' declare type of object to be stored in the objEventHandler variable
Dim objEventHandler As clsEventHandler
Sub CreateEventHandler()
'create a new event handler object and assign it to objEventHandler
Set objEventHandler = New clsEventHandler
'Notify the current instance of Word to check for
'application event procedures
'inside the event handler object
Set objEventHandler.AppThatLooksInsideThisEventHandler = Word.Application
End Sub
Sub DestroyEventHandler()
'release the memory being used by the event handler object
Set objEventHandler = Nothing
End Sub
Sub AutoExit()
'whenever this template is unloaded,
'automatically destroy the event handler
DestroyEventHandler
End Sub
Class Module named clsEventModule:
'Declare that only an instance of Word can be assigned to this variable
'and also that the assigned instance should be notified to
'check inside the event handler for application event procedures.
Public WithEvents _
AppThatLooksInsideThisEventHandler _
As Word.Application
Private Sub AppThatLooksInsideThisEventHandler_DocumentBeforeSave(ByVal Doc As Word.Document, SaveAsUI As Boolean, Cancel As Boolean)
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p^p"
.Replacement.Text = "^p"
.Forward = False
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
MsgBox "Event: DocumentBeforeSave"
End Sub
End Sub
Hope this helps?
Thanks
macropod
03-19-2013, 04:19 PM
Try changing your 'DocumentBeforeSave' sub to:
Private Sub AppThatLooksInsideThisEventHandler_DocumentBeforeSave(ByVal Doc As Word.Document, SaveAsUI As Boolean, Cancel As Boolean)
With Doc.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[^13]{2,}"
.Replacement.Text = "^p"
.Forward = False
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
MsgBox "Event: DocumentBeforeSave"
End Sub
PS: When posting code, please use the VBA code tags. They're on the toolbar.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.