Word 2010 - automatically insert word doc at the end of page of current doc

Posted on 2012-08-17
Last Modified: 2012-08-23
I am using word 2010.  How can use a keyboard stroke (ALT-M) that will open file dialog box, then insert selected word document into my current open word document at end of the page.  I use to have a for word 2003 that had the following code, but don't know how to create this in the normal.dotm in word 2010.  I need this to be native to word 2010 dotm file (without 2003 compatibility mode):

Sub insertmyfile()
Dim oBookmark As Bookmark
Dim oRange As Range
Dim oRangeStart As Range
Dim oRangeEnd As Range

  'Set the range at the last paragraph in the bookmark
    Set oRange = ActiveDocument.Content
    oRange.Collapse direction:=wdCollaspeEnd
  With oRange
    'Insert two paragraph markers
    .Collapse direction:=wdCollapseEnd
    .Text = Chr(13) '& Chr(13)
    .Collapse direction:=wdCollapseEnd
    'Insert the file
    Dim sPath As String
    Dim sFileName As String

    With Dialogs(wdDialogInsertFile)
      If .Display = True Then
        'User pressed Ok, get filename
        sFileName = .Name
        MsgBox "You didn't select a file to insert"
        Exit Sub
      End If
    End With
    'Get the path
    sPath = CurDir()
    'Add \ if necessary
    If Right(sPath, 1) <> Application.PathSeparator Then
      sPath = sPath & Application.PathSeparator
    End If
    'set the range from insert of document to end of document
    Set oRangeStart = ActiveDocument.Content            'begin is end of previous doc
    oRangeStart.Collapse direction:=wdCollaspeEnd       'collapse entire document
    'Get identical range for oRangeEnd
    Set oRangeEnd = oRangeStart.Duplicate
    'Insert a paragraph in range because it needs at least 1 char to mark an insertion point
    'insert file in range
    oRangeStart.InsertFile FileName:=sPath & sFileName, _
    'Delete paragraph marker
    'Get the first bookmark in inserted document
    If oRangeEnd.Bookmarks.Count > 0 Then
        'exist bookmark = True
        oRangeEnd.Collapse direction:=wdCollapseEnd
        'oRangeEnd.Text = Chr(13)
        oRangeEnd.Collapse direction:=wdCollapseEnd
    End If
  End With
  'Clean up variables
  Set oBookmark = Nothing
  Set oRange = Nothing
  Set oRangeStart = Nothing
  Set oRangeEnd = Nothing
End Sub

Question by:countyprob
    LVL 1

    Expert Comment

    This will insert a user selected word document at the end of the current document.  I wasn't sure if that's exactly what you wanted or if you wanted it at the end of the current page, but this should get you started.  

    Go into Word.  Choose the file tab.  choose options, customize ribbon.  in the Main Tabs window check the box for Developer.

    You should now have a developer tab on your ribbon.  Click that.  Click on Record Macro in the code section.  Give the macro your desired name and assign your keyboard shortcut.  click ok. click Stop Recording.

    Click the Macros button.  choose your macro and click Edit.

    Insert the following code between Sub and end Sub.

        Dim cfilename As String
        Dim nReturn As Integer
        Dim varfile As Variant
        Selection.EndKey Unit:=wdStory
        Selection.InsertBreak Type:=wdPageBreak
        nReturn = Dialogs(wdDialogInsertFile).Show
        If nReturn <> -1 Then
            MsgBox ("You didn't select a file to insert")
        End If
    LVL 1

    Author Comment

    Thanks  maggiec58.  The file that I am inserting has textbox form fields.  Any idea on how I can have the cursor land on first textbox form fields for the inserted document.  

    LVL 1

    Accepted Solution

    You need to know a little about the document you are inserting.  The following will land on the first form field.  If the first field might not be a text box, you could do a loop from nexisting + 1 to ncurrent checking for the formfield type.  When you find a text box then exit the loop.  The form field object has a type property and a name property.  either of those would help you.  Check the help for formfield and you should be able to figure the details out from here.

        Dim cfilename As String
        Dim nReturn As Integer
        Dim varfile As Variant
        Dim nexisting As Integer
        Dim ncurrent As Integer

        nexisting = ActiveDocument.FormFields.Count
        Selection.EndKey Unit:=wdStory
        Selection.InsertBreak Type:=wdPageBreak
        nReturn = Dialogs(wdDialogInsertFile).Show
        If nReturn <> -1 Then
            MsgBox ("You didn't select a file to insert")
        End If
        ncurrent = ActiveDocument.FormFields.Count
        If ncurrent > nexisting Then
            ActiveDocument.FormFields(nexisting + 1).Select
        End If
    End Sub

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
    Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
    In a previous video Micro Tutorial here at Experts Exchange (, I explained how to get a free, one-month trial of Office …
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now