none
Creating quick parts with VBA RRS feed

  • Question

  • I have tried to find an answer to my question without any success, so apologies if it has been asked before.

    I have an outlook message table containing a table, that I want to parse using vba to create or update quick part entries.

    The table looks something like this :

    +-----+--------------+--------------+
    | xxx |       A      |       B      |
    +-----+--------------+--------------+
    |  1  |   sampleA1   |   sampleB1   |
    +-----+--------------+--------------+
    |  2  |   sampleA2   |   sampleB2   |
    +-----+--------------+--------------+
    |  3  |   sampleA3   |   sampleB3   |
    +-----+--------------+--------------+

    My objective is to generate quick parts whose ID will be for example "xxxA1" and the corresponding text "sampleA1".

    The ID will be constructed from the first cell of the table and the row and column headers, and the values will be the corresponding cell contents.

    I have got as far as being able to read table and store the ID/Value pairs in an array, but I can't find how to add them to the quick parts library:

    My code so far looks like this.

    Sub ParseTable2QuickParts()
        Dim objOL As Outlook.Application
        Dim objNS As Outlook.NameSpace
        Dim objDoc As Object 'Word.Document
        Dim objSel As Object 'Word.Selection
    
        Dim cTitle As String
        Dim acRow(10) As String
        Dim nRow As Integer
        Dim acCol(10) As String
        Dim nCol As Integer
        Dim acValues(10, 10) As String
    
        On Error Resume Next
        Set objOL = Application
        If objOL.ActiveInspector.EditorType = olEditorWord Then
            Set objDoc = objOL.ActiveInspector.WordEditor
            Set objNS = objOL.Session
    
            Set objSel = objDoc.Windows(1).Selection
    
            objSel.Move wdstory, -1
            objSel.Move wdTable, 1
            objSel.Expand 12
            cTitle = Left(objSel.Text, Len(objSel.Text) - 2)
    
            For nCol = 2 To 3
               objSel.Move wdRow, 0
               objSel.Move wdCell, 1
               objSel.Expand 12
               acCol(nCol) = Left(objSel.Text, Len(objSel.Text) - 2)
            Next
    
            For nRow = 2 To 4
               objSel.Move wdRow, 1
               objSel.Move wdCell, 0
               objSel.Expand 12
               acRow(nRow) = Left(objSel.Text, Len(objSel.Text) - 2)
            Next
    
            objSel.Move wdstory, -1
            objSel.Move wdTable, 1
            For nRow = 2 To 4
                objSel.Move wdRow, 1
                For nCol = 2 To 3
                    objSel.Move wdCell, 1
                    objSel.Expand 12
                    acValues(nCol, nRow) = Left(objSel.Text, Len(objSel.Text) - 2)
                Next
            Next
    
            objSel.Move wdstory, 1
            For nRow = 2 To 4
                For nCol = 2 To 3
                    objSel.InsertAfter cTitle & acCol(nCol) & acRow(nRow) & " => " & acValues(nCol, nRow) & vbCrLf
                Next
            Next
    
        End If
        Set objOL = Nothing
        Set objNS = Nothing
    
    End Sub

    I just need to replace the final nested loops to store what I've read rather than just dump it back to the message. Can anyone point me in the right direction ?

    Thanks



    • Edited by ChrisEnn Wednesday, June 17, 2015 3:54 PM
    • Moved by L.HlModerator Thursday, June 18, 2015 2:03 AM
    Wednesday, June 17, 2015 3:38 PM

Answers

  • Hello Chris,

    The Add method of the BuildingBlockEntries class creates a new building block entry in a template and returns a BuildingBlock object that represents the new building block entry.

    Sub AddBBProgramatically()
    Dim oRng As Word.Range
    Dim oTmp As Template
      Set oRng = Selection.Range
      Set oTmp = Document.AttachedTemplate 
      oTmp.BuildingBlockEntries.Add _
        Name:="New BB", Type:=wdTypeQuickParts, Category:="New Category", _
        Range:=oRng, InsertOptions:=wdInsertContent
    lbl_Exit:
      Exit Sub
    End Sub

    I'd suggest asking Word specific questions on the Word for Developers forum instead.

    Wednesday, June 17, 2015 4:47 PM

All replies

  • Hello Chris,

    The Add method of the BuildingBlockEntries class creates a new building block entry in a template and returns a BuildingBlock object that represents the new building block entry.

    Sub AddBBProgramatically()
    Dim oRng As Word.Range
    Dim oTmp As Template
      Set oRng = Selection.Range
      Set oTmp = Document.AttachedTemplate 
      oTmp.BuildingBlockEntries.Add _
        Name:="New BB", Type:=wdTypeQuickParts, Category:="New Category", _
        Range:=oRng, InsertOptions:=wdInsertContent
    lbl_Exit:
      Exit Sub
    End Sub

    I'd suggest asking Word specific questions on the Word for Developers forum instead.

    Wednesday, June 17, 2015 4:47 PM
  • Hi Eugene,

    That's great, many thanks. With your example as a starting point, and a bit of further searching, I have managed to get my code to work. I now just need to transform the "proof of concept" into a usable project, but the hardest part is done. Once again, many thanks for your assistance.

    Chris

    Thursday, June 18, 2015 3:45 PM