![]() |
|
#1
|
|||
|
|||
![]()
Vivka,
Worked like a charm. Thank you. Cheers Onjai |
#2
|
|||
|
|||
![]()
Onjai, my pleasure!
|
#3
|
|||
|
|||
![]()
Trying to loop through multiple docs in a folder, cut out green colored text, paste green text to new doc, save both docs in same folder. Code crashes on .Activate. Set statement is empty. Not sure why?
Code:
Sub FindGreenText() Application.ScreenUpdating = False Dim oSel As Range Dim oResponse As Document Dim oDoc As Document Dim strDocName As String, strPath As String, strFile As String, strFolder As String strFolder = "<folder location with doc files>" strFile = Dir(strFolder & "\*.docx", vbNormal) Set oDoc = Documents.Open(FileName:=strFolder & " \ " & strFile, AddToRecentFiles:=False, Visible:=True) While strFile <> "" oDoc.Activate 'code stops here with empty set statement 'DoEvents strDocName = Left(oDoc.Name, InStrRev(oDoc.Name, Chr(46)) - 1) strDocName = strDocName & "_response.docx" strPath = oDoc.Path & Chr(92) Set sel = ActiveDocument.Range Set oResponse = Documents.Add With sel.Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .MatchWildcards = False .Forward = True .Wrap = wdFindStop .Font.ColorIndex = wdGreen .Execute Do Until Not .Found If sel.Font.ColorIndex = wdGreen Then sel.Cut With oResponse Selection.Range.Paste Selection.MoveStart unit:=wdParagraph Selection.TypeParagraph End With End If .Execute Loop End With oDoc.SaveAs2 oResponse.SaveAs2 strPath & strDocName oDoc.Close oResponse.Close strFile = Dir() Wend Application.ScreenUpdating = True MsgBox "Lucy, I'm home!" End Sub |
#4
|
|||
|
|||
![]()
Hi! What I've noticed at once:
1. There are unnedeed spaces before & after the backslash in: strFolder & " \ " & strFile 2. Use either sel or oSel, not both. |
#5
|
|||
|
|||
![]()
ahh, that was it!
Now for a very interesting twist.... code works for wdYellow text and not for wdGreen text. I tried this several times and cannot understand why. Anyone have a suggestion or thought? |
#6
|
|||
|
|||
![]()
Your code does work for WdGreen! The problem is your green font isn't actually the vba WdGreen,
although Word sets it as green. The vba green & the Word green have the same value (11) but are different colors! Check for yourself using RGB codes: the genuine (vba) WdGreen is R=0, G=128, B=0 but your Word green is anything else. It's strange, but the Word's Find-Replace dialogue recognizes the Word's green, set using the Find-Replace Format Font dialogue, but doesn't recognize the vba WdGreen, while the vba / macro code recognizes the vba WdGreen but is blind to the Word Green. To make the macro work for your "green", select any part of the colored text & use msgbox selection.Font.ColorIndex to get the code of your color. Then replace WdGreen in Find with the obtained code. |
![]() |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
gasparik | Word VBA | 1 | 05-11-2020 05:41 AM |
![]() |
RET | Word | 1 | 05-02-2016 07:28 PM |
![]() |
userman | Word | 2 | 09-26-2012 01:52 AM |
not showing colored text from external emails | MasterGator | Outlook | 0 | 01-31-2012 02:20 PM |
Making text boxes with colored bar at the top | daviddoria | PowerPoint | 0 | 08-30-2009 03:19 PM |