PDA

View Full Version : [SOLVED:] Is this possible?



Trader174
07-10-2021, 05:06 AM
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

gmayor
07-11-2021, 01:36 AM
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.

Trader174
07-11-2021, 01:52 AM
Thanks for taking the time to answer.


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!


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.

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.

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.

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.

gmayor
07-11-2021, 05:55 AM
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/LandscapeSection.htm

Trader174
07-12-2021, 01:07 AM
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!

Trader174
07-17-2021, 04:58 AM
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.

SamT
07-17-2021, 09:00 AM
:dunno
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

gmayor
07-17-2021, 08:56 PM
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.

Chas Kenyon
07-19-2021, 12:01 PM
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 (http://addbalance.com/word/MappedControls.htm#PageStart)

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

Trader174
07-22-2021, 11:04 AM
Sorry for the delay and thanks to both for the response. It does seem to be working correctly now!

Chas Kenyon
07-22-2021, 03:43 PM
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.

Trader174
07-22-2021, 11:51 PM
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