• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1329
  • Last Modified:

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
0
countyprob
Asked:
countyprob
  • 2
1 Solution
 
maggiec58Commented:
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
0
 
countyprobSystem AnalystAuthor Commented:
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
0
 
maggiec58Commented:
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now