none
Steps to Create Add-in in Word 2010 using VBA RRS feed

  • Question

  • Hello all,

    I have the following code that I want to run as add-in. Can you please give me the steps to create add-in in MS Word 2010. Thanks in advance.

    Sub MergeDataSource()


         Dim fd As FileDialog
        Dim xfilepath As Variant
        
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
        Set myMerge = ThisDocument.MailMerge
        
        'Dim doc As Word.Document
        
        myMerge.MainDocumentType = wdNotAMergeDocument
        myMerge.MainDocumentType = wdFormLetters
        
        'Browse for the Datasource
        
        Set fd = Application.FileDialog(msoFileDialogFilePicker)
        ' Set the title of the dialog box.
        
        With fd
        
        .Title = "Select the Excel File"
            If .Show = True Then
             ' Assign the file to a variable xfilepath.
                xfilepath = fd.SelectedItems.Item(1)
     
                Else
                    MsgBox "You clicked Canncel in the file dialog box.", , "Cancelling Merging process"
                    Exit Sub
          End If
        End With
        
        
        'Connection to the Data Dource
        On Error Resume Next
        myMerge.OpenDataSource Name:= _
            xfilepath _
            , ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
            AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
            WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
            Format:=wdOpenFormatAuto, Connection:= _
            "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=xfilepath;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";J" _
            , SQLStatement:="SELECT * FROM `HASHMAN19`", SQLStatement1:="", SubType:=wdMergeSubTypeAccess
        
         On Error GoTo 0
        'Print the document to the default printer
        
        With myMerge
            .Destination = wdSendToPrinter
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = wdDefaultFirstRecord
                .LastRecord = wdDefaultLastRecord
            End With
            .Execute Pause:=False
        End With
    End Sub

    Thanks all.


    Thanks to all that continue to serve as help HashMan


    • Edited by HashMan19_11 Saturday, July 13, 2013 4:48 PM Edit content
    Saturday, July 13, 2013 4:32 PM

Answers

  • In a nutshell, for VBA to be an Add-in the code must be in a template and the template loaded as an add-in. If you want the template to load automatically as an add-in, it must be in the user's STARTUP folder. Template add-ins can also be loaded via the Templates dialog box (available by default in the Developers tab).

    If you use this code as an add-in you of course want it to work on the current document, not the template containing the code. In that case, you must change

    Set myMerge = ThisDocument.MailMerge

    to

    Set myMerge = ActiveDocument.MailMerge


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, July 14, 2013 3:46 PM
    Moderator

All replies

  • In a nutshell, for VBA to be an Add-in the code must be in a template and the template loaded as an add-in. If you want the template to load automatically as an add-in, it must be in the user's STARTUP folder. Template add-ins can also be loaded via the Templates dialog box (available by default in the Developers tab).

    If you use this code as an add-in you of course want it to work on the current document, not the template containing the code. In that case, you must change

    Set myMerge = ThisDocument.MailMerge

    to

    Set myMerge = ActiveDocument.MailMerge


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, July 14, 2013 3:46 PM
    Moderator
  • Thanks Cindy for the advice. You are great.

    Thanks to all that continue to serve as help HashMan

    Tuesday, July 16, 2013 9:15 PM