Advertisement
Guest User

Untitled

a guest
Jan 28th, 2011
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Sub SpinArticle()
  2.  
  3.     Dim objSI As Word.SynonymInfo
  4.  
  5.     Dim strWord As String
  6.  
  7.     Dim strData As String
  8.  
  9.     Dim strResult As String
  10.  
  11.     Dim arrItems() As String
  12.  
  13.     Dim blnReplaced As Boolean
  14.  
  15.     Dim i As Integer
  16.  
  17.     Dim j As Integer
  18.  
  19.     Dim intMax As Integer
  20.  
  21.     Dim blnIsCap As Boolean
  22.  
  23.     Dim strTemp As String
  24.  
  25.     Dim strChar As String
  26.  
  27.     Dim lngNumWords As Long
  28.  
  29.     Dim strCap As String
  30.  
  31.     Dim strPunc As String
  32.  
  33.    
  34.  
  35.     RemoveTags
  36.  
  37.    
  38.  
  39.     strResult = ""
  40.  
  41.     strCap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  42.  
  43.     strPunc = ".!?,;"
  44.  
  45.    
  46.  
  47.     '* begin looping through every word in the ActiveDocument
  48.  
  49.     lngNumWords = ActiveDocument.Words.Count
  50.  
  51.     For j = 1 To lngNumWords
  52.  
  53.         strWord = Trim(ActiveDocument.Words(j))
  54.  
  55.         blnReplaced = False
  56.  
  57.        
  58.  
  59.         '* only worry about large words
  60.  
  61.         If Len(strWord) > 3 Then
  62.  
  63.             Set objSI = Word.SynonymInfo(strWord)
  64.  
  65.            
  66.  
  67.             '* check if the word is a NOUN or an ADJECTIVE
  68.  
  69.             If UBound(objSI.PartOfSpeechList) <> 0 Then
  70.  
  71.                 'If objSI.PartOfSpeechList(1) = wdNoun Then
  72.  
  73.                 If objSI.PartOfSpeechList(1) = wdAdjective Then
  74.  
  75.                    
  76.  
  77.                     '* check is synonyms found
  78.  
  79.                     If objSI.Found = True Then
  80.  
  81.                            
  82.  
  83.                         '* check for capitalization
  84.  
  85.                         strChar = Left(strWord, 1)
  86.  
  87.                         If InStr(strCap, strChar) > 0 Then
  88.  
  89.                             blnIsCap = True
  90.  
  91.                         Else
  92.  
  93.                             blnIsCap = False
  94.  
  95.                         End If
  96.  
  97.                        
  98.  
  99.                         '* examine the list of synonyms and prepare { tags
  100.  
  101.                         arrItems = objSI.SynonymList(1)
  102.  
  103.                         'arrItems = objSI.RelatedWordList
  104.  
  105.                         If UBound(arrItems) <> 0 Then
  106.  
  107.                                
  108.  
  109.                             strData = "{" & Trim(strWord)
  110.  
  111.                             intMax = UBound(arrItems)
  112.  
  113.                             If intMax > 3 Then intMax = 3
  114.  
  115.                             For i = 1 To intMax
  116.  
  117.                                 strTemp = arrItems(i)
  118.  
  119.                                 If blnIsCap Then
  120.  
  121.                                     strTemp = UCase(Left(strTemp, 1)) _
  122.  
  123.                                         & Right(strTemp, Len(strTemp) – 1)
  124.  
  125.                                 End If
  126.  
  127.                                 strData = strData & "|" & strTemp
  128.  
  129.                             Next i
  130.  
  131.                             strData = strData & "}"
  132.  
  133.                
  134.  
  135.                             '* confirm this replacement is good
  136.  
  137.                             If j > 3 And j < lngNumWords – 3 Then
  138.  
  139.                                 strTemp = ActiveDocument.Words(j – 3) _
  140.  
  141.                                         & " " & ActiveDocument.Words(j – 2) _
  142.  
  143.                                         & " " & ActiveDocument.Words(j – 1) _
  144.  
  145.                                         & " " & Chr(34) & strWord & Chr(34) _
  146.  
  147.                                         & " " & ActiveDocument.Words(j + 1) _
  148.  
  149.                                         & " " & ActiveDocument.Words(j + 2) _
  150.  
  151.                                         & " " & ActiveDocument.Words(j + 3)
  152.  
  153.                             Else
  154.  
  155.                                 strTemp = Chr(34) & strWord & Chr(34)
  156.  
  157.                             End If
  158.  
  159.                             'strTemp = InputBox("Change " & vbCrLf _
  160.  
  161.                                '& strTemp & vbCrLf & " to ", , strData)
  162.  
  163.                             'If StrPtr(strTemp) = 0 Then
  164.  
  165.                               '  '* user pressed cancel
  166.  
  167.                             'Else
  168.  
  169.                                 blnReplaced = True
  170.  
  171.                                 strResult = strResult & " " & strData
  172.  
  173.                             'End If
  174.  
  175.                         End If
  176.  
  177.                     End If
  178.  
  179.                 End If
  180.  
  181.             End If
  182.  
  183.         End If
  184.  
  185.    
  186.  
  187.         '* if the word was not replace, add the original word to the result
  188.  
  189.         If blnReplaced = False Then
  190.  
  191.             If InStr(strPunc, strWord) > 0 Then
  192.  
  193.                 strResult = strResult & strWord
  194.  
  195.             Else
  196.  
  197.                 strResult = strResult & " " & strWord
  198.  
  199.             End If
  200.  
  201.         End If
  202.  
  203.     Next j
  204.  
  205.        
  206.  
  207.     ActiveDocument.Select
  208.  
  209.     Selection.Text = strResult
  210.  
  211.    
  212.  
  213. End Sub
  214.  
  215.  
  216.  
  217. Public Sub RemoveTags()
  218.  
  219.     Dim intStart As Integer
  220.  
  221.     Dim intEnd As Integer
  222.  
  223.     Dim strData As String
  224.  
  225.     Dim arrItems() As String
  226.  
  227.  
  228.  
  229.     '* move to the beginning of the document
  230.  
  231.     Selection.HomeKey Unit:=wdStory
  232.  
  233.  
  234.  
  235.     '* find the first { tag
  236.  
  237.     Selection.Start = 0
  238.  
  239.     With Selection.Find
  240.  
  241.         .Text = "{"
  242.  
  243.         .Replacement.Text = ""
  244.  
  245.         .Forward = True
  246.  
  247.         .Wrap = wdFindContinue
  248.  
  249.         .Format = False
  250.  
  251.         .MatchCase = False
  252.  
  253.         .MatchWholeWord = False
  254.  
  255.         .MatchWildcards = False
  256.  
  257.         .MatchSoundsLike = False
  258.  
  259.         .MatchAllWordForms = False
  260.  
  261.     End With
  262.  
  263.     Selection.Find.Execute
  264.  
  265.    
  266.  
  267.     '* begin looping while { tags are found
  268.  
  269.     While Selection.Start > 0
  270.  
  271.         intStart = Selection.Start
  272.  
  273.    
  274.  
  275.         '* locate the ending } tag
  276.  
  277.         With Selection.Find
  278.  
  279.             .Text = "}"
  280.  
  281.             .Replacement.Text = ""
  282.  
  283.             .Forward = True
  284.  
  285.             .Wrap = wdFindContinue
  286.  
  287.             .Format = False
  288.  
  289.             .MatchCase = False
  290.  
  291.             .MatchWholeWord = False
  292.  
  293.             .MatchWildcards = False
  294.  
  295.             .MatchSoundsLike = False
  296.  
  297.             .MatchAllWordForms = False
  298.  
  299.         End With
  300.  
  301.         Selection.Find.Execute
  302.  
  303.        
  304.  
  305.         intEnd = Selection.End
  306.  
  307.    
  308.  
  309.         '* adjust the selection to capture {…}
  310.  
  311.         Selection.Start = intStart
  312.  
  313.         Selection.End = intEnd
  314.  
  315.         Selection.Select
  316.  
  317.            
  318.  
  319.         RemoveSelectionTags
  320.  
  321.        
  322.  
  323.         '* locate the next { tag
  324.  
  325.         Selection.Start = 0
  326.  
  327.         With Selection.Find
  328.  
  329.             .Text = "{"
  330.  
  331.             .Replacement.Text = ""
  332.  
  333.             .Forward = True
  334.  
  335.             .Wrap = wdFindContinue
  336.  
  337.             .Format = False
  338.  
  339.             .MatchCase = False
  340.  
  341.             .MatchWholeWord = False
  342.  
  343.             .MatchWildcards = False
  344.  
  345.             .MatchSoundsLike = False
  346.  
  347.             .MatchAllWordForms = False
  348.  
  349.         End With
  350.  
  351.         Selection.Find.Execute
  352.  
  353.    
  354.  
  355.     '* continue the looping
  356.  
  357.     Wend
  358.  
  359. End Sub
  360.  
  361.  
  362.  
  363. Public Sub RemoveSelectionTags()
  364.  
  365.      Dim strData As String
  366.  
  367.      Dim arrItems() As String
  368.  
  369.  
  370.  
  371.      '* remove the { tags
  372.  
  373.      strData = Selection.Text
  374.  
  375.      strData = Replace(strData, "{", "")
  376.  
  377.      strData = Replace(strData, "}", "")
  378.  
  379.    
  380.  
  381.      '* split the alternates by the pipe delimiter
  382.  
  383.      '* use the first alternative
  384.  
  385.      arrItems = Split(strData, "|")
  386.  
  387.      strData = arrItems(0)
  388.  
  389.    
  390.  
  391.      Selection.Text = strData
  392.  
  393. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement