Results 1 to 11 of 11
  1. #1
    5 Star Lounger
    Join Date
    Oct 2002
    Location
    Wellington, Wellington, New Zealand
    Posts
    621
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to post Macro code

    Folks - what is the best method to post VBA Macro code to a board so that it preserves formating?

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: How to post Macro code

    You have two choices.
    <pre> You can put the code between <!t>[PRE]<!/t> and <!t>[/PRE]<!/t> tags like this
    That preserves all your tabs and indents very nicely
    but if the user copies your code and posts it into VBA then
    all the newlines get thrown away.
    so the user has to first paste into Word and then copy again
    </pre>

    Or you can put the code directly in the post
    Without any tags
    But including the correct line breaks
    And the user can then copy and paste
    But your indents get lost.

    StuartR

  3. #3
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to post Macro code

    You can put <!t>[tab]<!/t> in to preserve indentation- though it can be a bit of a pain.

    Another way is to attach a txt file.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  4. #4
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: How to post Macro code

    I'd never noticed <!t>[tab]<!/t> before. It does a good job, and pastes into VBA very nicely.

    It's trivially easy to copy code from VBA to word and replace 4 spaces with <!t>[TAB]<!/t> to give this effect.

    For i = iStart To Len(strSource)
    strNextChar = Mid(strSource, i, 1)
    If (Not blInToken) And strNextChar = "<" Then
    i = i - 1
    Exit For
    End If
    strNextToken = strNextToken & strNextChar
    If (blInToken And strNextChar = ">") Then
    Exit For
    End If
    Next i

    StuartR

  5. #5
    5 Star Lounger
    Join Date
    Oct 2002
    Location
    Wellington, Wellington, New Zealand
    Posts
    621
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to post Macro code

    Thanx people that'll make life easier for others

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

    Re: How to post Macro/VBA code

    Till I saw this question recently I've always used <!t>[PRE]<!/t> tags to post code, I didn't realize that when you copied & pasted PRE formatted code into VB Editor that the new line characters were lost. Quick test proved otherwise. Not too useful for lengthy code examples that someone may want to copy & test. So now, except for short lines of code, as suggested above, I copy code into Word, format, then copy into post. To facilitate proper indentation I recorded simple Word "macro" to replace 4 spaces with <!t>[TAB]<!/t> tag (assumes VBE options "Tab Width" is set to 4):

    Sub FormatCode()
    Selection.HomeKey Unit:=wdStory
    Selection.EndKey Unit:=wdStory, Extend:=wdExtend
    With Selection.Font
    .Name = "Courier New"
    .Size = 10
    .Bold = False
    .Italic = False
    End With
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = " "
    .Replacement.Text = "<!t>[tab]<!/t>"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.HomeKey Unit:=wdStory
    End Sub

    I am not too proficient at Word VBA so above code can probably be greatly simplified, "Macro" recorder tends to record a lot of superfluous instructions. The PRE formatted code is easier to follow, maybe there is some other HTML tag that can be used for this purpose that preserves code formatting without "losing" the new line characters when pasted in code editor.

  7. #7
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to post Macro/VBA code

    I think that there may be some other reasons for using pre tags. For example, some combination of things that can be found in code can be mistaken for HTML tags and the code displayed will not the samd as the code you posted. I always post code and formula in pre tags.
    Legare Coleman

  8. #8
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to post Macro/VBA code

    <hr>I copy code into Word, format, then copy into post. <hr>
    Mark,

    No need to format in Word. Copt any code in Pre tags into Word, then copy again immediately, and paste into th VB Editor. There's no need for using a tab tag then either.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

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

    Re: How to post Macro/VBA code

    That's a good point, I'm aware that stuff like field names in brackets and more than one space in a text string may not display properly if PRE tags not used. I've seen code posted that had smilies in middle of code because poster didn't realize the term in brackets was a smilie tag.

    One more question: Is there a way to format a HTML character code so it is displayed as a literal value and not as the character it represents? For example, if you enter & # 36; (without spaces) in Reply to box, then preview reply, a dollar sign is displayed as expected, but the HTML code is also changed to dollar sign in Reply to box. At least it does in IE6. Using PRE tags does not seem to prevent this behavior. So how would you post code that involved use of HTML character codes?

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

    Re: How to post Macro/VBA code

    Thanx, further experimentation indicates WordPad will also preserve formatting, but Notepad will not. So as a word processor, the VB code editor is apparently more akin to Notepad and other simple text editors.

  11. #11
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: How to post Macro/VBA code

    I'm not an HTML expert, so the only way I know is to use pre tags.
    Legare Coleman

Posting Permissions

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