Link to home
Start Free TrialLog in
Avatar of N P
N P

asked on

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

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 normal.dot 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
      Else
        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
    oRangeEnd.InsertParagraph
   
    'insert file in range
    oRangeStart.InsertFile FileName:=sPath & sFileName, _
              ConfirmConversions:=False
   
    'Delete paragraph marker
    oRangeEnd.Characters.Last.Delete
    oRangeEnd.Characters.Last.Delete
 
    'Get the first bookmark in inserted document
    If oRangeEnd.Bookmarks.Count > 0 Then
        'exist bookmark = True
        oRangeEnd.Bookmarks(1).Select
    Else
        oRangeEnd.Collapse direction:=wdCollapseEnd
        'oRangeEnd.Text = Chr(13)
        oRangeEnd.Collapse direction:=wdCollapseEnd
        oRangeEnd.Select
    End If
   
  End With
 
  'Clean up variables
  Set oBookmark = Nothing
  Set oRange = Nothing
  Set oRangeStart = Nothing
  Set oRangeEnd = Nothing
 
End Sub

thanks
nick
Avatar of maggiec58
maggiec58
Flag of United States of America image

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
Avatar of N P
N P

ASKER

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.  

Thanks
np
ASKER CERTIFIED SOLUTION
Avatar of maggiec58
maggiec58
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial