Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    creating reports using mail merge

    I am new to the Lounge, having searched the web without success, and hope I can find help creating my reports, these consist of a dozen or so Word 2002 prepared documents of a page or half page making up 20 plus pages when inserted into the template. So far I use a macro to find bookmarks and insert each file by browsing for the filename, I want to automate this, perhaps using MailMerge. I want to enter a short filename into Access forms, and use that a source for mailmerge, I try linking but the link comes into the merged document as text, of the file location, I want the actual document to be inserted, any help will be appreciated.

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    It's not apparent to me from what you've posted that mailmerge is appropriate. Rather, if you're trying to insert file contents at bookmark locations, you'd use a straight vba solution. Perhaps you could provide more details, including sample documents (delete anything sensitive) and the code you're now using.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    New Lounger
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for reply, you are correct in that I want to insert file contents at bookmark locations by entering the filenames in a Form. I'll explain further, I did not want to make too long a question, I hoped it was not so complicated, and although I have fiddled with VB editor in Word Macros I am not much on programming so MailMerge is an easy option for me.
    I have a 14 page template for a report that stretches to around 20 to 24 pages after the addition of various document files I insert at bookmarks throughout the template. I have hudreds, maybe over a thousand of these additional files in 20 or so folders. I created a macro to locate bookmarks and to insert each individually I use a Macro I came across on a web search, each file is checked for spelling and re-saved, as it may have been updated since last use- here it is;

    Code:
    Sub FileInsMacro()
      testf = WordApplicationGetOpenFileName("*.doc", True, True)
     Documents.Open filename:=testf
      Selection.WholeStory
        With Selection.font
            .Name = "Bookman Old Style"
            .Size = 12
            .Bold = False
            .Italic = False
        End With
           Selection.MoveDown Unit:=wdLine, Count:=1
        If Options.CheckGrammarWithSpelling = True Then
            ActiveDocument.CheckGrammar
        Else
            ActiveDocument.CheckSpelling
        End If
        ActiveDocument.Save
        Selection.WholeStory
        Selection.Copy
        ActiveDocument.Close
        Selection.Paste
      End Sub
    Function WordApplicationGetOpenFileName(FileFilter As String, ReturnPath As Boolean, ReturnFile As Boolean) As String
    ' returns the folder and/or filename to a single user selected file
    Dim strFileName As String, strPathName As String
        If Not ReturnPath And Not ReturnFile Then Exit Function
           If FileFilter = "" Then FileFilter = "*.*"
        With Application.Dialogs(wdDialogFileOpen)
            .Name = FileFilter
            On Error GoTo MultipleFilesSelected
            If .Display = -1 Then
                strFileName = .Name
            End If
            On Error GoTo 0
        End With
        On Error GoTo 0
        ' remove any "-characters
        If InStr(1, strFileName, " ", vbTextCompare) > 0 Then
            strFileName = Mid$(strFileName, 2, Len(strFileName) - 2)
        End If
        If ReturnPath Then
            strPathName = CurDir & Application.PathSeparator
        Else
            strPathName = ""
        End If
        If Not ReturnFile Then strFileName = ""
        WordApplicationGetOpenFileName = strPathName & strFileName
        
    MultipleFilesSelected:
    End Function
    I select each file individually to insert, but as I now have an Access database for clients I would like to type into the Client Form the relevant filenames of all the files I want to insert, filenames are short, e.g. li7, gm19, and I don't want to have to enter full path every time, I also add brief texts that I have added to Database and tried MailMerge to insert these into the template and works fine, but I now need to add the filenames but don't know how to insert the actual document texts, I hope this helps, your advice is much appreciated, Thank you.
    Last edited by macropod; 2013-11-05 at 15:56. Reason: Added code tags

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Presumably you have the file paths and their names stored somewhere. In that case, you could replace your existing code with something like:
    Code:
    Sub DocInsert(StrFile As String, BmkNm As String)
    Dim SrcDoc As Document, BmkRng As Range
    SrcDoc = Documents.Open(FileName:=StrFile, ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
    SrcDoc.Range.Copy
    SrcDoc.Close
    With ActiveDocument
      If .Bookmarks.Exists(BmkNm) Then
        Set BmkRng = .Bookmarks(BmkNm).Range
        BmkRng.Paste
        .Bookmarks.Add BmkNm, BmkRng
      Else
        MsgBox "Bookmark: " & BmkNm & " not found."
      End If
    End With
    Set BmkRng = Nothing: Set SrcDoc = Nothing
    End Sub
    Where 'StrFile' is a string containing the input document's full name and path and 'BmkNm' is the name of the bookmark you want to update. Nothing needs to be selected. To update a given bookmark, you'd then use something like:
    Code:
    Sub Demo()
    Dim StrPath As String, StrName As String, StrBkMk As String
    StrPath = "C:\Users\" & Environ("UserName") & "\Desktop\"
    StrName ="MyDocument.doc"
    StrBkMk = "SomeBookmark" 
    Call DocInsert(StrPath & StrName, StrBkMk)
    End Sub
    In reality, you'd get the content for the above variables from your database.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. The Following User Says Thank You to macropod For This Useful Post:

    Alexix (2013-11-05)

  6. #5
    New Lounger
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    To close this, although Paul's reply is helpful I have found just what I wanted at http://www.wordbanter.com/showthread.php?t=139779 it goes like this:
    {QUOTE {INCLUDETEXT "C:\\Document Path\\{ MERGEFIELD Article }.docx"}} but there is more to it, check the web page.

  7. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi Alexis,

    Whilst with the benefit of hindsight one might see how the field code is applicable, there is nothing previous in your posts (except for the thread title) that indicated that mailmerge was actually being used; indeed your vba code suggested otherwise (an Access-driven report).
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •