Mtext - creating new lines in VBA

I am attempting to create some Mtext from VBA. The problem I am having is that I can't figure out how to get a new line. There may be a bug in BricsCad.All my efforts only result in one line. There is no word wrap, even if the line is much longer than the text box. Also, if I edit the text, it shows in the edit window just fine, with the new lined I created shown properly. If I manually add a carrage return in the text, it shows in the edit window, but again not on the drawing.I've tried the following as my new line character in the string;VbCrLf, VbCr, Chr(10), VbKeyReturnIf I manually edit the properties of the mtext, and change the text height, and textbox width, the drawing will now wrap the text. Then, when I edit the text, I can manually enter a carrage return.I've run out of ideas of what else to try. Is this a bug or am I just not figuring out the correct character to put into a string for a new line?Joe Dunfee

Comments

  • Joe,I think you should set the Mtext.height to something... For some reason the height is 0and renders the Mtext as you describe. When creating the Mtext by calling the .AddMText function you pass it the Width. Once the Mtext object is created you tweak it's Height as shown below:Dim anMtext As BricscadDb.AcadMTextanMtext= ThisDrawing.ModelSpace.AddMText(InsertionPoint, Width, Text)anMtext.Height = 5anMtext.Update()Hope this helps,regards,Ferdinand

  • Thank you for the effort. But with your code, I get an error message on the line;anMtext= ThisDrawing.ModelSpace.AddMText(InsertionPoint, Width, Text) "Runtimje Error 91, Object variable or With Block variable not set."I did try manually editing the properties of the mtext to give it a height. It did wrap text, and did allow me to manually enter a carriage return in the text. But, existing text would not do a carriage return, dispite having the carriage return inserted in the text string.Joe Dunfee

  • Joe,okyou should of course supply something sensible for Insertionpoint,Width and Text...I suppose your own original code already contains a call to addMText...just after creating the Mtext object modify it's Height property

  • Yes, my original code had the correct variables set. I copied your code into a new module, and tried it there by itself. Here is a copy of what I did. It generated the exact same error message.Note that I am on version 8, could you be on a different version?---------------------Sub InsertText()Dim Width As DoubleDim myText As StringDim MyPoint(0 To 2) As DoubleDim anMtext As BricscadDb.AcadMTextMyPoint(0) = -50MyPoint(1) = 50MyPoint(2) = 0Width = 50myText = "This is a test.This is a test.This is a test.This is a test.This is a test. "anMtext = ThisDrawing.ModelSpace.AddMText(MyPoint, Width, myText)anMtext.Height = 5anMtext.UpdateEnd Sub--------------Joe Dunfee

  • That happens (up to v7 at least) when editing mtext entities - the added text is so small it is barely visible. You have to remove the code which gets added telling it to be smaller than the rest of the paragraph. Don't know VBA, but you don't need VBA to get the problem.

  • we're in VBA (VB6) so object must be intialized with Set...Sorry my mistake change:anMtext = ThisDrawing.ModelSpace.AddMText(MyPoint, Width, myText)toSet anMtext = ThisDrawing.ModelSpace.AddMText(MyPoint, Width, myText)

  • Rather than vbCRLF I use \P for a hard return embedded in the string, ie;"This is a Test.\PThis is a Test."I think these are rich text formating. Would be nice to have alist of some of the more common codes. Not sure where to find said.Jerry

  • I've finally been able to sit down and test out the various ways to work out the problem.Yes, the method of setting the height after creating the mtext [anMtext.Height = 5] solves the problem of the text being all on one line.The /p method does not work, and in fact, causes any text following it to not display. In other words, if the text is "Line One./p Line two." it will display as;Line One.If I edit the text, it will show both lines, with the /p between them.I have however had success with using either chr(10) or VbCrLf.Joe Dunfee

  • Note to Briscsys;The Mtext issue seems to have two separate bugs.;#1, The creation of a Mtext entity should default to the current text height, rather than a text height of 0.#2, There \p code should not cause text to not be displayed. I am uncertain how this affects opening a legacy dwg version, vs a current dwg version.Joe Dunfee

  • \P not /p. Works with your code on my machine. (added Set). If you retreive some mtext.text from an entity create in cad you will see some of the various codes that can be used.Unless I've got some variable setting that is allowing this behaviour on my machine and not your's, you might try again.Jerry

  • Try this:Sub InsertText()Dim Width As DoubleDim myText As StringDim MyPoint(0 To 2) As DoubleDim anMtext As BricscadDb.AcadMTextMyPoint(0) = 50MyPoint(1) = 50MyPoint(2) = 0Width = 120myText = "This is a line of Text.\P\LAnd another Line of Text.\P\fTahoma|b0|i1|c0|p0;\lThis is Tahoma!\P\fTahoma|b1|i0|c0|p0;This is Tahoma Bold!!"Set anMtext = ThisDrawing.ModelSpace.AddMText(MyPoint, Width, myText)anMtext.Height = 5anMtext.UpdateEnd SubJerry

  • Jerry, since your code worked perfectly, I continuted to investigate. It turns out that the letter must be capitalized or it will cause the rest of the line to not be displayed.So, it must be \P, and not \p.Thanks for sticking with me.Joe Dunfee

This discussion has been closed.