I’m considering a code alternative to take BB Code out of text and need help with the “replacing using Wildcards” in VBA in a VBA code….
So I have some text in Word documents that include BB Code Code Tags as well as Word formatting that matches the BB Code
So, for example , what you would actually see is this in the Word document:
[color=Blue]Sub[/color] BBCodeCodeTagsWegDaMit()
.Text = "" [color=Green]' ??? ' Search text in Wildcard Format ??[/color]
I would like to do a couple of similar things to it.
Both are basically removing the BB Code Code tags
_1) One way is automating doing it in Word.
_2) The other, still in a VBA code, is to take it out of the text copied to the clipboard
I have a code that almost does what I want:
Code Part 1)
Rem 1 ) Copies a selected text as it is
Rem 2) Makes a temporary Word document and pastes the selected stuff complete and saves it
Rem 3) I want this just to take out the BB Code, so I would get this back for the above example
Sub BBCodeCodeTagsWegDaMit()
.Text = "" ' ??? ' Search text in Wildcard Format ??
( So in the temporary word document I still have the color format )
I save that to a second temporary Word document "TempNoBBCodeCopy.docx"
( Rem 5) I close / kill those two files currently but I may not do that later )
Code part 2)
This is intended to take the text from the clipboard, remove the BB Code Code tags and then put the simple plain text back in the clipboard. So in the example I would end up with this in the clipboard
Sub BBCodeCodeTagsWegDaMit()
.Text = "" ' ??? ' Search text in Wildcard Format ??
_.....
So I had a go:
I got a lot of the way from here: http://www.eileenslounge.com/viewtopic. ... 03#p175712" onclick="window.open(this.href);return false;
But I am stuck on what I expect is doing things to do with replacing using wildcards. I have no experience with them , and I get a bad headache whenever I try to figure them out.
The full code is :
Sub BBCodeCodeTagsWegDaMit()
and is in this File “eileenslounge.docm”
https://app.box.com/s/mtwz5jil0p5nbymqrb1cgszjuyg94bea" onclick="window.open(this.href);return false;
It is in the Normal Code Module
NormalCodesBackUp
Below is a stripped down versions with just bits I am stuck on:
Code part 1) I expect I need just the correct wildcard bit here
.Text = "" ' ??? ' Search text in Wildcard Format ??
‘Possibly this is more subtle and I need like
.Text = [AnyText]ThisText[/AnyText]
‘and also to go with that
.Replacement.Text = ThisText
Code part 2) I am not sure what I need here, - basically I would like "Raped Text" to be what is in the variable TextWithBBCodeEnit but with all BB Code stuff removed. ( I know about Replace stuff and can manipulate strings quite well but doing it with wild cards I am unfamiliar with. ).
_- I am sure I can do it in a complicated way, like working backwards from the string end, looking for a combination of ] , [/ , ] and [ , and deleting those and all characters between them. I have done that a lot before, and I am having a go with that just now… . But it is about time I learnt how to do it properly…. But trying to figure out Wildcards make me go a bit ( more ) crazy…. (
Thanks
Alan
Shortened code:, ( where I need help is shown twice thus 'HELP !!!===Please )
Code: Select all
Sub BBCodeCodeTagsWegDaMitSHimpfGlified()
Rem Code Part 1) make two temporary Word Files with and withouut BB Code
Rem 1) Copy selection to Clipboard
Selection.Copy
Rem 2) Make temporary WORD document
Documents.Add: ActiveDocument.Content.Paste
' 2b) Copy of Full Text with BB Code
Dim FullFilePathAndFullNameBBCode As String
ActiveDocument.SaveAs FileName:="TempBBCodeCopy.docx", FileFormat:=wdFormatXMLDocument
Let FullFilePathAndFullNameBBCode = ActiveDocument.Path & "\" & ActiveDocument.Name
Rem 3) From Han's Text Find Replacement Dialogue 'http://www.eileenslounge.com/viewtopic.php?f=26&t=22603#p175712
' 3a) Take out all BB Code
Selection.WholeStory
With Selection.Find
.ClearFormatting: .Replacement.ClearFormatting
.Wrap = wdFindStop
'.MatchWildcards = False ' Don't use wildcards. The default anyway, but in this code is an important ???
.MatchWildcards = True ' ??? need this
'HELP !!!===Please
.Text = "" ' ??? ' Search text for BB Code Code Tags in Wildcard Format ??
.Replacement.Text = "" ' Replace text is nothing. ?? Or more subtle
'=================
.Execute Replace:=wdReplaceAll
End With
' 3b) Copy of Colored Text without BB Code Code tags
Dim FullFilePathAndFullNameNoBBCode As String
ActiveDocument.SaveAs FileName:="TempNoBBCodeCopy.docx", FileFormat:=wdFormatXMLDocument
Let FullFilePathAndFullNameNoBBCode = ActiveDocument.Path & "\" & ActiveDocument.Name
Rem 4) "Reset the "Find Replace Text Dialogue" "Thing" "
ActiveDocument.Select
Selection.WholeStory
With Selection.Find
.ClearFormatting: .Replacement.ClearFormatting: .Text = "": .Replacement.Text = "": .Forward = True: .Wrap = wdFindAsk: .Format = False: .MatchCase = False: .MatchWholeWord = False: .MatchKashida = False: .MatchDiacritics = False: .MatchAlefHamza = False: .MatchControl = False: .MatchWildcards = False: .MatchSoundsLike = False: .MatchAllWordForms = False '
End With
Rem 5) Option to close / kill document
ActiveDocument.Close (wdDoNotSaveChanges)
Kill FullFilePathAndFullNameBBCode
Kill FullFilePathAndFullNameNoBBCode
Rem Code Part 2 Put text without BB code into clipboard
Rem 6) Use data Object ( assumes we copied to the Clipboard )
'1 a) Data Object to get the data from the clipboard.
Dim objCliTextCopied As Object ' Late Binding equivalent'
Set objCliTextCopied = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}") '
'6a) Put all Clipboard Infomation into Data Object
objCliTextCopied.GetFromClipboard
'6b) get code as long string String. This can be very long
Dim TextWithBBCodeEnit As String '
Let TextWithBBCodeEnit = objCliTextCopied.GetText() ''retrieve the text
Rem 8) Take out BB Code bits from text string.
Dim RapedText As String
'HELP======= Please :)
Let RapedText = "Raped Text" ' I want here what is in TextWithBBCodeEnit but with BB Code Code tags removed
'=====================
Rem 9) This a Another Object from class to be sure we have the data in the Clipboard
Dim objDat As Object
Set objDat = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
'9b) Put in (and Get back from clipboard) the raped text.
objDat.SetText RapedText
objDat.PutInClipboard
End Sub