Jump to content


This is a read only archive of the old forums
The new CBn forums are located at https://quarterdeck.commanderbond.net/

 
Photo

Word Macro Assistance For Fan Fiction Writers


3 replies to this topic

#1 MkB

MkB

    Commander

  • Veterans
  • PipPipPipPip
  • 3864 posts

Posted 10 August 2008 - 04:33 PM

I wrote a little piece of fan fiction with a text processing software, but when I wanted to post it on the forum, I realised that
  • posting an html export doesn't work (the post shows all your html tags without interpreting them)
  • a simple copy / paste makes all your italics, bold, etc. settings disappear in the post

I tried to find a way to convert a Word / OpenOffice / RTF document into something compatible with the language used on the board (BBcode), but I didn't find anything useful (maybe it exists, but I didn't find it).

So, I modified a Word macro created by Alexis Dupont-Roc and released under under GPL licence for another CMS (SPIP), to fit my basic needs of BBcode. As I thought it could be useful to others here, you'll find the VBA code below.

It is not a full conversion tool: it will only convert
  • italics,
  • text in bold,
  • first level titles in centered, size = +5 text,
  • second level titles in centered, size = +3 text.

' text in bold
Sub bbcodeBold()

	 Selection.Find.ClearFormatting
	 Selection.Find.Font.Bold = True
	 Selection.Find.Replacement.ClearFormatting
	 With Selection.Find
		 .Text = ""
		 .Replacement.Text = "[b]^&[/b]"
		 .Forward = True
		 .Wrap = wdFindContinue
		 .Format = True
		 .MatchCase = False
		 .MatchWholeWord = False
		 .MatchWildcards = False
		 .MatchSoundsLike = False
		 .MatchAllWordForms = False
	 End With
	 Selection.Find.Execute Replace:=wdReplaceAll

End Sub


' text in bold and italics
Sub bbcodeBoldItalics()
	 
	 Selection.Find.ClearFormatting
	 Selection.Find.Font.Italic = True
	 Selection.Find.Font.Bold = True
	 Selection.Find.Replacement.ClearFormatting
	 With Selection.Find
		 .Text = ""
		 .Replacement.Text = "[b][i]^&[/i][/b]"
		 .Replacement.Font.Italic = False
		 .Replacement.Font.Bold = False
		 .Forward = True
		 .Wrap = wdFindContinue
		 .Format = True
		 .MatchCase = False
		 .MatchWholeWord = False
		 .MatchWildcards = False
		 .MatchSoundsLike = False
		 .MatchAllWordForms = False
	 End With
	 Selection.Find.Execute Replace:=wdReplaceAll

End Sub


' text in italics
Sub bbcodeItalics()
	 
	 Selection.Find.ClearFormatting
	 Selection.Find.Font.Italic = True
	 Selection.Find.Replacement.ClearFormatting
	 With Selection.Find
		 .Text = ""
		 .Replacement.Text = "[i]^&[/i]"
		 .Forward = True
		 .Wrap = wdFindContinue
		 .Format = True
		 .MatchCase = False
		 .MatchWholeWord = False
		 .MatchWildcards = False
		 .MatchSoundsLike = False
		 .MatchAllWordForms = False
	 End With
	 Selection.Find.Execute Replace:=wdReplaceAll

End Sub



' Title1 style => text size = +5
Sub bbcodeTitle1()

Dim i As Integer
Dim aRange As Range
For i = 1 To ActiveDocument.Paragraphs.Count
	If ActiveDocument.Paragraphs(i).style = ActiveDocument.Styles(wdStyleHeading1) Then
		ActiveDocument.Paragraphs(i).style = wdStyleNormal
		Set aRange = ActiveDocument.Range(Start:=ActiveDocument.Paragraphs(i).Range.Start, End:=ActiveDocument.Paragraphs(i).Range.End - 1)
		aRange.InsertBefore ("[center][size=" + Chr(34) + "5" + Chr(34) + "]")
		aRange.InsertAfter ("[/size][/center]")
	End If
Next i
End Sub



' Title2 style => text size = +3
Sub bbcodeTitle2()

Dim i As Integer
Dim aRange As Range
For i = 1 To ActiveDocument.Paragraphs.Count
	If ActiveDocument.Paragraphs(i).style = ActiveDocument.Styles(wdStyleHeading2) Then
		ActiveDocument.Paragraphs(i).style = wdStyleNormal
		Set aRange = ActiveDocument.Range(Start:=ActiveDocument.Paragraphs(i).Range.Start, End:=ActiveDocument.Paragraphs(i).Range.End - 1)
		aRange.InsertBefore ("[center][size=" + chr(34) + "3" + chr(34) + "]")
		aRange.InsertAfter ("[/size][/center]")
	End If
Next i

End Sub

How to use these macros?

In MS Word, go to Tools / Macro / Visual Basic Editor
Create a new module
Paste the code above into it.
Back to MS Word, go to Tools / Macro / Macros...: it should show you a list of macros in the active document. If everything went well, you'll find a list of those 5 macros:
  • bbcodeBold
  • bbcodeBoldItalics
  • bbcodeItalics
  • bbcodeTitle1
  • bbcodeTitle2
Execute those you need to: they add BBcode directly in your Word files, where needed.
Copy and paste the text in a post on the forum!


I strongly advise you not to do this on you original file: use a copy of your file, or create a new file and paste into it your text, then apply the macros (they come as is, without any guarantee as usual).

PS: of course, I thought it would be more useful to fanfic writers, but you can use this to post anywhere else on the CBn board (and other boards using BBcode), the BBcode is always the same.

#2 Harry Fawkes

Harry Fawkes

    Lt. Commander

  • Veterans
  • PipPipPip
  • 2229 posts
  • Location:Malta G.C

Posted 10 August 2008 - 07:21 PM

Thanks for that, Mkb. It certainly is good to know and work with I'm sure.

#3 Scrambled Eggs

Scrambled Eggs

    Lt. Commander

  • Veterans
  • PipPip
  • 784 posts

Posted 10 August 2008 - 11:39 PM

Nice work. I'll probably give this a go.

#4 Qwerty

Qwerty

    Commander RNVR

  • Commanding Officers
  • PipPipPipPip
  • 85605 posts
  • Location:New York / Pennsylvania

Posted 12 March 2009 - 03:35 AM

[Moderator's Note: Topic pinned.]