+ Reply to Thread
Results 1 to 9 of 9

Problems creating a shape textbox in word 2010

  1. #1
    Forum Contributor
    Join Date
    11-15-2012
    Location
    Buffalo, NY
    MS-Off Ver
    Office 365
    Posts
    319

    Problems creating a shape textbox in word 2010

    I'm trying to create a set of nested textboxes in Word 2010. I can do it using the UI, but I need to be able to do this programmatically. What I'm trying to do is to create the 'main' text box (called QuestionBox), and then after adding some text (QuestionText) to create some textboxes inside the QuestionBox (let's call them ResponseBox1 .. ResponseBox3. Each of ResponseBox contains text, possibly on more than one line.
    I also want to be able to add a shape outside the response box, but inside the QuestionBox - the shape is either a small circle or a small square. (Yes - they are replacements for a RadioButton and a CheckBox ;-)

    So far I have VBA code that (seems to) create the QuestionBox, but there is nothing visible - even if I set the visible attribute to true.
    Please Login or Register  to view this content.
    This is incredibly frustrating, as I can't even use Macro Recorder to record the steps of creating and formatting the text box. I turn on recording, and end up with the following (pretty useless, but syntactically perfect) code:

    Please Login or Register  to view this content.
    Any takers?

    Thanks,

    Tony

  2. #2
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,856

    Re: Problems creating a shape textbox in word 2010

    Hi Tony,

    Had it occurred to you that you could probably get similar results using a table with text wrapping?
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Forum Contributor
    Join Date
    11-15-2012
    Location
    Buffalo, NY
    MS-Off Ver
    Office 365
    Posts
    319

    Re: Problems creating a shape textbox in word 2010

    Thanks, Paul

    That had not occurred to me, so I gave it a try. So now I'm trying to manage a table in Word 2010, using VBA.
    All the rows except the first row of the table need to be set to the same height, but it seems not to be working.

    I try (initially) creating the rows with no specifications for row height except defaults. After creating the table I manually change the contents of the rows, so that some of them take more space. After this, the value returned by row.height seems not to have changed. Here is a table (!) of the results

    Code:

    HeightRule InitialHeight Height value returned
    none none 99999 (Seems to be wdAuto by default - the modified rows expand visually)
    wdAtLeast 12 12 (For every row - even the 'bigger' ones - the modified rows expand visually)
    wdExactly 12 12 (For every row - even the bigger ones - all the extra lines remain invisible)
    wdAuto 12 12 (For every row - even the bigger ones - the modified rows expand visually)

    So - in some instances (wdAuto, wdAtLeast) Word is able to adjust the visual aspect of the row, even while insisting that it is exacly the same height that it was set to originally. There must be some new hidden property, and that's the one I'd like to find. I suppose it would be called:
    HeightThatIAppearToBeEvenThoughIStubbornlyReportSomeOtherValue.

    Any ideas?
    Tony

    This is the lab code that I'm using
    Code:

    Sub BuildQuestionTable(QuestionIndex As Integer, QuestionText As String, NumResponses As Integer)

    Const NumColumns = 2
    Const QuestionMargin = 0.5
    Const QuestionIndent = -0.5
    Const ResponseMargin = 0.75
    Const ResponseIndent = -0.25

    Dim Range As Range
    Dim Table As Table
    Dim NumRows As Integer
    Dim r As Integer
    Dim MaxHeight As Single

    ' Construct a table with two columns and n+1 rows, where n is the number of
    ' ResponseLabel entries. (If n=0, then create 2 rows)

    If NumResponses > 0 Then
    NumRows = NumResponses + 1
    Else
    NumRows = 2
    End If

    Set Range = ActiveDocument.Content
    Range.Collapse Direction:=wdCollapseEnd
    Set Table = ActiveDocument.Tables.Add(Range, NumRows, NumColumns)
    Debug.Print "Created new table with "; NumColumns; " columns, and "; NumRows; " rows"

    ' There are no borders visible

    For r = 1 To NumRows
    With Table.Cell(r, 1)
    If r = 1 Then
    .Range.ParagraphFormat.LeftIndent = InchesToPoints(QuestionMargin)
    .Range.ParagraphFormat.FirstLineIndent = InchesToPoints(QuestionIndent)
    .Range.ParagraphFormat.SpaceBefore = 0
    .Range.ParagraphFormat.SpaceAfter = 0
    .Range.Text = Format(QuestionIndex) + vbTab + QuestionText
    Else
    .Range.ParagraphFormat.LeftIndent = InchesToPoints(ResponseMargin)
    .Range.ParagraphFormat.FirstLineIndent = InchesToPoints(ResponseIndent)
    .Range.ParagraphFormat.SpaceBefore = 0
    .Range.ParagraphFormat.SpaceAfter = 0
    .Range.Text = "#" + Format(r - 1) + vbTab + "This is a response"
    End If
    End With
    ' Table.Rows(r).HeightRule = wdRowHeightAuto
    ' Table.Rows(r).HeightRule = wdRowHeightAtLeast
    ' Table.Rows(r).HeightRule = wdRowHeightExactly
    ' Table.Rows(r).Height = 12
    Next r
    Debug.Print "Loaded data into rows 1 through "; NumRows

    ' Now set the heights of each of the rows to the same value
    ' First determine the max height

    MsgBox "Adjust the contents of the rows, then resume"

    MaxHeight = 0
    r = 0
    While r < NumRows
    r = r + 1
    With Table.Rows(r)
    Debug.Print "Response " + Format(r) + " has height of "; Format(.Height) + " points"
    If .Height > MaxHeight Then
    Debug.Print "Setting MaxHeight to " + Format(.Height) + " points"
    MaxHeight = .Height
    End If
    End With
    Wend
    ' Now establish it
    r = 1
    While r < NumRows
    r = r + 1
    Table.Rows(r).Height = MaxHeight
    Wend
    ' Lastly, add borders all around

    r = 0
    While r < NumRows
    r = r + 1
    With Table.Rows(r)
    .Borders.OutsideColor = wdColorBlack
    .Borders.OutsideLineStyle = wdLineStyleSingle
    .Borders.OutsideLineWidth = wdLineWidth100pt
    .Borders.InsideColor = wdColorBlack
    .Borders.InsideLineStyle = wdLineStyleSingle
    .Borders.InsideLineWidth = wdLineWidth100pt
    End With
    Wend

    End Sub

    Sub TestTable()

    BuildQuestionTable 23, "This is the text of question 23", 3
    MsgBox "Check table and resume to delete it!"

    ActiveDocument.Tables(ActiveDocument.Tables.Count).Delete

    End Sub

  4. #4
    Forum Contributor
    Join Date
    11-15-2012
    Location
    Buffalo, NY
    MS-Off Ver
    Office 365
    Posts
    319

    Re: Problems creating a shape textbox in word 2010

    Sorry about the munged up formatting there, still suffering from Newbieitis!

    T

  5. #5
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,856

    Re: Problems creating a shape textbox in word 2010

    The row heights will have a minimum value based on the fonts you're using.

    I gather you're trying to set them to 12pt, except for the first row - which has to be suffiiciently high to accommodate the question. Try the following:
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    11-15-2012
    Location
    Buffalo, NY
    MS-Off Ver
    Office 365
    Posts
    319

    Re: Problems creating a shape textbox in word 2010

    Hi, Paul

    Thanks for your response. The problem is a little bit trickier than that.All of the rows need to have their heights adjustable. The first row - the QuestionRow needs to be able to accommodate an arbitrary amount of text. The ResponseRows are the problem. Some will be a single line, others will be multiple lines, and I don't know which will be what. All of this material is coming out of a database, so it's not visible to the user. This is why I cycle through the rows twice. First time to identify the maximum height of all the response rows, second time to set all the response rows to that value.

    I have seen the wdVerticalPositionRelativeToPage technique used elsewhere (maybe even from you), but it fails to handle the last row (primarily because there is no row after the last row from which the calculation of vertical difference on page can be made. It's important that the ResponseRows be adjusted to be all the same height, and that height be calculable accurately. These figures are then used to position a "control" onto the page, just to the right of the response, on the centerline of the Response cell.

    What is mind-numbingly annoying is that because Word is pretty good about adjusting what the actual visible dimensions of the row are, these dimensions must be a) calculable and b) known somewhere inside Word. Given that, why Microsoft chooses not to make these values available is an act of customer-hostility that defies description. What is equally frustrating is that if I manipulate the table through the UI, then all of the values are available (we're getting closer, but I'm doing all this programmatically to avoid having to manage every step manually) and reflect the internal reality.

    I'm beginning to think that I'm going to drop the possibility of creating hard copy (via Word), and limit this project to web-only access. I think massaging HTML will be a whole load easier!

    Tony

    BTW What are the code delimiting brackets on this forum?

  7. #7
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,856

    Re: Problems creating a shape textbox in word 2010

    Hi Tony,

    If you want all the rows to be able to expand to accommodate whatever is inserted into them, simply use 'wdRowHeightAuto' property (and don't specify a height) or the 'wdRowHeightAtLeast' property instead of the 'wdRowHeightExactly'.

    As for the 'wdVerticalPositionRelativeToPage' technique, you'll see that I'm not trying to measure the row height, but the additional height of the text within it, where a minimum height of 12pt was subsequently specified. As implemented, that approach will happily work on all rows in the table.

    The code tags are inserted via the button with the # symbol on it.

  8. #8
    Forum Contributor
    Join Date
    11-15-2012
    Location
    Buffalo, NY
    MS-Off Ver
    Office 365
    Posts
    319

    Re: Problems creating a shape textbox in word 2010

    Hi Paul,

    Well, apologies for not looking more carefully at your code - I saw the wdVerticalPositionRelativeToPage constant, and assumed (silly me) that it was the "calculate the height as the difference between two rows. Now that I do what I should have done, I think that you have actually given me the solution I've been seeking.

    Please Login or Register  to view this content.
    This allows us to determine the MaxHeight on any of the response rows. Then:
    Please Login or Register  to view this content.
    This should do it, yes?

    The need was to determine the MaxHeight of rows 2..n, and then to make rows 2..n all the same height - MaxHeight.
    Thanks, Paul. Takeaway is always read the code offered up by an expert!

    Tony

  9. #9
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,856

    Re: Problems creating a shape textbox in word 2010

    Hi Tony,

    So your aim is to set all rows, except row 1 to the same height? In that case, try:
    Please Login or Register  to view this content.
    PS: You'll note that I've used Rng and Tbl as the variable names, instead of Range and Table. It is inadvisable to use vba property names as variable names.
    Last edited by macropod; 12-17-2012 at 11:40 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1