Consulting

Results 1 to 12 of 12

Thread: Is this possible?

  1. #1

    Is this possible?

    Hi

    I am new to VBA. I am creating some templates with content controls for people to fill out. I have made some rules to check certain dimensions are being filled on correctly etc.

    Sometimes additional pages need to be filled out and it would be really useful to be able to allow the user to insert / append these additional pages into the current document so that I can continue using the rules I have already set up.

    I need to know if it is possible to:

    1) be able to insert a different template / page / document into the current active document without anything having to be closed or reopened
    2) be able to maintain the inserted documents orientation such as portrait / landscape which may be different from the previous page.
    3) if 1 and 2 are possible then could I create a form where users could request a selection of different pages / templates and even in some instances multiples of the same page? All to stil use my original conditional rules already set in the orginal document?

    If someone could advise if this is possible, and possible for someone within limited knowledge to achieve, that would be most apprecaited.

    Many thanks

  2. #2
    Is it possible? Yes, but as someone new to VBA you are attempting to run before you can walk.
    The main questions that need to be asked first are who is going to use this template?
    Will that person (or persons) have access to the location of the documents that you want to include.
    It is fairly straightforward to insert a document at a particular location. You need to determine what that location is and under what circumstances it is to be inserted before deciding on the best process for doing so.
    This question appears to be related to your question at https://www.msofficeforums.com/. Please observe cross posting etiquette or you will quickly lose the goodwill of those who provide support.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thanks for taking the time to answer.

    Quote Originally Posted by gmayor View Post
    Is it possible? Yes, but as someone new to VBA you are attempting to run before you can walk.
    Ok but have to start somewhere. The question is where!

    Quote Originally Posted by gmayor View Post
    IThe main questions that need to be asked first are who is going to use this template?
    This is set up for a number of users who have to fill in a form following a site visit. They are not computer savvy so needs to be as basic as possible and not allow them to get in too much of a mess.
    Quote Originally Posted by gmayor View Post
    IWill that person (or persons) have access to the location of the documents that you want to include.
    My plan was to provide a folder with all templates. They load the main template and can add any others from the folder provided.
    Quote Originally Posted by gmayor View Post
    IIt is fairly straightforward to insert a document at a particular location. You need to determine what that location is and under what circumstances it is to be inserted before deciding on the best process for doing so.
    The location should just on a new page at the end of the document.
    Quote Originally Posted by gmayor View Post
    IThis question appears to be related to your question at https://www.msofficeforums.com/. Please observe cross posting etiquette or you will quickly lose the goodwill of those who provide support.
    It is related but not the same. I had hoped this would not cross the line with the etiquette rules. If it has then I will be more than prepared to take whatever action is necessary to fix the issue. The last thing I want is to cause any upset to those taking the time to provide help to users like myself.

  4. #4
    To get you started create a userform with a list box and two command buttons.
    Save the template with the userform.
    Create a subfolder of the folder in which you saved the template and call it 'Templates'.
    Put your documents in that folder.
    In the userform module add the following code.The documents you select will be added to the end of the document.

    Option Explicit
    Private sPath As String
    Private sFile As String
    Private oRng As Range
    Dim i As Integer
    
    Private Sub CommandButton1_Click()
        sPath = ThisDocument.path & "\Templates\"
        With ActiveDocument
            For i = 0 To ListBox1.ListCount - 1
                If ListBox1.Selected(i) = True Then
                    Set oRng = .Range
                    With oRng
                        .Collapse wdCollapseEnd
                        .InsertBreak wdSectionBreakNextPage
                        .End = ActiveDocument.Range.End
                        .Collapse wdCollapseEnd
                        .InsertFile sPath & ListBox1.List(i) & ".docx"
                    End With
                End If
            Next i
        End With
        Unload Me
    End Sub
    
    Private Sub CommandButton2_Click()
        Unload Me
    End Sub
    
    Private Sub UserForm_Initialize()
        sPath = ThisDocument.path & "\Templates\"
        With ListBox1
            .MultiSelect = fmMultiSelectMulti
            sFile = Dir$(sPath & "*.docx")
            While sFile <> ""
                .AddItem Left(sFile, InStrRev(sFile, Chr(46)) - 1)
                sFile = Dir$()
            Wend
        End With
        CommandButton1.Caption = "Insert Document"
        CommandButton2.Caption = "Cancel"
    End Sub
    See https://wordmvp.com/FAQs/Formatting/...apeSection.htm
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Hi

    That is extremely kind of you to take the time to do this - I am most grateful.

    I am going to give it a go shortly and will report back on the outcome!

  6. #6
    Quote Originally Posted by gmayor View Post
    To get you started create a userform with a list box and two command buttons.
    Save the template with the userform.
    Create a subfolder of the folder in which you saved the template and call it 'Templates'.
    Put your documents in that folder.
    In the userform module add the following code.The documents you select will be added to the end of the document.
    Hi Graham

    I have been testing this out and it does work really great but I still don't understand and cannot work out how I can get it to insert the new template whilst mainting the template orientation. So my current document consists of several pages, some portrait and some landscape. All my templates are already set up in the correct format but some of these are also portrait and some are landscape. Is it possible to add multiple documents to the open document whilst preserving the additional templates orioentation? If not, what is the best way to achieve this?

    On another note, I beleive that when I add a new page, my exiting contentcontrol grouping is removed - could this be correct and is this expected behaviour?

    Many thanks once again for taking the time to assist.

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location

    Orientation property
    as it applies to the PageSetup object.

    Returns or sets the orientation of the page. Read/write WdOrientation
    WdOrientation can be one of these WdOrientation constants.
    wdOrientLandscape
    wdOrientPortrait
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    If you are going to insert documents that vary in orientation, you are going to have to identify to the macro which documents are portrait and which are landscape. One way to do that is to insert 'landscape' into the filename. You can then modify the code to detect that name and set the orientation for the added section accordingly e.g.
    Private Sub CommandButton1_Click()
        sPath = ThisDocument.Path & "\Templates\"
        With ActiveDocument
            For i = 0 To ListBox1.ListCount - 1
                If ListBox1.Selected(i) = True Then
                    Set oRng = .Range
                    With oRng
                        .Collapse wdCollapseEnd
                        .InsertBreak wdSectionBreakNextPage
                        .End = ActiveDocument.Range.End
                        .Collapse wdCollapseEnd
                        With .PageSetup
                            If InStr(1, LCase(ListBox1.List(i)), "landscape") > 0 Then
                                .Orientation = wdOrientLandscape
                            Else
                                .Orientation = wdOrientPortrait
                            End If
                            .DifferentFirstPageHeaderFooter = False
                        End With
                        .InsertFile sPath & ListBox1.List(i) & ".docx"
                    End With
                End If
            Next i
        End With
        Unload Me
    End Sub
    I don't understand your comment about content controls, but if you add documents that have content controls, any macro that references content controls will need to know about those additional controls.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    119
    Location
    Be also aware that you can have mapped content controls that repeat data. There is a basic set of these available in all documents under Quick Parts > Document Properties. Note that some of these are not really document properties. Repeating Data Using Document Property Content Controls and Other Mapped Content Controls

    If you use these in your inserted documents, they will pick up any already inserted information.

  10. #10
    Sorry for the delay and thanks to both for the response. It does seem to be working correctly now!

  11. #11
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    119
    Location
    Could you do a favor for the forum?
    1. Change the title to something reflecting more about what you are trying to do. That way others can find this more easily to help with their problems.
    2. Mark Graham's post(s) as the answer to your question.

  12. #12
    Hi Chas

    I have managed to mark the thread as solved but I can't see how to edit the title or mark a specific post as the answer. I am happy to do this if I know how.

    Thanks

Posting Permissions

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