none
embed image into .dotm template file for distribution RRS feed

  • Question

  • I would like to include one or images, our logos, into a word template that I've added several macros to that build forms in VBA. My objective is to be able to embed an image into some of those forms dynamically. I have put the logos in word files with no problem. But for the template, the logo is not in the default document. Thanks for any assistance with my problem.

    bdpb

    Wednesday, January 19, 2011 10:18 AM

Answers

  • Hi bdpb

    Thanks for the additional information :-) I think I understand, now, what you want.

    have you considered storing the images as BuildingBlockEntries in the template?

    To get a feel for this functionality, go to Insert/Quick Parts/Building Block Organizer in the UI. You'll see that entries can be stored in "Galleries" and "Categories", and are saved in templates. The built-in ones are meant to be available to the application, thus the storage is a central template (BuildingBlocks.dotx). But they can also be saved in other templates used to create documents and will then be available only to those documents attached to the template.

    Basically, your code inserts the BuildingBlockEntry required at the moment.


    Cindy Meister, VSTO/Word MVP
    Wednesday, January 19, 2011 4:08 PM
    Moderator
  • Hi bdpb

    <<The problem I'm having today is that if I don't click on the Quick Parts button the insert throws an error. >>

    Always helps if you mention the text of the errror...

    Try Application.Templates.LoadBuildingBlocks


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by JPL_GDoL Thursday, January 20, 2011 5:44 PM
    Thursday, January 20, 2011 4:32 PM
    Moderator

All replies

  • Hi bdpb

    Can you be more specific please about what kind of "forms"? Do you mean forms with form fields? Or VBA forms? Something else?

    Which version of Word?

    How have you inserted the logo in the template? Please list the exact steps, starting with opening the template file.

    How are you creating the "default document" where you're not seeing the logo? (Please list the command steps used)


    Cindy Meister, VSTO/Word MVP
    Wednesday, January 19, 2011 3:39 PM
    Moderator
  • OK thank-you Cindy for replying,

    First of all this is for MS Word 2007.

    Next I mean "paper soft forms" in VBA within the Word Template.

    My org has many forms that users select from the ribbon and fill in, print or email.

    I did the test imsert just using Word and inserting an image from the Insert tab on the ribbon. Which is fine if you want the image to come up in the users default Word document but I only want to use the logo/image dynamically in code.

    My goal is in embed our logo in the template so it can pushed out with SMS

    So there is two parts, first add the file to the .dotm file and second to call it from code. An example of a form I am creating would be:

    Sub NewOJTFactSheet(ByVal control As IRibbonControl)

        Dim intX As Integer
        Dim ques(2) As String ' OJT questions
        Dim pars(3) As String ' array of paragraphs
       
        ques(0) = "How does it work?"
        ques(1) = "What support services are available?"
        ques(2) = "What paperwork is involved?"
       
        pars(0) = "paragraph 1, bla, bla, bla..."
        pars(1) = "paragraph 2, bla, bla, bla..."
        pars(2) = "paragraph 3, bla, bla, bla..."
        pars(3) = "paragraph 4, bla, bla, bla..."

        MsgBox introMsg, vbInformation, ModGl.appTitle
       
        CreateNewDocument 12, "Garamond", "P", "JobCoachFactSheet", 1, 1, 1, 1
       
        ' Document Header
        With Selection
            ' Select entire document and begin adding content
            .Expand Unit:=wdStory
            With .Font
                .Size = 16
                .Bold = True
                .Italic = False
                .Underline = wdUnderlineNone
            End With
            .InsertAfter Text:="Vocational Rehabilitation" & vbCrLf
            .InsertAfter Text:="On the Job Training"
            .ParagraphFormat.Alignment = wdAlignParagraphCenter
            .EndKey Unit:=wdLine
            .TypeParagraph
            .TypeParagraph
            .TypeParagraph
        End With
       
        With Selection
            .EndKey Unit:=wdStory
            With .Font
                .Size = 12
                .Bold = True
            End With
            .ParagraphFormat.Alignment = wdAlignParagraphJustify
            .TypeText pars(0)
            .TypeParagraph
            .TypeParagraph
            For intX = 0 To 2
                .Font.Name = "Wingdings 2"
                .TypeText Chr(174) & " "
                .Font.Name = "Garamond"
                .Font.Size = 12
                .TypeText ques(intX)
                .TypeParagraph
                .TypeParagraph
                .TypeText pars(intX + 1)
                .TypeParagraph
                .TypeParagraph
            Next intX
        End With
        PrepTheDocument
    End Sub


    bdpb
    Wednesday, January 19, 2011 3:54 PM
  • Hi bdpb

    Thanks for the additional information :-) I think I understand, now, what you want.

    have you considered storing the images as BuildingBlockEntries in the template?

    To get a feel for this functionality, go to Insert/Quick Parts/Building Block Organizer in the UI. You'll see that entries can be stored in "Galleries" and "Categories", and are saved in templates. The built-in ones are meant to be available to the application, thus the storage is a central template (BuildingBlocks.dotx). But they can also be saved in other templates used to create documents and will then be available only to those documents attached to the template.

    Basically, your code inserts the BuildingBlockEntry required at the moment.


    Cindy Meister, VSTO/Word MVP
    Wednesday, January 19, 2011 4:08 PM
    Moderator
  • I've never heard of BuildingBlockEntries. I will be glad to look into that and try to make it work. Thank-you Cindy, I will reply soon.

    Do you know of any how-to sites?

    thanks,

    bdpb


    bdpb
    Wednesday, January 19, 2011 5:33 PM
  • Hi bdpb

    If you've worked with Word in days-gone-by, this is what replaced AutoText.

    I don't know of any particular special link, but here's a Google search that turns up a number of articles.

    http://www.google.ch/search?q=Word+Building+Blocks&rls=com.microsoft:en-US&ie=UTF-8&oe=UTF-8&startIndex=&startPage=1&redir_esc=&ei=dSo3TamWMYOBOoi_4fgD

    This one article is from the developer POV and should give a pretty good overview
    http://msdn.microsoft.com/en-us/library/bb258119(office.12).aspx

    A couple of "how to" articles I found search MSDN

    http://msdn.microsoft.com/en-us/library/bb410041(office.12).aspx
    http://msdn.microsoft.com/en-us/library/bb457184(office.12).aspx
    http://msdn.microsoft.com/en-us/library/bb498197(office.12).aspx


    Cindy Meister, VSTO/Word MVP
    Wednesday, January 19, 2011 6:21 PM
    Moderator
  • If the forms are userforms, you would add an image control to the form and in the initialize event include the following code:

    Image1.Picture = LoadPicture("C:\Users\Doug\Pictures\photo_00001.jpg") 'Use your own path\filename

    'and probably also include the following:

    Image1.PictureSizeMode = fmPictureSizeModeZoom
     -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "bdpb" wrote in message news:d876f3bf-714d-415d-a37b-1465d4ce40f2@communitybridge.codeplex.com...

    I would like to include one or images, our logos, into a word template that I've added several macros to that build forms in VBA. My objective is to be able to embed an image into some of those forms dynamically. I have put the logos in word files with no problem. But for the template, the logo is not in the default document. Thanks for any assistance with my problem.

    bdpb


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Wednesday, January 19, 2011 8:44 PM
  • Thanks Doug,

    No this is only to go into documents that are created dynamically by the user, by them selecting from the custom ribbon tab's buttons. Thanks to Cindy, I've been able to get my image in the Quick Parts gallery, including creating an "images" category. The problem I'm having today is that if I don't click on the Quick Parts button the insert throws an error. But if I click on Quick Parts (on the Insert tab) first then the insert works great from there on.

    So I'm guessing I need to open and close that list using VBA some how to get it to load the Buiding Blocks.dotx template. I read an article yesterday saying that the BB template doesn't load when the application starts, only after an event opens one of the galleries.

    bdpb

     


    bdpb
    Thursday, January 20, 2011 12:45 PM
  • Hi bdpb

    <<The problem I'm having today is that if I don't click on the Quick Parts button the insert throws an error. >>

    Always helps if you mention the text of the errror...

    Try Application.Templates.LoadBuildingBlocks


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by JPL_GDoL Thursday, January 20, 2011 5:44 PM
    Thursday, January 20, 2011 4:32 PM
    Moderator
  • Wonderful!! That did it. Now I can really start making forms with logos - not just text.

    Thank-you very much Cindy, you really helped me on this one a ton!

    ps Note taken, I will give more info on the error in any future postings.


    bdpb
    Thursday, January 20, 2011 5:46 PM
  • You should add the image to the template's buildingblocks.  (Insert the image on the template's document.  Select it, then select Insert > Quick Parts > Save Selection to QuickPart Gallery. 

    Now you can use the following code to insert the image in documents created by that template:

    In VBA:

    Sub InsertPic()
        Dim myTemplate As Template
        Set myTemplate = ActiveDocument.AttachedTemplate
        Application.Templates(myTemplate.FullName).BuildingBlockEntries("TheNameOfTheQuickPart").Insert where:=Selection.Range, RichText:=True
        End If
    End Sub
    

    In Visual Studio Tools for MS Office it's a bit more complicated.  The following method is called from another which passes the Name of the specific BuildingBlocks.Item to insert.  If needed, you could also pass in the name of the BuildingBlock Type, and Category. (The gallery name determines the "BuildingBlocks.Type", like wdTypeQuickParts, wdTypeCustomQuickParts, wdTypeCustom1.)  In the code below, I saved the image to the "Custom 1" gallery in the category "PTK".

        Public Sub InsertBB(ByVal strBBname As String)
            '' Calling Method passes in the name of the BuildingBlock Item
            Dim objTemplate As Template
            Dim doc As Word.Document = Application.ActiveDocument
            objTemplate = doc.AttachedTemplate
    
            Dim objBB As BuildingBlock
            '' Set insertion point as active document's selection
            Dim sel As Word.Selection = doc.Application.Selection
    
            Try
                sel.Collapse(WdCollapseDirection.wdCollapseStart)
                objBB = objTemplate.BuildingBlockTypes.Item(WdBuildingBlockTypes.wdTypeCustom1). _
                    Categories.Item("PTK").BuildingBlocks.Item(strBBname)
                objBB.Insert(sel.Range)
                sel = doc.Application.Selection
                sel.Collapse(WdCollapseDirection.wdCollapseEnd)
            Catch ex As Exception
                MsgBox("Error in ThisDocument.InsertBB: " & strBBname & ", " & ex.Message, _
                       MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error")
            End Try
        End Sub
    
    

    Hope this helps!


    Noel Gordon
    • Proposed as answer by NCGordon Thursday, February 2, 2012 4:40 PM
    Thursday, February 2, 2012 4:32 PM