Share via

Using macros to insert pages within a document

Anonymous
2012-03-29T13:42:33+00:00

I'm working on a new template for field notes on multiple day jobs that take place out of town.  Depending on the location and type of job, I will need different amounts of certain pages. Say the job is only a one day job out of town  but within the state. I will need three daily logs, and one diving log for the working day. If the job takes place in Sydney and last three days, I will need seven daily logs and three diving logs. These pages will have to be inserted in the correct place within the document.

What I want to do is use a prompt to ask the user how many travel days to, how many working days, and how many travel days from. The user will put in an answer, and a macro will insert the Quick Parts in the appropriate place in the doc.

I have already created the quick parts, and tested a macro that will insert the page. But I need some help on the prompt part and getting it to insert the appropriate page in the appropriate place.

Thanks for any help

-uriah

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2012-03-29T14:15:30+00:00

I would create a userform - seehttp://www.gmayor.com/Userform.htm

Using that as an example create a form with three fields, and a command button

Top field is for Travel To, middle field is for Working and bottom field is for Travel From

Add a Label across the top of the form to display messages (Label1) . Add more labels to describe the fields and then associate the following code with the form.

Option Explicit

Private Sub CommandButton1_Click()

Dim oRng As Range

Dim i As Long

If Not IsNumeric(Me.TextBox1.Value) Then

    Me.Label1.Caption = "Enter a numeric value in Travel To days!"

    Me.TextBox1.SetFocus

    Exit Sub

End If

If Not IsNumeric(Me.TextBox2.Value) Then

    Me.Label1.Caption = "Enter a numeric value in Working days!"

    Me.TextBox2.SetFocus

    Exit Sub

End If

If Not IsNumeric(Me.TextBox3.Value) Then

    Me.Label1.Caption = "Enter a numeric value in Travel From days!"

    Me.TextBox3.SetFocus

    Exit Sub

End If

For i = 1 To Me.TextBox1.Value

    Set oRng = ActiveDocument.Range

    oRng.Collapse wdCollapseEnd

    ActiveDocument.AttachedTemplate.BuildingBlockEntries("Travel To").Insert Where:=oRng, _

        RichText:=True

Next i

For i = 1 To Me.TextBox2.Value

    Set oRng = ActiveDocument.Range

    oRng.Collapse wdCollapseEnd

    ActiveDocument.AttachedTemplate.BuildingBlockEntries("Working").Insert Where:=oRng, _

        RichText:=True

Next i

For i = 1 To Me.TextBox3.Value

    Set oRng = ActiveDocument.Range

    oRng.Collapse wdCollapseEnd

    ActiveDocument.AttachedTemplate.BuildingBlockEntries("Travel From").Insert Where:=oRng, _

        RichText:=True

Next i

Unload Me

End Sub

Basically it inserts your Travel To building block from the document template as many times as you request, then as many copies of your working building block and finally as many copies of yoir travel from building block as you request.

I have tried to keep it as simple as possible to get you started. You will have to supply the correct names and locations for the building blocks

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Charles Kenyon 167.4K Reputation points Volunteer Moderator
2012-03-29T13:51:59+00:00

This is probably done with a custom dialog box or User Form.

See How to Create a User Form

The following subroutine inserts a named AutoText Entry at the insertion point in the document. It is called by other procedures.

Sub ATInsert(strEntry As String)

Dim strFull As String

On Error GoTo Err_Handler

strFull = ThisDocument.Path & "" & ThisDocument.Name

    Application.Templates( _

        strFull).BuildingBlockEntries(strEntry).Insert Where:=Selection.Range, _

         RichText:=True

Exit Sub

Err_Handler:

MsgBox "Error - AutoText entry '" & strEntry & "' does not work!", vbCritical

End Sub

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful