Results 1 to 10 of 10
  1. #1
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Macros (2002)

    You can do a Find/Replace.
    1. Select the paragraph(s) for which you want information.
    2. Press Ctrl+H to bring up the replace box.
    3. In Find, type <font color=red>^t</font color=red>.
    4. In Replace, type <font color=red>^&</font color=red>.
    5. Do a replace all.

    This will replace a tab with itself & give you a count of how many replacements were made.
    Cheers,

  2. #2
    New Lounger
    Join Date
    Feb 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Macros (2002)

    Thanks Phil, that does give me the count I need. However, I didn't explain why I needed this information. I'm writing a macro that selects a paragraph, counts the number of tabs. If the paragraph contains 4 tabs, it performs a series of steps. If the paragraph contains 3 tabs, it performs a different series of steps. Is this possible? Actually, this is in preparation to converting text to a table.

  3. #3
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Macros (2002)

    I'm not a VBA expert, but I'm sure it can be done. It might be a simple Find/Replace, depending upon exactly what steps you want to perform.

  4. #4
    New Lounger
    Join Date
    Feb 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Macros (2002)

    Thanks, Andrew77. I'll give that a try.

  5. #5
    New Lounger
    Join Date
    Feb 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Macros (2002)

    It works!!! Thanks again!!!

  6. #6
    New Lounger
    Join Date
    Feb 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macro to count tabs in a paragraph (2002)

    <P ID="edit" class=small>(Edited by jscher2000 on 28-May-04 18:43. Subject updated for future reference)</P>I need to know the number of tabs in a paragraph. Is there a word function that will give me this information? Will a macro be able to do this?

  7. #7
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word Macros (2002)

    <P ID="edit" class=small>(Edited by Andrew77 on 29-May-04 03:46. )</P>Hello,

    I'm sure there are other ways, but here's a quick and dirty method that may get you started.

    <pre>Sub ProcessParasWithTabs
    Dim iTabs As Integer
    Dim rng As Range
    Dim para As Paragraph
    For Each para In ActiveDocument.Paragraphs
    iTabs = 0
    Set rng = para.Range
    Do While InStr(rng.Text, Chr(9)) <> 0
    iTabs = iTabs + 1
    rng.MoveStartUntil cset:=Chr(9), Count:=Len(rng.Text)
    rng.MoveStart unit:=wdCharacter, Count:=1
    Loop
    If iTabs = 3 Then
    ' Do stuff here
    ElseIf iTabs = 4 Then
    ' do stuff here
    End If
    Next para
    End Sub
    </pre>


    HTH

    Added: A more general function to get the count of any character in a paragraph would be:
    <pre>Function CharacterCountInPara(para As Paragraph, ByVal char As String) As Integer
    Dim rng As Range
    Dim iCount As Integer
    Set rng = para.Range
    iCount = 0
    char = Left(char, 1)
    Do While InStr(rng.Text, char) <> 0
    iCount = iCount + 1
    rng.MoveStartUntil cset:=char, Count:=Len(rng.Text)
    rng.MoveStart unit:=wdCharacter, Count:=1
    Loop
    CharacterCountInPara = iCount
    Set rng = Nothing
    End Function
    </pre>


    Comments welcome.

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Word Macros (2002)

    Hi Andrew,

    The following general function may be a little simpler - rather than resetting the range start for each match, it just loops through the paragraph range's collection of characters:

    <pre>Public Sub ProcessParasWithTabs()

    Dim lngTabCt As Long
    Dim aPara As Paragraph
    Dim ParaChars As Characters
    Dim strChar As String

    strChar = vbTab 'or Chr(9)

    For Each aPara In ActiveDocument.Paragraphs
    Set ParaChars = aPara.Range.Characters
    lngTabCt = GetCharCtInPara(ParaChars, strChar)
    Select Case lngTabCt
    Case 3
    'do appropriate stuff
    Case 4
    'do appropriate stuff
    Case Else
    'do appropriate stuff
    End Select
    Set ParaChars = Nothing
    Next 'aPara

    End Sub

    Public Function GetCharCtInPara(ParaCharacters As Characters, _
    Char As String) As Long
    Dim CharRng As Range
    Dim Counter As Long

    For Each CharRng In ParaCharacters
    If CharRng = Char Then
    Counter = Counter + 1
    End If
    Next 'CharRng

    GetCharCtInPara = Counter

    End Function</pre>

    Gary

  9. #9
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Word Macros (2002)

    Don't know if this is any simpler or more efficient than other suggestions, I usually do this type of thing in Word or other VBA host applications by using Len & Replace functions (Len tends to be efficient because it only has to evaluate the length of the text string, which is stored in the 4 bytes preceding the actual characters; it does not have to evaluate the text itself. This can be significant if the text being evaluated is lengthy.) Sample function & test sub:

    <code>Public Sub TestGetCharCount()</code>
    <code></code>
    <code> Dim sChar As String</code>
    <code> </code>
    <code> sChar = vbTab</code>
    <code> ' Non-case-sensitive comparison:</code>
    <code> MsgBox "Non-case-sensitive:" & vbCrLf & "Search text: " & sChar & vbCrLf & _</code>
    <code> "Found: " & GetSearchCharCount(sChar, vbTextCompare), vbInformation, _</code>
    <code> "GET CHAR COUNT FUNCTION"</code>
    <code> </code>
    <code> ' Case-sensitive comparison:</code>
    <code> MsgBox "Case-sensitive:" & vbCrLf & "Search text: " & sChar & vbCrLf & _</code>
    <code> "Found: " & GetSearchCharCount(sChar, vbBinaryCompare), vbInformation, _</code>
    <code> "GET CHAR COUNT FUNCTION"</code>
    <code></code>
    <code>End Sub</code>
    <code></code>
    <code>Public Function GetSearchCharCount(ByRef sChar As String, _</code>
    <code> ByVal CompareType As VbCompareMethod) As Long</code>
    <code></code>
    <code> Dim strTxt As String</code>
    <code> </code>
    <code> ' Evaluate 1st paragraph in selection:</code>
    <code> strTxt = Application.Selection.Paragraphs(1).Range.Text</code>
    <code> </code>
    <code> If Len(sChar) > 0 And InStr(1, strTxt, sChar, CompareType) > 0 Then</code>
    <code> GetSearchCharCount = (Len(strTxt) - _</code>
    <code> Len(Replace(strTxt, sChar, vbNullString, 1, -1, CompareType))) / Len(sChar)</code>
    <code> Else</code>
    <code> GetSearchCharCount = 0</code>
    <code> End If</code>
    <code> </code>
    <code>End Function</code>
    <code></code>

    The test sub looks for a tab character (vbTab constant) but function can be used with any text string of one or more characters (empty string will return zero). Also you have option of specifying case-sensitive (Binary) or non- case-sensitive (Text) comparison where applicable. The function evaluates the first paragraph in current selection; this can be modified as necessary.

    HTH

  10. #10
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Word Macros (2002)

    God bless you, boys -- keep those code samples coming!

    FWIW, here's a yet-more-generic function that adapts Mark's technique to count the number of substrings within a string:

    Function InStrCount(strFull As String, strTarget As String, _
    Optional ByVal CompareType As VbCompareMethod = vbTextCompare) As Long
    On Error GoTo ErrorHandler

    Dim strStripped As String

    If Len(strTarget) = 0 Or InStr(1, strFull, strTarget, CompareType) = 0 Then
    GoTo ExitLabel
    End If

    strStripped = Replace(strFull, strTarget, vbNullString, 1, -1, CompareType)

    InStrCount = (Len(strFull) - Len(strStripped)) / Len(strTarget)

    ExitLabel:
    Exit Function

    ErrorHandler:
    'Your error-handling code here.
    Resume ExitLabel

    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •