Creating, formatting, arranging, and grouping Drawing objects using VBA and Word

Braillepro

New Member
Joined
Apr 25, 2016
Messages
1
Hello Everyone,

I've gotten lots of help from these forums, but I've never needed to post before. Maybe this means I'm getting 'better' at vba because I'm attempting more ambitious projects.

I feel like I've "beaten the system" whenever I figure out how to cause a computer to do some of the more tedious chores with which I'm tasked, to wit:

I make tactile graphics for VI students using Word.

Every day, among other things, I use Word's shapes and drawing tools to make many different grids with various line styles and weights arranged precisely with labels in text boxes.

I've started to imagine a world, where I wouldn't have to repeatedly: draw, format, duplicate, arrange, align, group, rotate, un-group, reformat, regroup, move to back, move to front, flip, distribute, etc from scratch for every page.

I wrote this thingee to assist, and it kinda works, but it's not perfect and the results are difficult to work with because the elements are tedious to select when they're not grouped sensibly.

My problem is that I'm not really a coder. I don't know the best process for coming up with code, and, I don't even know enough about what's possible with vba to make a decent plan for the project. Here's my prototype:

Code:
Sub Grid()


' Grid Macro


    
    Dim i As Integer
    Dim Offset As Integer
    Dim HLines As Integer
    Dim VLines As Integer
    Dim GridLine As LineFormat
    Dim TickMark As LineFormat
    Dim HOriginLine As Integer
    Dim VOriginLine As Integer
    Dim XBeg, YBeg, XEnd, YEnd As Single
    'Dim CanvasWidth As Integer
    'Dim CanvasHeight As Integer
    
    ' CanvasWidth = 700
    ' InputBox("How wide is your drawing area? (800 points is full width.)", "Canvas Width")
    ' CanvasHeight = 700
    ' InputBox("How tall is your drawing area? (700 points is full height.)", "Canvas Height")
    
    HLines = InputBox _
        ("How many Horizontal lines are in your grid?", "Horizontal Lines")
    HOriginLine = InputBox _
        ("On which line, counting from the top, is the origin line?", "Horizontal Origin")
    VLines = InputBox _
        ("How many Vertical lines are in your grid?", "Vertical Lines")
    VOriginLine = InputBox _
        ("On which line, counting from the left, is the origin line?", "Vertical Origin")
    
    XBeg = 50
    YBeg = 50
    XEnd = VLines * 20
    YEnd = YBeg


'   Draw the Horizontals
    
    For i = 1 To HLines
        
        Offset = 20 * (i - 1)
        
        Select Case i
            Case HOriginLine
                Set GridLine = ActiveDocument.Shapes.AddLine _
                    (XBeg - 10, YBeg + Offset, XEnd + 10, YEnd + Offset).Line
                    With GridLine
                        .Weight = 3
                        .ForeColor = RGB(0, 0, 0)
                        .BeginArrowheadLength = msoArrowheadLong
                        .BeginArrowheadStyle = msoArrowheadOpen
                        .EndArrowheadLength = msoArrowheadLong
                        .EndArrowheadStyle = msoArrowheadOpen
                    End With
            Case 1, HLines
                Set GridLine = ActiveDocument.Shapes.AddLine _
                    (XBeg, YBeg + Offset, XEnd, YEnd + Offset).Line
                    With GridLine
                        .Weight = 1
                        .ForeColor = RGB(224, 224, 224)
                    End With
            Case Else
                Set GridLine = ActiveDocument.Shapes.AddLine _
                    (XBeg, YBeg + Offset, XEnd, YEnd + Offset).Line
                    With GridLine
                        .Weight = 1
                        .ForeColor = RGB(224, 224, 224)
                    End With
                Set TickMark = ActiveDocument.Shapes.AddLine _
                    (XBeg + ((VOriginLine - 1) * 20) - 12, YBeg + Offset, _
                    XBeg + ((VOriginLine - 1) * 20) + 12, YEnd + Offset).Line
                    With TickMark
                        .Weight = 3
                        .ForeColor = RGB(0, 0, 0)
                    End With
        End Select


    Next i


'   And now, the verticals
   
    XEnd = XBeg
    YEnd = HLines * 20


     For i = 1 To VLines
        Offset = 20 * (i - 1)
        Select Case i
            Case VOriginLine
                Set GridLine = ActiveDocument.Shapes.AddLine _
                    (XBeg + Offset, YBeg - 10, XEnd + Offset, YEnd + 10).Line
                    With GridLine
                        .Weight = 3
                        .ForeColor = RGB(0, 0, 0)
                        .BeginArrowheadLength = msoArrowheadLong
                        .BeginArrowheadStyle = msoArrowheadOpen
                        .EndArrowheadLength = msoArrowheadLong
                        .EndArrowheadStyle = msoArrowheadOpen
                    End With
            Case 1, VLines
                Set GridLine = ActiveDocument.Shapes.AddLine _
                    (XBeg + Offset, YBeg, XEnd + Offset, YEnd).Line
                    With GridLine
                        .Weight = 1
                        .ForeColor = RGB(224, 224, 224)
                    End With
            Case Else
                Set GridLine = ActiveDocument.Shapes.AddLine _
                    (XBeg + Offset, YBeg, XEnd + Offset, YEnd).Line
                    With GridLine
                        .Weight = 1
                        .ForeColor = RGB(224, 224, 224)
                    End With
                Set TickMark = ActiveDocument.Shapes.AddLine _
                    (XBeg + Offset, YBeg + ((HOriginLine - 1) * 20) - 12, _
                    XBeg + Offset, YBeg + ((HOriginLine - 1) * 20) + 12).Line
                    With TickMark
                        .Weight = 3
                        .ForeColor = RGB(0, 0, 0)
                    End With
        End Select
    Next i


End Sub

This draws most of the elements I need, close to where they need to be, but I need to work further to finish them. It would be great if all the verticals plus the horizontal ticks would be grouped, and all the horizontals with the vertical ticks. then I could more easily select and adjust manually.

I need to add text boxes labeling the tick marks. Id love to add them programatically as well, but even generating the appropriate number aligned and grouped appropriately would be a great boon.

As well, maybe you can see that I was playing with the idea of custom offsets based on the number of lines and the size of the drawing area, but since I didn't know how to sensibly group the components, this version seeks to make my code fiddling easier.

Is what I have done an appropriate solution to my problem?

Would it be better if I had created the lines outside of the loop, and used the loops merely to place them? (I'm trying after posting this.)

Maybe I'll build a dialog with buttons calling routines that paste the six different lines, (grid lines with ticks for the body, grid lines without ticks for the outside lines, and heavier origin lines ,in both dimensions), using variables to keep track of the Xs and Ys and Zs.

I'd appreciate any feedback at all, except for slamming my primitive coding abilities.

Thanks for reading.:help:

BP
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top