![]() |
|
#1
|
||||
|
||||
![]()
Having re-read the question, I think there is enough information to suggest a function. Open a document with the required text and test with the following to see if it gives you the results you want:
Code:
Sub Test() ExtractText ActiveDocument End Sub Try it on a small group of three or four files first. The function puts the files in the same folder as the documents, but you can change the path if you wish. Code:
Function ExtractText(oDoc As Document) As Boolean Dim oRng As Range Dim oNewRange As Range Dim oNewDoc As Document Dim strNewName As String Const strStart As String = "The information received does not support the service requested:" Const strEnd As String = "The above actions are supported by the following:" On Error GoTo Err_Handler Set oRng = oDoc.Range With oRng .Start = .Start + InStr(oRng, strStart) - 1 .End = .Start + InStr(oRng, strEnd) - 1 If Len(.Text) > 0 Then Set oNewDoc = Documents.Add(Template:=oDoc.FullName, Visible:=False) oNewDoc.Range.FormattedText = oRng.FormattedText Set oNewRange = oNewDoc.Range oNewRange.Collapse wdCollapseStart oNewRange.MoveEndUntil ":" oNewRange.End = oNewRange.End + 2 oNewRange.Delete strNewName = oDoc.FullName strNewName = Left(strNewName, InStrRev(strNewName, Chr(46)) - 1) strNewName = strNewName & "EN" strNewName = strNewName & Right(oDoc.name, Len(oDoc.name) - InStrRev(oDoc.name, Chr(46)) + 1) oNewDoc.SaveAs strNewName, addtorecentfiles:=False oNewDoc.Close 0 End If End With ExtractText = True lbl_Exit: Exit Function Err_Handler: ExtractText = False End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#2
|
|||
|
|||
![]()
Paul and Graham first I would like to thank you for helping me
Paul to answer your questions I would like to include the continent at each end of the search range. If possible I would like to keep the formatting within the search range including the search strings or be able to place the text into a pre-formatted area of the new docs Graham I will give your recommendation a shot and report back Thank again Jim |
#3
|
|||
|
|||
![]()
Graham
Thank you I almost have it going your function looks like it extracts the data. My hurdle to overcome is the function will not auto save the extracted source it creates a new Document1 that is hidden I can only see it being created because I am in the VBA screen, when I log-off the computer word will ask me if I want to save the Document1 it will allow me save the file and the file contains the extracted data as needed less the search strings. I have used the script on Word 2013, 2010 with same result when I debug (step-into) it will make many new Documents. I do not get any errors during the macro run do you have any ideas? |
#4
|
|||
|
|||
![]()
Update
I temporly set Code:
Set oNewDoc = Documents.Add(Template:=oDoc.FullName, Visible:=True) When I step into the code and run the Code:
oNewRange.Delete Code:
lbl_Exit: Exit Function Code:
oNewRange.Delete Thanks Jim |
#5
|
|||
|
|||
![]()
I hit another snag
It looks like all the copied data is good until the end on all the files the last nine characters are missing. It happens when I run Code:
oNewDoc.Range.FormattedText = oRng.FormattedText At this stage I am still skipping Code:
oNewRange.Delete |
#6
|
||||
|
||||
![]()
If you want to keep the start and end search strings, you don't need any of the oNewRange lines. The reason for the skipping is presumably because you are testing with an unsaved document, so the naming section errors out and when that occurs it moves to the label. If that is not it, comment out the On Error line (while testing with the test macro) and see which line causes the error. It works here when tested in Word 2010 and 2013.
Code:
Function ExtractText(oDoc As Document) As Boolean Dim oRng As Range Dim oNewDoc As Document Dim strNewName As String Const strStart As String = "The information received does not support the service requested:" Const strEnd As String = "The above actions are supported by the following:" On Error GoTo Err_Handler Set oRng = oDoc.Range With oRng .Start = .Start + InStr(oRng, strStart) - 1 .End = .Start + InStr(oRng, strEnd) + 1 .MoveEndUntil Chr(58) .End = .End + 1 If Len(.Text) > 0 Then Set oNewDoc = Documents.Add(Template:=oDoc.FullName, Visible:=True) oNewDoc.Range.FormattedText = oRng.FormattedText strNewName = oDoc.FullName strNewName = Left(strNewName, InStrRev(strNewName, Chr(46)) - 1) strNewName = strNewName & "EN" strNewName = strNewName & Right(oDoc.name, Len(oDoc.name) - InStrRev(oDoc.name, Chr(46)) + 1) oNewDoc.SaveAs strNewName, addtorecentfiles:=False oNewDoc.Close 0 End If End With ExtractText = True lbl_Exit: Exit Function Err_Handler: ExtractText = False End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#7
|
|||
|
|||
![]()
Graham
You are the man! ![]() I still have one problem all is well until I get to the end at Code:
oNewDoc.SaveAs strNewName, addtorecentfiles:=False Run-time error 6015: A table in the document has become corrupted. There are tables in the doc and they are not needed in the new doc they are before and after search strings the result is the new document or documents will not close, if I keep going in the code all looks good in the new doc except for a empty table that could go away it is not needed. I am looking for solutions on-line as well Again thanks Jim |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Copy Text Twice to Paste into word | Albundy | Word | 2 | 09-02-2016 12:59 PM |
Text Shifting When Saving Word 2007 Docs | DerekS | Word | 1 | 09-21-2013 02:42 AM |
Can't copy/cut and paste text in Word 2002 | mmiller751 | Word | 0 | 04-17-2012 02:55 PM |
![]() |
mikewooten | Word | 1 | 06-15-2010 02:04 AM |
![]() |
bubbleboi | Word | 3 | 11-13-2009 01:19 AM |