Line break relative to current location (VBA)

cocobouuu -  
 cocobouuu -
Hello :)

I am encountering the following little problem: I wrote a macro that allows me to insert a quick part at the current location, however, when I insert several quick parts in a row (in this case, tables), they overlap, whereas I would like them to be placed one after the other instead of being superimposed. I think I need to add line breaks, but I haven't found a VBA code that allows me to insert a line break at the current location.

For inserting the quick part, here is the code (my quick part, a table, is called test):

Private Sub Insert_Click()
Templates.LoadBuildingBlocks ' loading the building blocks template
' searching for the right template
For Each docModele In Templates
If docModele.Name = "Building Blocks.dotx" Then
' if found, insert the table of contents block
Templates(docModele.FullName).BuildingBlockEntries("test"). _
Insert Where:=Selection.Range, RichText:=True
Exit For
End If
Next
End Sub

Wishing you a pleasant weekend :p

Configuration: Windows / Chrome 58.0.3029.110

4 answers

  1. m@rina Posted messages 27616 Registration date   Status Moderator Last intervention   11 563
     
    Hello

    First of all, I don't understand your loop and your IF... since you know the template, you might as well name it directly.

    Next, tables only overlap if they have wrapping. If wrapping is unnecessary, it shouldn't be applied, it's always more complicated. So start by recreating your blocks without wrapping and name your template.

    m@rina

    --
    Be careful with herbal tea: it's a dangerous poison...
    1
  2. cocobouuu
     
    Hello and thank you for your response!

    I'm having trouble inserting a quick part without the IF loop; if I go directly to retrieve the object :S with the following code, Word doesn't find it:
    Private Sub Insert_Click()
    Templates.LoadBuildingBlocks ' loading the building blocks template
    Templates(docModele.FullName).BuildingBlockEntries("test"). _
    Insert Where:=Selection.Range, RichText:=True
    End Sub

    Also, my tables have no wrapping. I may not have expressed myself clearly, but basically, when I insert several quick parts of table type (named "test") one after the other, instead of creating multiple tables one below the other, it creates a single table where rows keep getting added with each click.

    If you have any suggestions, I would be very, very interested ^^

    Have a great end of the day :)
    0
  3. m@rina Posted messages 27616 Registration date   Status Moderator Last intervention   11 563
     
    Hello

    If your block is in the building blocks.dotx, it is not in the document template.

    You need to specify the location explicitly, for example:
    path = Environ("USERPROFILE") & "\"AppData\Roaming\Microsoft\Document Building Blocks\1036\16\Building blocks.dotx"
    Application.Templates(path).BuildingBlockEntries("test").Insert Where:=Selection.Range, RichText :=True

    As for your tables, we can insert a line break, but why not add the line break to the block? It's simpler.

    m@rina

    --
    Beware of herbal tea: it's a dangerous poison...
    0
  4. cocobouuu
     
    Hi m@rina, and thank you for your response :) When I try to add a line break with my table, the QuickParts insert tab becomes grayed out, which is why I can't do it ^^ If you have another idea, it would be welcome.
    0