Macro to set Spacing Before

Started by YossiD, March 07, 2017, 03:10:28 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

YossiD

I have tried recording a macro to set paragraph spacing before (Format>Paragraph>Text>Spacing Before), but it doesn't work.

Here's the code that Visio recorded.

Sub SpaceBeforePara_3()
'
' Insert 3 points leading before paragraph
'
' Keyboard Shortcut: Ctrl+a'

    Dim UndoScopeID2 As Long
    UndoScopeID2 = Application.BeginUndoScope("Text Properties")
    Dim vsoCharacters1 As Visio.Characters
    Set vsoCharacters1 = Application.ActiveWindow.Page.Shapes.ItemFromID(308).Characters
    vsoCharacters1.Begin = 13
    vsoCharacters1.End = 27
    vsoCharacters1.ParaProps(visSpaceBefore) = 3#
    Application.EndUndoScope UndoScopeID2, True

End Sub


Can someone tell me how to make it work? I'm pretty sure it's rather simple for those fluent in VBA.

Thanks

Yacine

Sub Macro1()
Dim shp As Shape
    For Each shp In ActiveWindow.Selection
        shp.CellsSRC(visSectionParagraph, 0, visSpaceBefore).FormulaU = "3pt"
    Next shp
End Sub


Not sure what "#3" in your example stands for.
Yacine

wapperdude

In defense of the macro recorder, in this case either 3# or "3 pt" will work.  I think, "3 pt" is more obvious, though.

Wapperdude
Visio 2019 Pro

YossiD

Thanks Yacine. Your macro works, but it adds the Space Before to all paragraphs of text in the shape and I want to add it only to the paragraph where the cursor is. Can that be done?

wapperdude

Don't think you can get  position of the cursor as you indicate...inside a shape.  But, I did find this code  by JuneTheSecond.  Basically, you select a single character at location you want the added space.


Dim vsoCharacters1 As Visio.Characters
Set vsoCharacters1 = ActiveWindow.SelectedText
MsgBox vsoCharacters1.Begin & " " & vsoCharacters1.End

'Add remaining paragraph spacing center here...without the char.begin/.end lines



This seems like it ought to work.  Haven't tried it.  The comment is mine, not JuneTeSecond's.

Wapperdude
Visio 2019 Pro

wapperdude

#5
Had a chance to try the code, it works.  Tried a variation, using two line feeds.  Works too.  Both macros shown below.  That makes 3 macros available to use.


Sub MyMac1()
' Turns out, only need to place the text cursor, do not need to make an actual selection.
' This code adds two line feeds after the text cursor insertion point.  Assumes no paragraph present.  Otherwise,
' modify the code to just add one line feed.
' Amount of spacing is defined by the line to line spacing.


    Dim vsoChars1 As Visio.Characters
   
    Set vsoChars1 = ActiveWindow.SelectedText
'    Debug.Print vsoChars1.Begin & "  " & vsoChars1.End
   
'    vsoCharacters1.Text = "" & Chr(10) & Chr(10) & ""
    vsoChars1.Text = Chr(10) & Chr(10)
   
End Sub

Sub MyMac2()
' Adding the space before or after the selected text depends upon the location of the
' selection.  Paragraphs must exist to see results.
' It is only necessary to select the 1st character of the paragraph of interest. 
' That's all.  Just the single, leading character.  The code allows specification of spacing size

    Dim vsoChars3 As Visio.Characters
   
    Set vsoChars3 = ActiveWindow.SelectedText
    vsoChars3.ParaProps(visSpaceBefore) = 6#
'    vsoChars3.ParaProps(visSpaceAfter) = 2#       'Optional to set following paragraph's spacing

End Sub


Wapperdude
Visio 2019 Pro

YossiD

wapperdude comes through again! Where do you find the time?

These examples are very useful.

As you say, selecting only a single character is sufficient to set the SpaceBefore for a single paragraph of text in a shape, which is exactly what I need.

I find it strange that Visio's VBA does not have Selection functions as in VBA for Word. I suppose dealing with text is considered less important in Visio. Can't really argue with that.

Thanks for your help.

I really must find the time to take a VB/VBA course one of these days; getting macros to work is great fun.

wapperdude

Quotewapperdude comes through again! Where do you find the time?
I'm retired.  🙌

I suppose there are general classes, books on VBA to get basic concepts and terms.  There's a lot for Excel, not very much for Visio.  I find the macro recorder most useful...covers most everything I try, but not all.  Look at examples.  Just explore...see what people do, why.  Then the internet and SDK are good resources.  And, of course, I keep examples, both Visio files and just text...usually code snippets.  A lot of it is trial and error.

The Visio object model is different than Word, Excel.  The model gets updated with new releases.  Foe example, now there are path related objects, improved connectivity objects.

Wapperdude
Visio 2019 Pro

YossiD

Most of my macros are for Word and I generally manage starting with a recorded macro and then a bunch of trial and error, internet searches, and help from forums. The problem is that it's a time consuming process and I often don't have the luxury of doing it. Success is always very satisfying, but the time invested does not always justify the time saved, unless I can be sure I will be reusing the macro in the future.

I get annoyed about a lot of what Microsoft does (what were they thinking with the ribbons), but the macro recorder is a stroke of genius. Of course it frequently doesn't record things the way I want, but I agree that it's always a good starting point.

Thanks for your ongoing assistance. I guess that you, like me, get a kick out of helping people.

Paul Herber

The macro recorder was part of Visio long before Microsoft bought it in 2000.
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/