#1
|
|||
|
|||
We have many word docs we need to copy text from
I was tasked with this and I am not sure how to start I have been googling all day I have not found a solution I am hoping you can help.
We have many word docs we need to copy text from using a search algorithm we need to search folders and sub-folders for the specified text and copy the said text from each file to a new doc files in a different folder. Search start: The information received does not support the service requested: Bla bla bla lots of text this text changes and is different in all docs from start of search to end of search this area could span multiple pages in some cases. Search end: The above actions are supported by the following: The text would need to copied to a new files using the same filenames with the addition of EN to the end. Example: orig file name: T54916811242.doc new file name: T54916811242EN.doc |
#2
|
||||
|
||||
It's not clear from your post whether the export range includes the content at each end of the search range. It's also not clear whether you want the text (as "copy the said text" and "The text would need to copied " imply) or the formatted content.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
http://www.gmayor.com/document_batch_processes.htm will open every document in a folder and optionally all its sub folders and was designed with such applications in mind.
It does however require that you have some VBA knowledge to create the custom process to apply to each document, using the basic format shown on the web site. If you know what you want to do with the documents, then it should be easy enough to adapt to your requirements.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#4
|
||||
|
||||
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 |
#5
|
|||
|
|||
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 |
#6
|
|||
|
|||
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? |
#7
|
|||
|
|||
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 |
#8
|
|||
|
|||
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 |
#9
|
||||
|
||||
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 |
#10
|
|||
|
|||
Graham
You are the man! thank you again 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 |
#11
|
|||
|
|||
Update
I have discovered that the original ExtractText function somehow inserts the last table before the start search string into the new document because I am a beginer to all this the only way for me to remove the added table or tables are to remove them using the Sub Removetables. My problem now is in order for my new Sub to run on the new document I need to set oNewDoc = Documents.Add(Template:=oDoc.FullName, Visible:=True) but when it is set to true the Batch Process Documents Add-in does not work so I must do them one at a time, If I set it to False the Batch Process Documents Add-in works with error corrupted tables as in last post but my Sub runs on the active document (the original). I am assuming this is somthing simple but at this stage of my grasp of VBA I am at a loss. Thank You Jim Code:
Sub TestENV2() ExtractTextENV2 ActiveDocument End Sub Function ExtractTextENV2(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) '<---If set to True Sub Removetables works fine 'but Batch Process Documents Add-in does not work if set to False Batch Process Documents works but Sub Removetables 'does not work 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) Call Removetables '<---new call to Sub to remove all tables in new document (by Jim) oNewDoc.SaveAs strNewName, addtorecentfiles:=False oNewDoc.Close 0 End If End With ExtractTextENV2 = True lbl_Exit: Exit Function Err_Handler: ExtractTextENV2 = False End Function 'This Sub used to remove all tables in new document Sub Removetables() Dim oTable As Table Dim oNewDoc As Document Set oNewDoc = ActiveDocument On Error Resume Next For Each oTable In ActiveDocument.Tables oTable.Delete Next oTable End Sub |
#12
|
||||
|
||||
You will have to include the table stripping in the main function. The following should work.
Code:
Function ExtractTextENV2(oDoc As Document) As Boolean Dim oRng As Range Dim oNewDoc As Document Dim strNewName As String Dim oTable As Table 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 And .Start > oDoc.Range.Start Then Set oNewDoc = Documents.Add(Template:=oDoc.FullName, Visible:=False) oNewDoc.Range.FormattedText = oRng.FormattedText For Each oTable In oNewDoc.Tables oTable.Delete Next oTable 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 ExtractTextENV2 = True lbl_Exit: Exit Function Err_Handler: ExtractTextENV2 = 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 Last edited by gmayor; 08-30-2014 at 05:34 AM. |
#13
|
||||
|
||||
Although it should not affect the processing of the last posted version, the testing threw up an issue with the reporting from the add-in that could result in a crash if the function was modified along the lines of my original response (updated below to include recent changes). I have now fixed that with an update to the add-in which you can download from my web site.
Code:
Function ExtractTextENV(oDoc As Document) As Boolean Dim oRng As Range Dim oNewDoc As Document Dim strNewName As String Dim oTable As Table 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 And .Start > oDoc.Range.Start Then Set oNewDoc = Documents.Add(Template:=oDoc.FullName, Visible:=False) oNewDoc.Range.FormattedText = oRng.FormattedText For Each oTable In oNewDoc.Tables oTable.Delete Next oTable 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 Else GoTo Err_Handler: End If End With ExtractTextENV = True lbl_Exit: Exit Function Err_Handler: ExtractTextENV = 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 |
#14
|
|||
|
|||
Graham
Sorry for the delay I have used the new code and now I am getting error the Run-time error (6015: A table in the document has become corrupted) when I run it as a macro, at code Code:
oTable.Delete Code:
On Error Resume Next Code:
Each oTable In oNewDoc.Tables I am attaching a sanitized doc because there is personnel information in the originals I hope you don't mind I run into the same issues on the sanitized doc as well by the way the bookmarks are not in all docs so I can't use this as search string. Also I can't run your Word_Batch Process Add-in V2.2 it gives compile error (Compile error in hidden module: ModMain. This error commonly occurs when code is incompatible with the version, platform, or architecture of this application.) I get this in word 2013 and 2010 did I install it incorrectly? I replaced the last one with the new one in the word startup folder as you state in the instructions also used the installer no difference. Again thank you |
#15
|
||||
|
||||
Apologies for the error - a function was inadvertently deleted whilst updating my master copy The version on my web site has now been updated (copy attached here).
I will checkout your attachment and report back later.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
Similar Threads | ||||
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 |
copy from word into a formatting text box | mikewooten | Word | 1 | 06-15-2010 02:04 AM |
Is it possible to create 'balloon' text in docs | bubbleboi | Word | 3 | 11-13-2009 01:19 AM |