From: Graham Mayor on 30 Dec 2009 09:58 Using FormattedText did however resolve a problem I was having getting the macro to format the text correctly in a table cell. I don't know why I didn't consider it earlier :( Using the table does seem noticably faster. Can you tell it's the slack period twixt Christmas and New Year? ;) Dim oRng, oNrng As Range Dim oSource, oDoc As Document Dim oTable As Table Dim iRow, iPage, ILen As Integer Dim iPara, iIst, iLast As Integer Dim sFont, SComp, sNext, sWords As String Dim sColor As WdColor Set oSource = ActiveDocument Set oDoc = Documents.Add Set oTable = oDoc.Tables.Add(oDoc.Range, 2, 4) With oTable .Cell(1, 1).Range.Text = "Highlighted Text" .Cell(1, 2).Range.Text = "Page" .Cell(1, 3).Range.Text = "Font" .Cell(1, 4).Range.Text = "Comments" With .Rows(1).Range .ParagraphFormat.Alignment = _ wdAlignParagraphCenter .Font.name = "Arial" .Font.Size = "12" .Bold = True End With End With oSource.Activate With Selection .HomeKey Unit:=wdStory With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .Highlight = True .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = False Do While .Execute = True Set oRng = Selection.Range With oRng iIst = .Start - .Words.First.Start + 1 iLast = .Words.Last.End - .End sNext = .Next.Characters(1) 'sColor = .HighlightColorIndex If .Start <> .Words.First.Start Or _ .End <> .Words.Last.End - 1 And _ sNext <> "" Then Select Case sNext Case ",", ".", "?", "!", ":", ";" SComp = "" iLast = iLast + 1 Case Else SComp = "Partly highlighted" End Select Else SComp = "" End If .Start = .Words.First.Start .End = .Words.Last.End If .Characters.Last = Chr(32) Then .End = .Words.Last.End - 1 End If sFont = .Font.name If Len(sFont) < 1 Then sFont = "Mixed fonts detected" iPage = .Information(wdActiveEndPageNumber) iRow = oTable.Rows.Count oTable.Cell(iRow, 1).Range.FormattedText = oRng.FormattedText oTable.Cell(iRow, 2).Range.Text = iPage oTable.Cell(iRow, 2).Range.ParagraphFormat.Alignment _ = wdAlignParagraphCenter oTable.Cell(iRow, 3).Range.Text = sFont oTable.Cell(iRow, 4).Range.Text = SComp oTable.Rows.Add End With Loop End With End With oTable.Rows.Last.Delete oDoc.Activate -- <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org <>>< ><<> ><<> <>>< ><<> <>>< <>><<> "DaveLett" <DaveLett(a)discussions.microsoft.com> wrote in message news:C14FC748-B6F0-4352-B6C9-C06E38D0F3EA(a)microsoft.com... > Hi, > Just out of curiosity... > Would it be more efficient to use .FormattedText on the range objects > instead of .Copy and .Paste? From my test, doing so would change the > following: > > iPage = .Information(wdActiveEndPageNumber) > .Copy > .Collapse wdCollapseEnd > With oListRng > .Collapse wdCollapseEnd > .InsertAfter "," > .End = .End - 1 > .Paste > .Start = .End + 1 > .Text = " Page " & iPage & ", Name: " & sFont & sComp & vbCr > .HighlightColorIndex = wdAuto > End With > > To the following: > iPage = .Information(wdActiveEndPageNumber) > With oListRng > .Collapse wdCollapseEnd > .FormattedText = oRng.FormattedText > .Start = .End + 1 > .InsertAfter Text:=", Page " & iPage & ", Name: " & sFont & sComp & vbCr > .HighlightColorIndex = wdAuto > End With > .Collapse wdCollapseEnd > > Thanks, > Dave
From: andreas on 30 Dec 2009 11:41 On 30 Dez., 15:58, "Graham Mayor" <gma...(a)REMOVETHISmvps.org> wrote: > Using FormattedText did however resolve a problem I was having getting the > macro to format the text correctly in a table cell. I don't know why I > didn't consider it earlier :( > > Using the table does seem noticably faster. > > Can you tell it's the slack period twixt Christmas and New Year? ;) > > Dim oRng, oNrng As Range > Dim oSource, oDoc As Document > Dim oTable As Table > Dim iRow, iPage, ILen As Integer > Dim iPara, iIst, iLast As Integer > Dim sFont, SComp, sNext, sWords As String > Dim sColor As WdColor > Set oSource = ActiveDocument > Set oDoc = Documents.Add > Set oTable = oDoc.Tables.Add(oDoc.Range, 2, 4) > With oTable > .Cell(1, 1).Range.Text = "Highlighted Text" > .Cell(1, 2).Range.Text = "Page" > .Cell(1, 3).Range.Text = "Font" > .Cell(1, 4).Range.Text = "Comments" > With .Rows(1).Range > .ParagraphFormat.Alignment = _ > wdAlignParagraphCenter > .Font.name = "Arial" > .Font.Size = "12" > .Bold = True > End With > End With > oSource.Activate > With Selection > .HomeKey Unit:=wdStory > With .Find > .ClearFormatting > .Replacement.ClearFormatting > .Text = "" > .Highlight = True > .Replacement.Text = "" > .Forward = True > .Wrap = wdFindContinue > .Format = True > .MatchCase = False > .MatchWholeWord = False > .MatchAllWordForms = False > .MatchSoundsLike = False > .MatchWildcards = False > Do While .Execute = True > Set oRng = Selection.Range > With oRng > iIst = .Start - .Words.First.Start + 1 > iLast = .Words.Last.End - .End > sNext = .Next.Characters(1) > 'sColor = .HighlightColorIndex > If .Start <> .Words.First.Start Or _ > .End <> .Words.Last.End - 1 And _ > sNext <> "" Then > Select Case sNext > Case ",", ".", "?", "!", ":", ";" > SComp = "" > iLast = iLast + 1 > Case Else > SComp = "Partly highlighted" > End Select > Else > SComp = "" > End If > .Start = .Words.First.Start > .End = .Words.Last.End > If .Characters.Last = Chr(32) Then > .End = .Words.Last.End - 1 > End If > sFont = .Font.name > If Len(sFont) < 1 Then sFont = "Mixed fonts detected" > iPage = .Information(wdActiveEndPageNumber) > iRow = oTable.Rows.Count > oTable.Cell(iRow, 1).Range.FormattedText = > oRng.FormattedText > oTable.Cell(iRow, 2).Range.Text = iPage > oTable.Cell(iRow, 2).Range.ParagraphFormat.Alignment _ > = wdAlignParagraphCenter > oTable.Cell(iRow, 3).Range.Text = sFont > oTable.Cell(iRow, 4).Range.Text = SComp > oTable.Rows.Add > End With > Loop > End With > End With > oTable.Rows.Last.Delete > oDoc.Activate > > -- > <>>< ><<> ><<> <>>< ><<> <>>< <>><<> > Graham Mayor - Word MVP > > My web sitewww.gmayor.com > Word MVP web sitehttp://word.mvps.org > <>>< ><<> ><<> <>>< ><<> <>>< <>><<> > > "DaveLett" <DaveL...(a)discussions.microsoft.com> wrote in message > > news:C14FC748-B6F0-4352-B6C9-C06E38D0F3EA(a)microsoft.com... > > > > > Hi, > > Just out of curiosity... > > Would it be more efficient to use .FormattedText on the range objects > > instead of .Copy and .Paste? From my test, doing so would change the > > following: > > > iPage = .Information(wdActiveEndPageNumber) > > .Copy > > .Collapse wdCollapseEnd > > With oListRng > > .Collapse wdCollapseEnd > > .InsertAfter "," > > .End = .End - 1 > > .Paste > > .Start = .End + 1 > > .Text = " Page " & iPage & ", Name: " & sFont & sComp & vbCr > > .HighlightColorIndex = wdAuto > > End With > > > To the following: > > iPage = .Information(wdActiveEndPageNumber) > > With oListRng > > .Collapse wdCollapseEnd > > .FormattedText = oRng.FormattedText > > .Start = .End + 1 > > .InsertAfter Text:=", Page " & iPage & ", Name: " & sFont & sComp & vbCr > > .HighlightColorIndex = wdAuto > > End With > > .Collapse wdCollapseEnd > > > Thanks, > > Dave- Zitierten Text ausblenden - > > - Zitierten Text anzeigen - To all of you (especially Graham and Greg, as well as Dave). What shall I say? I am so impressed by your skills!! Exactly what I wanted!! You two truly deserve the MVP designation. Graham's code serves my purpose a little better since partially highlighted words get copied and pasted completely (that is not just the highlighted part, but the whole word). Anyway. Great coding!! You three have a really nice new year's eve and all the best for 2010 from an enthusiastic user of this forum. Regards, Andreas
From: andreas on 30 Dec 2009 11:52 On 30 Dez., 13:19, "Greg Maxey" <gma...(a)mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote: > Graham, > > Yep you are right. I trimmed to much and was using poor example text. > Culprit was the trailing space character in the .Word.Range. This should > sort it out: > > Sub ScratchMaco() > Dim oSource As Word.Document, oListDoc As Word.Document > Dim oRng As Word.Range, oListRng As Word.Range > Dim sFont As String, sComp As String > Dim bCompStart As Boolean, bCompEnd As Boolean > Dim iPage As Long > Set oSource = ActiveDocument > Set oListDoc = Documents.Add > Set oListRng = oListDoc.Range > oSource.Activate > oSource.Range(0, 0).Select > Set oRng = oSource.Range > With oRng > With .Find > .ClearFormatting > .Replacement.ClearFormatting > .Highlight = True > .Forward = True > .Wrap = wdFindStop > .Format = True > Do While .Execute = True > With oRng > bCompStart = False > bCompEnd = False > If .Start = .Words(1).Start Then bCompStart = True > .Start = .Words.First.Start > Select Case .Words(.Words.Count).Characters.Last > Case Is = Chr(32) > If .End = .Words(.Words.Count).End - 1 Then bCompEnd = True > Case Else > If .End = .Words(.Words.Count).End Then bCompEnd = True > .End = .Words.Last.End > End Select > sComp = "" > If Not bCompStart Or Not bCompEnd Then sComp = ", partially > highlighted" > sFont = .Font.Name > If Len(sFont) < 1 Then sFont = "Mixed fonts detected and a specific > name is not determined" > iPage = .Information(wdActiveEndPageNumber) > .Copy > .Collapse wdCollapseEnd > With oListRng > .Collapse wdCollapseEnd > .InsertAfter "," > .End = .End - 1 > .Paste > .Start = .End + 1 > .Text = " Page " & iPage & ", Name: " & sFont & sComp & vbCr > .HighlightColorIndex = wdAuto > End With > End With > Loop > End With > End With > oListDoc.Range.Paragraphs.Last.Range.Delete > oListDoc.Activate > End Sub > > > > Graham Mayor wrote: > > While the font name trap line is a handy addition, your version of > > the macro marks all the highlighted words in my test sample as > > partially highted - back to the drawing board ;) > > > "Greg Maxey" <gma...(a)mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote in > > messagenews:ONCoLlNiKHA.2184(a)TK2MSFTNGP04.phx.gbl... > >> I think my first attempt involved some over kill as I was half way > >> through it before I realized the OP wanted to list the whole of > >> partically highlighted words. Rev 2: > > >> Sub ScratchMaco() > >> Dim oSource As Word.Document, oListDoc As Word.Document > >> Dim oRng As Word.Range, oListRng As Word.Range > >> Dim sFont As String, sComp As String > >> Dim bCompStart As Boolean, bCompEnd As Boolean > >> Dim iPage As Long > >> Set oSource = ActiveDocument > >> Set oListDoc = Documents.Add > >> Set oListRng = oListDoc.Range > >> oSource.Activate > >> oSource.Range(0, 0).Select > >> Set oRng = oSource.Range > >> With oRng > >> With .Find > >> .ClearFormatting > >> .Replacement.ClearFormatting > >> .Highlight = True > >> .Forward = True > >> .Wrap = wdFindStop > >> .Format = True > >> Do While .Execute = True > >> With oRng > >> bCompStart = False > >> bCompEnd = False > >> If .Start = .Words(1).Start Then bCompStart = True > >> If .End = .Words(.Words.Count).End Then bCompEnd = True > >> sComp = "" > >> If Not bCompStart Or Not bCompEnd Then sComp = ", partially > >> highlighted" > >> sFont = .Font.Name > >> If Len(sFont) < 1 Then sFont = "Mixed fonts detected and a > >> specific name is not determined" > >> iPage = .Information(wdActiveEndPageNumber) > >> .Start = .Words.First.Start > >> .End = .Words.Last.End > >> .Copy > >> .Collapse wdCollapseEnd > >> With oListRng > >> .Collapse wdCollapseEnd > >> .InsertAfter "," > >> .End = .End - 1 > >> .Paste > >> .Start = .End + 1 > >> .Text = " Page " & iPage & ", Name: " & sFont & sComp & vbCr > >> .HighlightColorIndex = wdAuto > >> End With > >> End With > >> Loop > >> End With > >> End With > >> oListDoc.Range.Paragraphs.Last.Range.Delete > >> oListDoc.Activate > >> End Sub > > >> Graham Mayor wrote: > >>> The following takes care of the issues raised by Dave and the one I > >>> raised myself. It gets no prizes for elegant coding, but should do > >>> the job? > >>> Dim oRng As Range > >>> Dim oNRng As Range > >>> Dim oSource As Document > >>> Dim oDoc As Document > >>> Dim iPage As Integer > >>> Dim iLen As Integer > >>> Dim iPara As Integer > >>> Dim iIst As Integer > >>> Dim iLast As Integer > >>> Dim sFont As String > >>> Dim sComp As String > >>> Dim sNext As String > >>> Dim sWords As String > >>> Dim sColor As WdColor > >>> Set oSource = ActiveDocument > >>> Set oDoc = Documents.Add > >>> oSource.Activate > >>> With Selection > >>> .HomeKey Unit:=wdStory > >>> With .Find > >>> .ClearFormatting > >>> .Replacement.ClearFormatting > >>> .Text = "" > >>> .Highlight = True > >>> .Replacement.Text = "" > >>> .Forward = True > >>> .Wrap = wdFindContinue > >>> .Format = True > >>> .MatchCase = False > >>> .MatchWholeWord = False > >>> .MatchAllWordForms = False > >>> .MatchSoundsLike = False > >>> .MatchWildcards = False > >>> Do While .Execute = True > >>> Set oRng = Selection.Range > >>> With oRng > >>> iIst = .Start - .Words.First.Start + 1 > >>> iLast = .Words.Last.End - .End > >>> sNext = .Next.Characters(1) > >>> sColor = .HighlightColorIndex > >>> If .Start <> .Words.First.Start Or _ > >>> .End <> .Words.Last.End - 1 And _ > >>> sNext <> "" Then > >>> Select Case sNext > >>> Case ",", ".", "?", "!", ":", ";" > >>> sComp = "" > >>> iLast = iLast + 1 > >>> Case Else > >>> sComp = ", Partly highlighted" > >>> End Select > >>> Else > >>> sComp = "" > >>> End If > >>> .Start = .Words.First.Start > >>> .End = .Words.Last.End > >>> If .Characters.Last = Chr(32) Then > >>> .End = .Words.Last.End - 1 > >>> End If > >>> .Copy > >>> sFont = .Font.name > >>> iPage = .Information(wdActiveEndPageNumber) > >>> oDoc.Range.InsertAfter oRng.Text & _ > >>> ", Page " & _ > >>> iPage & ", Name: " _ > >>> & sFont & sComp & vbCr > >>> iPara = oDoc.Paragraphs.Count - 1 > >>> iLen = InStr(1, oDoc.Paragraphs(iPara).Range.Text, > >>> ",") For i = iIst To iLen - iLast > > >>> oDoc.Paragraphs(iPara).Range.Characters(i).HighlightColorIndex = > >>> sColor Next i > >>> End With > >>> Loop > >>> End With > >>> End With > >>> With oDoc.Range > >>> .Paragraphs.Last.Range.Delete > >>> End With > >>> oDoc.Activate > > >>> "andreas" <andreas.her...(a)gmx.de> wrote in message > >>>news:77082fa1-4e2e-469b-a89a-61effbb4ad60(a)a21g2000yqc.googlegroups.com.... > >>> On 29 Dez., 08:59, "Graham Mayor" <gma...(a)REMOVETHISmvps.org> wrote: > >>>> That first possibility occurred to me after I posted, but I was > >>>> going out to > >>>> a festive party, so I did not pursue it further. Now back, with a > >>>> hangover, > >>>> I might be inclined to explore it further. > > >>>> Regarding the second point, I took the OP at his word that he meant > >>>> 'word' and not 'words' (always a risky thing to do), but felt it > >>>> possible that there may be a couple of words and I allowed for > >>>> that. The macro should report the font name, though hopefully > >>>> there would not be more than one font > >>>> in the range. > > >>>> They were not the only problems I could think of either. I suspect > >>>> that the > >>>> OP wants the highlighted word formatted as it appears in the > >>>> document when it is reproduced in the extract. This is fiddly to > >>>> achieve without copy and > >>>> paste - and in any case then I wonder at the point of it. If the > >>>> plan was to > >>>> identify words that had not been completely highlighted it would be > >>>> simpler > >>>> to correct that with the macro before extracting the data and have > >>>> no partially formatted words. > > >>>> I'll wait and see what the OP has to say before playing further - > >>>> to give the hangover time to fade ;) - when we might learn the > >>>> point of the exercise, which might then point to a different > >>>> approach entirely. -- > >>>> <>>< ><<> ><<> <>>< ><<> <>>< <>><<> > >>>> Graham Mayor - Word MVP > > >>>> My web sitewww.gmayor.com > >>>> Word MVP web sitehttp://word.mvps.org > >>>> <>>< ><<> ><<> <>>< ><<> <>>< <>><<> > > >>>> "DaveLett" <DaveL...(a)discussions.microsoft.com> wrote in message > > >>>>news:BBD90143-59BE-42B9-8A93-955D82953E1F(a)microsoft.com... > > >>>>> Hi all, > >>>>> I was reading/testing Graham's post to see if I could learn > >>>>> something new, > >>>>> and I think I might have found a couple of issues, which are > >>>>> probably just > >>>>> rare occurrences anyway. > > >>>>> 1) The line ".End = .Words.Last.End - 1" presumes (I think) that > >>>>> the word > >>>>> ends with a space after it. When I run the routine in a test > >>>>> document, any > >>>>> word that is highlighted and followed by a punctuation mark or > >>>>> paragraph, > >>>>> the > >>>>> routine reports as partially highlighted. > > >>>>> 2) In the very outside chance the that highlighted word is > >>>>> actually a group > >>>>> of words, then the routine doesn't "report" the font name. > > >>>>> All of this is just FYI and FWIW, > > >>>>> Dave- Zitierten Text ausblenden - > > >>>> - Zitierten Text anzeigen - > > >>> Hi Graham and Dave, > > >>> I am really very impressed by the professionalism with which you > >>> tackle/sort out VBA problems. This forum is more than terrific. > > >>> I am getting back soon with a detailed analysis. > > ... > > Erfahren Sie mehr »- Zitierten Text ausblenden - > > - Zitierten Text anzeigen - Hi Greg, please see comment to all of you at the very bottom.Terrific support. Thank you very much. Regards, Andreas
From: Greg Maxey on 30 Dec 2009 19:49 Graham, Can you make it play a tune ;-) Graham Mayor wrote: > Using FormattedText did however resolve a problem I was having > getting the macro to format the text correctly in a table cell. I > don't know why I didn't consider it earlier :( > > Using the table does seem noticably faster. > > Can you tell it's the slack period twixt Christmas and New Year? ;) > > Dim oRng, oNrng As Range > Dim oSource, oDoc As Document > Dim oTable As Table > Dim iRow, iPage, ILen As Integer > Dim iPara, iIst, iLast As Integer > Dim sFont, SComp, sNext, sWords As String > Dim sColor As WdColor > Set oSource = ActiveDocument > Set oDoc = Documents.Add > Set oTable = oDoc.Tables.Add(oDoc.Range, 2, 4) > With oTable > .Cell(1, 1).Range.Text = "Highlighted Text" > .Cell(1, 2).Range.Text = "Page" > .Cell(1, 3).Range.Text = "Font" > .Cell(1, 4).Range.Text = "Comments" > With .Rows(1).Range > .ParagraphFormat.Alignment = _ > wdAlignParagraphCenter > .Font.name = "Arial" > .Font.Size = "12" > .Bold = True > End With > End With > oSource.Activate > With Selection > .HomeKey Unit:=wdStory > With .Find > .ClearFormatting > .Replacement.ClearFormatting > .Text = "" > .Highlight = True > .Replacement.Text = "" > .Forward = True > .Wrap = wdFindContinue > .Format = True > .MatchCase = False > .MatchWholeWord = False > .MatchAllWordForms = False > .MatchSoundsLike = False > .MatchWildcards = False > Do While .Execute = True > Set oRng = Selection.Range > With oRng > iIst = .Start - .Words.First.Start + 1 > iLast = .Words.Last.End - .End > sNext = .Next.Characters(1) > 'sColor = .HighlightColorIndex > If .Start <> .Words.First.Start Or _ > .End <> .Words.Last.End - 1 And _ > sNext <> "" Then > Select Case sNext > Case ",", ".", "?", "!", ":", ";" > SComp = "" > iLast = iLast + 1 > Case Else > SComp = "Partly highlighted" > End Select > Else > SComp = "" > End If > .Start = .Words.First.Start > .End = .Words.Last.End > If .Characters.Last = Chr(32) Then > .End = .Words.Last.End - 1 > End If > sFont = .Font.name > If Len(sFont) < 1 Then sFont = "Mixed fonts detected" > iPage = .Information(wdActiveEndPageNumber) > iRow = oTable.Rows.Count > oTable.Cell(iRow, 1).Range.FormattedText = > oRng.FormattedText > oTable.Cell(iRow, 2).Range.Text = iPage > oTable.Cell(iRow, 2).Range.ParagraphFormat.Alignment _ > = wdAlignParagraphCenter > oTable.Cell(iRow, 3).Range.Text = sFont > oTable.Cell(iRow, 4).Range.Text = SComp > oTable.Rows.Add > End With > Loop > End With > End With > oTable.Rows.Last.Delete > oDoc.Activate > > > "DaveLett" <DaveLett(a)discussions.microsoft.com> wrote in message > news:C14FC748-B6F0-4352-B6C9-C06E38D0F3EA(a)microsoft.com... >> Hi, >> Just out of curiosity... >> Would it be more efficient to use .FormattedText on the range objects >> instead of .Copy and .Paste? From my test, doing so would change the >> following: >> >> iPage = .Information(wdActiveEndPageNumber) >> .Copy >> .Collapse wdCollapseEnd >> With oListRng >> .Collapse wdCollapseEnd >> .InsertAfter "," >> .End = .End - 1 >> .Paste >> .Start = .End + 1 >> .Text = " Page " & iPage & ", Name: " & sFont & sComp & vbCr >> .HighlightColorIndex = wdAuto >> End With >> >> To the following: >> iPage = .Information(wdActiveEndPageNumber) >> With oListRng >> .Collapse wdCollapseEnd >> .FormattedText = oRng.FormattedText >> .Start = .End + 1 >> .InsertAfter Text:=", Page " & iPage & ", Name: " & sFont & sComp & >> vbCr .HighlightColorIndex = wdAuto >> End With >> .Collapse wdCollapseEnd >> >> Thanks, >> Dave
First
|
Prev
|
Pages: 1 2 3 4 Prev: Different results from counts ... why? Next: save as docx from office 2003 |