PDA

View Full Version : Replacement of 1.text string with 2.text string-2.text string ends with character "("



Cubajz
05-22-2015, 09:15 AM
Hi, I need to make a macro for Word to make my work easier...I tryied a bit of work on my own but it seems itīs not working properly..Thank you for any help I am really a beginner.

Sub Makro1()
Selection.Copy
Windows("Dokument2.docx").Activate
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = Selection
End With
Selection.Find.Execute
Selection.MoveEndUntil (Cset = "(")
Selection.Copy
Windows("Dokument2.docx").Activate
Selection.PasteAndFormat (wdFormatOriginalFormatting)
End Sub




I attached two files as little example. Dokument1 technical norms are out-of-date, Dokument2 technical norms are up-to-date. I want those out-of-date norms to be replaced with up-to-date norms. :) I highlighted in text in both documents first and last technical norm - just to give you example of what I mean by technical norm. Thanks in advance.

1347213473

gmayor
05-22-2015, 11:28 PM
The first issue to address is that the selected reference in document to be edited does not appear in the table, so it is never going to work. The problem is that there are assorted non-breaking spaces in both documents that interfere with the string matching. The suggested code addresses that issue and makes changes to both documents before string matching.

Put the path of 'Dokument2.docx' in the line

Set oSource = Documents.Open(Filename:="C:\Path\Dokument2.docx", _
AddToRecentFiles:=False, Visible:=False)

It doesn't matter whether that document is open or not when running the macro. Select the reference to be updated and run the macro. If the reference is found it will be updated according to the criteria you posted. If not found, you will see a warning message. Dokument2 is then closed without saving the changes incurred by the macro. The macro doesn't save or close 'Dokument1'



Sub Macro1()
'Macro by G Mayor 23 May 2015
Dim oDoc As Document
Dim oSource As Document
Dim oRng As Range
Dim oFind As Range
Dim strReference As String
Dim bFound As Boolean
Set oDoc = ActiveDocument
Set oRng = Selection.Range
oRng.Text = Replace(oRng.Text, Chr(160), Chr(32))
strReference = oRng.Text
If Len(oRng) < 6 Then
MsgBox "Select the reference first!", vbCritical
GoTo lbl_Exit
End If
Set oSource = Documents.Open(Filename:="C:\Path\Dokument2.docx", _
AddToRecentFiles:=False, Visible:=False)
Set oFind = oSource.Tables(1).Range
oFind.Text = Replace(oFind.Text, Chr(160), Chr(32))
With oFind.Find
Do While .Execute(FindText:=strReference)
bFound = True
oFind.MoveEndUntil "("
oFind.End = oFind.End - 1
oRng.Text = oFind.Text
Exit Do
Loop
End With
If Not bFound Then
MsgBox "The selected reference was not found in the reference table.", vbInformation
End If
oSource.Close 0
lbl_Exit:
Set oDoc = Nothing
Set oSource = Nothing
Set oRng = Nothing
Set oFind = Nothing
Exit Sub
End Sub

Cubajz
05-26-2015, 01:11 AM
Hi it seems to work perfectly with the example I provided, thank you for that! But somehow on larger document it is always showing msBox "The selected reference was not found in the reference table." I attached two new documents (second one is bigger-16pages) and I highlited on the first page of both documents the norm which is not working as an example. I am normaly working with dokuments with 300 and more pages, so this is problem for me :) But thanks again for your work, it is good progress. :)

1351213513

gmayor
05-26-2015, 01:26 AM
The original reference document had one table. This one has 2 and the macro only addresses the first. If your reference document is going to have more than one table, you need to modify the macro as follows:


Sub Macro1()
'Macro by G Mayor 23 May 2015
'Modified 26 May 2015
Dim oDoc As Document
Dim oTable As Table
Dim oSource As Document
Dim oRng As Range
Dim oFind As Range
Dim strReference As String
Dim bFound As Boolean
Set oDoc = ActiveDocument
Set oRng = Selection.Range
oRng.Text = Replace(oRng.Text, Chr(160), Chr(32))
strReference = oRng.Text
If Len(oRng) < 6 Then
MsgBox "Select the reference first!", vbCritical
GoTo lbl_Exit
End If
Set oSource = Documents.Open(Filename:="C:\Path\Dokument2.docx", _
AddToRecentFiles:=False, Visible:=False)
For Each oTable In oSource.Tables
Set oFind = oTable.Range
oFind.Text = Replace(oFind.Text, Chr(160), Chr(32))
With oFind.Find
Do While .Execute(FindText:=strReference)
bFound = True
oFind.MoveEndUntil "("
oFind.End = oFind.End - 1
oRng.Text = oFind.Text
Exit Do
Loop
End With
Next oTable
If Not bFound Then
MsgBox "The selected reference was not found in the reference table.", vbInformation
End If
oSource.Close 0
lbl_Exit:
Set oDoc = Nothing
Set oSource = Nothing
Set oRng = Nothing
Set oFind = Nothing
Exit Sub
End Sub

Cubajz
06-09-2015, 06:19 AM
This is obviously tremendous job already! Thank you for your work and sry for late answer I was on vacation with my family :) This macro works well, but it takes a bit long time processing that one selection...that made me think: Can you think of any way how to make this type of work even more automatic? (meaninig without need of selecting the text first)? Its not very time effective when it takes about 9-15 seconds to wait the results for one technical norm (I have hundreds in one document, and many many documents). I have strong computer so I think this could be ok...

Is there any way to make a skript which would automaticly find specific text string and then replace it with "almost" similar equivalent found in my Catalog? I personaly think the character "/" and string ":[YEAR]" could be the key to programming some "search engine" for this specific issue and solving the whole problem in a way, that I could let my computer do the work for few minutes/hours and do other important thing in the meantime ...

The argument is this: Because technical norms always ends with year (for example ČSN EN ISO 12100:2011) , there is always something like this ":2011" at the end of string
Character "/" is the one which either is or is not immidiately after this ":2011" - that is how the script will now if:
- the norm is the same (meaning there is not character "/" after ":2011" ) - norm is up to date - job done
- the norm is different (meaning there is character "/" after ":2011" ) - must continue the selection until there is character "/" immidiately after ":2011" and replace the old norm in document.


I think it is quite confusing how I wrote it :rofl: But if you just simply think about importance of character "/" and ":[YEAR]" for a second you can than understand the logic as well I think...I am able to understand this logic (I can write it to the details if you would like to) but I am absolutely unable to do the scripting, so again, I am asking for your help. Thanks again for any answer, you already suprised me with the first macro! Very impressive job. :)

gmayor
06-11-2015, 07:08 AM
An example is worth a thousand words. Whether what you ask is possible it is difficult to say. My eyes glazed over when I saw your specification:banghead:. Can you post a sample that has the various strings that need processing highlighted in one colour and the ones that don't require processing in another?

Cubajz
06-16-2015, 02:26 AM
Sry:rofl:...here are some examples:

Norm Doesnt Require processing x Requires processing


1. example
Document 1(Work text):
ČSN EN 15794:2010
Document 2(Catalog)
ČSN EN 15794:2010

2. example
Document 1(Work text):
ČSN EN 12493+A1:2014
Document 2(Catalog):
ČSN EN 12493+A1:2014

3. example
Document 1(Work text):
ČSN EN ISO 13849-1:2008/Opr. 1:2009/Opr. 2:2014/Opr. 3:2015
Document 2(Catalog):
ČSN EN ISO 13849-1:2008/Opr. 1:2009/Opr. 2:2014/Opr. 3:2015

4. example
Document 1(Work text):
ČSN EN 60079-10-1:2009
Document 2(Catalog):
ČSN EN 60079-10-1:2009/Opr. 1:2011

5. example
Document 1(Work text):
ČSN EN 60079-17 ed. 3:2008
Document 2(Catalog):
ČSN EN 60079-17 ed. 3:2008/Opr. 1:2009/Z1:2014

6. example
Document 1(Work text):
ČSN EN 50545-1:2012
Document 2(Catalog):
- norm is not existing in Catalog (process=> make text of the norm RED in Document 1(Work text).