locked
Selecting quick part RRS feed

  • Question

  • Hi,

    I currently have a word template in SharePoint for a document library. I have inserted quick parts inside the template that link to the columns inside the document library (also called file properties). I am trying to use VBA to select a quick part, so I can get the value and store it inside a variable.

    Does anybody know how I can select a quick part and store whatever the value is inside a variable?

    One of my quick parts is called "Forename"

    Thanks.




    • Edited by t3ch21 Monday, February 12, 2018 9:43 AM
    Monday, February 12, 2018 9:34 AM

All replies

  • It might be better to use docvariables or docproperties for this than quickparts, however it depends what you want to do with the value.

    You could insert the quickpart at a bookmarked location e.g. to insert it at a bookmark called Forename

    Sub Example1()
        AutoTextToBM "bmForename", ActiveDocument.AttachedTemplate, "Forename"
    End Sub
    
    Sub AutoTextToBM(strbmName As String, oTemplate As Template, strAutotext As String)
    'Graham Mayor - http://www.gmayor.com - Last updated - 12 Feb 2018'
    'strBMName is the name of the bookmark to fill'
    'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate'
    'strAutotext is the name of the autotext entry'
    Dim oRng As Range
        On Error GoTo lbl_Exit
        With ActiveDocument
            Set oRng = .Bookmarks(strbmName).Range
            Set oRng = oTemplate.AutoTextEntries(strAutotext).Insert _
                       (Where:=oRng, RichText:=True)
            .Bookmarks.Add Name:=strbmName, Range:=oRng
        End With
    lbl_Exit:
        Exit Sub
    End Sub

    or you can simply grab the value of the quickpart e.g.

    Sub Example2()
        Dim strText As String
        strText = GetAutoTextValue(ActiveDocument.AttachedTemplate, "Forename")
        MsgBox strText
    End Sub
    
    Function GetAutoTextValue(oTemplate As Template, strAutotext As String) As String
    'Graham Mayor - http://www.gmayor.com - Last updated - 12 Feb 2018'
    'oTemplate is the template with the autotext - probably ActiveDocument.AttachedTemplate'
    'strAutotext is the name of the autotext entry'
    Dim strValue As String
        On Error GoTo err_Handler
        GetAutoTextValue = oTemplate.AutoTextEntries(strAutotext).Value
    lbl_Exit:
        Exit Function
    err_Handler:
        Err.Clear
        GetAutoTextValue = ""
        GoTo lbl_Exit
    End Function
    
    
    


    Graham Mayor - Word MVP
    www.gmayor.com

    Monday, February 12, 2018 11:59 AM
  • Hi, thanks for the quick reply!

    I have just tried example 2 but it was going to the err_Handler. I think the problem is that the code works for Auto Text quick parts, my Forename quick part is under the Document Property. (So I go to Insert ribbon > drop down for quick parts > document property > forename)

    Do you know if I can do the above code with document property instead, and if yes how? Thanks.

    In the meantime I will see if I can do it.

    Also just a quick note, the document is .docm

    • Edited by t3ch21 Monday, February 12, 2018 12:51 PM
    Monday, February 12, 2018 12:23 PM
  • Sorry - my fault. I misunderstood the requirement (which is also why I was somewhat confused about why you were doing it that way).

    You can read a custom docproperty in a macro enabled document using

    ThisDocument.CustomDocumentProperties("PropertyName").Value

    However checkout the difference between ThisDocument and ActiveDocument

    If you want a function like the one I posted earlier

    Function GetDocPropertyValue(strProperty As String) As String
    'Graham Mayor - http://www.gmayor.com - Last updated - 13 Feb 2018'
    Dim strValue As String
        On Error GoTo err_Handler
        GetDocPropertyValue = ThisDocument.CustomDocumentProperties(strProperty).Value
    lbl_Exit:
        Exit Function
    err_Handler:
        Err.Clear
        GetDocPropertyValue = ""
        GoTo lbl_Exit
    End Function
    You may also find http://www.gmayor.com/BookmarkandVariableEditor.htm useful.





    Graham Mayor - Word MVP
    www.gmayor.com


    Tuesday, February 13, 2018 5:08 AM
  • Hi,

    Np, it was my fault, I didn't realise there was an Auto Text property.

    I actually figured out how to do it. I used:

    GetDocPropertyValue = ActiveDocument.ContentTypeProperties.Item(strProperty)

    or

    GetDocPropertyValue = ActiveDocument.ContentTypeProperties(strProperty).Value

    Your line doesn't work for me:

    GetDocPropertyValue = ThisDocument.CustomDocumentProperties(strProperty).Value

    And ThisDocument doesn't work either, any ideas why?

    Tuesday, February 13, 2018 4:21 PM
  • I did caution to checkout thisdocument vis-a-vis activedocument. Thisdocument is the document/template with the macro. ActiveDocument is the document you are working on. They may not be the same document.

    ContentTypeProperties are usually related to doc management systems, so presumably relate to Sharepoint, which would explain why my suggested code didn't work here. Fortunately you were able to see past that :)


    Graham Mayor - Word MVP
    www.gmayor.com

    Wednesday, February 14, 2018 9:45 AM