Quantcast
Channel: Free Tutorials » Development
Viewing all articles
Browse latest Browse all 10

Convert Word document to BBcode

$
0
0

To be honest I will provide you with a macro script for MS Word, which is not written by me, and it was taken from the phpBB community Word to BBcode Macro. This Visual Basic BBCode converter is really useful when you have CMS which is using BBCode, but in the same time does not provide enough options for easy management. Then every text you write, must be gone trough and where you have bold, italic, bolditalic, URL, , must be surrounded with the correct BB code. This is rather annoying especially when you want to mark frequently different keyword for your SEO.

The Word2BBCode-Converter is written on June 2, 2006 by Matthew Kruer,as some parts are adapted from \”Word2Wiki-Converter\”. Major improvements were made by Gunter Schmidt.
The BBCode converter works with MS Word XP,2000 and above. The current license is GPL, which means that you are free to use and modify, but you have to keep the credits and do not sell.

  1.     'Word2BBCode-Converter v0.1
  2.  
  3.    Sub Word2BBCode()
  4.      
  5.        Application.ScreenUpdating = False
  6.          
  7.        ConvertItalic
  8.        ConvertBold
  9.        ConvertUnderline
  10.        ConvertHyperlinks
  11.      
  12.        ActiveDocument.Content.Copy
  13.      
  14.        Application.ScreenUpdating = True
  15.    End Sub
  16.    Private Sub ConvertBold()
  17.        ActiveDocument.Select
  18.      
  19.        With Selection.Find
  20.      
  21.            .ClearFormatting
  22.            .Font.Bold = True
  23.            .Text = ""
  24.          
  25.            .Format = True
  26.            .MatchCase = False
  27.            .MatchWholeWord = False
  28.            .MatchWildcards = False
  29.            .MatchSoundsLike = False
  30.            .MatchAllWordForms = False
  31.          
  32.            .Forward = True
  33.            .Wrap = wdFindContinue
  34.          
  35.            Do While .Execute
  36.                With Selection
  37.                    If InStr(1, .Text, vbCr) Then
  38.                        ' Just process the chunk before any newline characters
  39.                         ' We'll pickup the rest with the next search
  40.                               .Font.Bold = False
  41.                         .Collapse
  42.                         .MoveEndUntil vbCr
  43.                     End If
  44.                                            
  45.                     ' Don't bother to markup newline characters (prevents a loop, as well)
  46.                     If Not .Text = vbCr Then
  47.                         .InsertBefore "[b]"
  48.                         .InsertAfter "[/b]"
  49.                     End If
  50.                    
  51.                     .Font.Bold = False
  52.                 End With
  53.             Loop
  54.         End With
  55.     End Sub
  56.     Private Sub ConvertItalic()
  57.         ActiveDocument.Select
  58.        
  59.         With Selection.Find
  60.        
  61.             .ClearFormatting
  62.             .Font.Italic = True
  63.             .Text = ""
  64.            
  65.             .Format = True
  66.             .MatchCase = False
  67.             .MatchWholeWord = False
  68.             .MatchWildcards = False
  69.             .MatchSoundsLike = False
  70.             .MatchAllWordForms = False
  71.            
  72.             .Forward = True
  73.             .Wrap = wdFindContinue
  74.            
  75.             Do While .Execute
  76.                 With Selection
  77.                     If InStr(1, .Text, vbCr) Then
  78.                         ' Just process the chunk before any newline characters
  79.                        ' We'll pick-up the rest with the next search
  80.                        .Font.Italic = False
  81.                        .Collapse
  82.                        .MoveEndUntil vbCr
  83.                    End If
  84.                                          
  85.                    ' Don't bother to markup newline characters (prevents a loop, as well)
  86.                    If Not .Text = vbCr Then
  87.                        .InsertBefore "[i]"
  88.                        .InsertAfter "[/i]"
  89.                    End If
  90.                  
  91.                    .Font.Italic = False
  92.                End With
  93.            Loop
  94.        End With
  95.    End Sub
  96.    Private Sub ConvertUnderline()
  97.        ActiveDocument.Select
  98.      
  99.        With Selection.Find
  100.      
  101.            .ClearFormatting
  102.            .Font.Underline = True
  103.            .Text = ""
  104.          
  105.            .Format = True
  106.            .MatchCase = False
  107.            .MatchWholeWord = False
  108.            .MatchWildcards = False
  109.            .MatchSoundsLike = False
  110.            .MatchAllWordForms = False
  111.          
  112.            .Forward = True
  113.            .Wrap = wdFindContinue
  114.          
  115.            Do While .Execute
  116.                With Selection
  117.                    If InStr(1, .Text, vbCr) Then
  118.                        ' Just process the chunk before any newline characters
  119.                         ' We'll pickup the rest with the next search
  120.                         .Font.Underline = False
  121.                         .Collapse
  122.                         .MoveEndUntil vbCr
  123.                     End If
  124.                                            
  125.                     ' Don't bother to markup newline characters (prevents a loop, as well)
  126.                     If Not .Text = vbCr Then
  127.                         .InsertBefore "[u]"
  128.                         .InsertAfter "[/u]"
  129.                     End If
  130.                    
  131.                     .Font.Underline = False
  132.                 End With
  133.             Loop
  134.         End With
  135.     End Sub
  136.  
  137.     Private Sub ConvertHyperlinks()
  138.         'converts Hyperlinks
  139.        '24MAY-2006: only convert http…, mark others with error marker
  140.  
  141.     Dim hyperCount&
  142.         Dim i&
  143.         Dim addr$ ', title$
  144.  
  145.        hyperCount = ActiveDocument.Hyperlinks.Count
  146.  
  147.        For i = 1 To hyperCount
  148.  
  149.            With ActiveDocument.Hyperlinks(1) 'must be 1, since the delete changes count and position
  150.  
  151.                 addr = .Address
  152.                 If Trim$(addr) = "" Then addr = "no hyperlink found"
  153.                 'title = .Range.Text
  154.              
  155.                'http, ftp
  156.                 If LCase(Left$(addr, 4)) = "http" Or LCase(Left$(addr, 3)) = "ftp" Then
  157.                     .Delete 'hyperlink
  158.                    .Range.InsertBefore "[url=" & addr & "]"
  159.                    .Range.InsertAfter "[/url]"
  160.                  
  161.                    GoTo ConvertHyperlinks_Next
  162.                End If
  163.              
  164.                'mailto:
  165.                 If LCase(Left$(addr, 7)) = "mailto:" Then
  166.                     .Delete 'hyperlink
  167.                    .Range.InsertBefore "[email]" & addr & " "
  168.                    .Range.InsertAfter "[/email]"
  169.                  
  170.                    GoTo ConvertHyperlinks_Next
  171.                End If
  172.              
  173.                'file guess
  174.                 If Len(addr) > 4 Then 'the reason for not nice goto
  175.                    If Mid$(addr, Len(addr) – 3, 1) = "." Then
  176.                        .Delete
  177.                        .Range.InsertBefore "[file://" & Replace(addr, " ", "_") & " "
  178.                        .Range.InsertAfter "]"
  179.                      
  180.                        GoTo ConvertHyperlinks_Next
  181.                    End If
  182.                End If
  183.              
  184.                'unidentified
  185.                 .Delete
  186.                 .Range.InsertBefore UnableToConvertMarker & "[" & addr & " "
  187.                 .Range.InsertAfter "]"
  188.  
  189.     ConvertHyperlinks_Next:
  190.             End With
  191.  
  192.         Next i
  193.  
  194.     End Sub

Copy the Word to BBCode converter, from the above field and save it as a .bas file(for example w2bb.bas).
To load the script, open your MS Word and expand the Macro section where you will find the Visual Basic editor.
Import the script from File -> Import File.

Convert Word document to BBcode

Once the Word to BBCode converter is imported either click ‘F5′, or go to ‘Run’ and then click ‘Run Macro’.
Then go back to MS Word and you will see your document converted.

The script is working perfect for me.
There is only one bug that happens, but I have not reported it yet since I am not sure what causing it. I think on a specific hidden character it just cannot end the \"while\" cycle.

The only resolution is to copy and paste the text in Notepad and then in a clean MS Word document. Unfortunately you will loose any formatting made.

Convert Word document to BBcode


Viewing all articles
Browse latest Browse all 10

Trending Articles