+ Reply to Thread
Results 1 to 3 of 3

Checkbox - adding document into existing document.

Hybrid View

  1. #1
    Registered User
    Join Date
    11-24-2015
    Location
    London, England
    MS-Off Ver
    2010
    Posts
    1

    Question Checkbox - adding document into existing document.

    As a newbie to VBA i apologize in advance if this seems a little simple!
    i am trying to code a document so when a specific option is checked an existing word doc is added to the document in question.
    currently i have the following code

    Private Sub CheckBox1_Click()
    
    If (CheckBox1.Value = True) Then Selection.EndKey Unit:=wdStory
        ChangeFileOpenDirectory "C:\Users\FILE LOCATION HERE"
        Selection.InsertFile FileName:="Event Risk Assessment 1.docx", _
    Link:=False, Attachment:=False
    
    
    End Sub
    this works fine upon 'checking' the box however when 'unchecking' it simply repeats the process however instead of inserting at the end of the document it places it where the checkbox is.
    The ideal is so that when the box is 'unchecked' it deletes/removes the inserted document altogether.

    as another point i could only find out how to insert the document after the last paragraph using code
    Selection.EndKey Unit:=wdStory
    I was hoping you might be able to share a code which will insert it at the bottom of the existing doc on a new page
    any help greatly appreciated.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,642

    Re: Checkbox - adding document into existing document.

    Hi there,

    I haven't tested this, but here's a knee-jerk reaction - try:

    
    
    Private Sub CheckBox1_Click()
    
        If (CheckBox1.Value = True) Then
    
            Selection.EndKey Unit:=wdStory
            ChangeFileOpenDirectory "C:\Users\FILE LOCATION HERE"
            Selection.InsertFile Filename:="Event Risk Assessment 1.docx", _
                                 Link:=False, Attachment:=False
    
        End If
    
    End Sub

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Forum Expert macropod's Avatar
    Join Date
    12-22-2011
    Location
    Canberra, Australia
    MS-Off Ver
    Word, Excel & Powerpoint 2003 & 2010
    Posts
    3,839

    Re: Checkbox - adding document into existing document.

    Try:
    Private Sub CheckBox1_Click()
    Dim BmkRng As Range
    Const BmkNm As String = "_InsertFile"
    With ActiveDocument
      If .Bookmarks.Exists(BmkNm) = False Then
        Set BmkRng = .Range.Characters.Last
        .Bookmarks.Add BmkNm, BmkRng
      Else
        Set BmkRng = .Bookmarks(BmkNm).Range
      End If
      If CheckBox1.Value = True Then
        BmkRng.InsertFile _
        FileName:="C:\Users\FILE LOCATION HERE\Event Risk Assessment 1.docx", _
        Link:=False, Attachment:=False
      Else
        BmkRng.Text = ""
      End If
      .Bookmarks.Add BmkNm, BmkRng
    End With
    End Sub
    Cheers,
    Paul Edstein
    [Fmr MS MVP - Word]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Adding fillable form fields to an existing document
    By Sionos in forum Word Formatting & General
    Replies: 5
    Last Post: 04-25-2013, 07:42 AM
  2. Excel Opens Existing Document not blank one
    By OcMac in forum Excel General
    Replies: 4
    Last Post: 03-22-2013, 04:34 PM
  3. Add text to Existing Word Document if it Doesn't exist create Word Document
    By unstable81 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-10-2013, 12:15 AM
  4. Excel 2007 : CSV tab copy to existing excel document
    By awaters7832 in forum Excel General
    Replies: 0
    Last Post: 03-04-2011, 06:52 PM
  5. Putting a Checkbox in a table in a word document
    By Brunstgnegg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-11-2010, 03:17 AM
  6. Excel 2007 : Saving an existing document
    By satpal in forum Excel General
    Replies: 1
    Last Post: 10-15-2009, 06:20 PM
  7. Modify an existing document
    By Bob Kasper in forum Excel General
    Replies: 3
    Last Post: 05-01-2008, 09:30 AM
  8. Replies: 2
    Last Post: 03-14-2007, 08:09 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1