I have a 7 page document and via vba i would like to
a) insert a new page
b) insert autotext on that page
Would someone be so kind as to provide me with the VBA code on how i do this?
I have a 7 page document and via vba i would like to
a) insert a new page
b) insert autotext on that page
Would someone be so kind as to provide me with the VBA code on how i do this?
Provide you? Hmmm, I suppose we could, but it would be better for you to actually try and do it yourself. If you have a problem, post a specific question.
Try writing out precisely what it is you want to do. For example:
"insert a new page"
Ummm, insert it WHERE...exactly? Insert it at the start of the document? Insert it at the end of the document? Insert it in the middle of the document? Insert it somewhere you are going to specify? Insert it at the current cursor (Selection) location? Insert it with another page break following, to keep it separate? Insert it with no following page break?
Once you have that figured out, try using the macro recorder. Then look at the code it produces. We can help you if you have something specific that does not seem to be working the way you want.
Hi fumei
Thank you for responding to my Thread.
There are 7 pages in my document and I have a frame with 5 check boxes representing the 5 pages that i need to insert into my document. If the chkbox value =0 then it is to delete the selected page otherwise it is to insert the selected page
I created two functions hereunder where one is to delete the page and the other to insert the page. Once the page has been inserted then it needs to paste the autotext information.
usage: when the chkbox is checked then
InsertPage 2 for example
[VBA]
Sub InsertPage(iNum As Integer)
On Error GoTo Err_Handler
Dim Mycounter As Long
For Mycounter = 7 To 1 Step -1
If Mycounter = iNum + 1 Then
Selection.GoTo what:=wdGoToPage, which:=wdGoToAbsolute, Count:=Mycounter
ActiveDocument.Bookmarks("\page").Range.Select
Selection.InsertBreak Type:=wdPageBreak
Selection.GoTo what:=wdGoToPage, which:=wdGoToAbsolute, Count:=iNum - 1
Select Case iNum
'Selection.InsertBreak Type:=wdPageBreak
Case 1:
ActiveDocument.AttachedTemplate.AutoTextEntries("Table Of Contents").Insert Where:=Selection.Range, RichText:=True
Exit For
Case 2:
ActiveDocument.AttachedTemplate.AutoTextEntries("List of Tables").Insert Where:=Selection.Range, RichText:=True
Exit For
Case 3:
ActiveDocument.AttachedTemplate.AutoTextEntries("List of Figures").Insert Where:=Selection.Range, RichText:=True
Exit For
Case 4:
ActiveDocument.AttachedTemplate.AutoTextEntries("List of Appendices").Insert Where:=Selection.Range, RichText:=True
Exit For
Case 5:
ActiveDocument.AttachedTemplate.AutoTextEntries("List of Schedules").Insert Where:=Selection.Range, RichText:=True
Exit For
End Select
Exit For
End If
Next Mycounter
'Selection.InsertBreak Type:=wdSectionBreakContinuous
Exit Sub
Err_Handler:
MsgBox Error(Err)
Resume
End Sub
[/VBA]
The checkboxes if unchecked are to remove the selected page from the document
Useage DeletePate "LIST OF FIGURES"
[VBA]
Sub DeletePage(sText As String)
On Error GoTo Err_Handler
Dim Mycounter As Long
Dim iPages As Integer
Application.ScreenUpdating = False
'iPages = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
iPages = ActiveDocument.ActiveWindow.Panes(1).Pages.Count
For Mycounter = iPages To 1 Step -1
Selection.GoTo what:=wdGoToPage, which:=wdGoToAbsolute, Count:=Mycounter
If UCase$(ActiveDocument.Bookmarks("\Page").Range.text) Like "*" & UCase$(sText) & "*" Then
ActiveDocument.Bookmarks("\page").Range.Select
ActiveDocument.Bookmarks("\Page").Range.Delete
MsgBox sText & " has been Removed"
'End If
Exit For
End If
Next Mycounter
Application.ScreenUpdating = True
Exit Sub
Err_Handler:
MsgBox Error(Err)
Resume
End Sub
[/VBA]
Hope that helps.
I sincerely appreciate your help.
timark, is this a document that someone has sent to you or is it something you are developing?
ps when posting code, select it and hit the vba button and it will be formatted for the forum as I have done to your code above.
Steve
"Nearly all men can stand adversity, but if you want to test a man's character, give him power."
-Abraham Lincoln
Hi Lucas
No that is what im developing ....i pasted it for fumei who requested it
If you are developing this then why not have everything in the document and just delete the parts you don't want each time using radio buttons or some control to make the selection?
Steve
"Nearly all men can stand adversity, but if you want to test a man's character, give him power."
-Abraham Lincoln
because i need the option to reinsert a page if the user checks the check box for that page
I got your pm so here is an idea for you. Check out the attachment and then get back with us. When you open the file you will get a userform and you can select which chunks you want included in the letter....doesn't affect formatting etc so it's a very useful method. One that I learned from Gerry in this forum.
My suggestion would be to use this as a template so each time you open it a brand new document is created and you just re-use the template each time you need the document. Save the created doc and it has no code although it is linked to the code in your machine but if you send it out it will have no code of it's own.
If I understand, you also may have a need to put some of this back in after some decision is made later. That could be hard coded in if necessary. Take a look and see if this looks like something that might work for your need at least as a starting point.
Steve
"Nearly all men can stand adversity, but if you want to test a man's character, give him power."
-Abraham Lincoln
Each of the chunks can be your page as they are contained within bookmarks. go to tools-options select the view tab and check the box next to bookmarks to see the bookmarks in the document.
Steve
"Nearly all men can stand adversity, but if you want to test a man's character, give him power."
-Abraham Lincoln
? im not sure what you mean. once a user removes a page then that page is not accessible. i need to insert a new page at a specific place and then use autotext to insert the information of the page
You can insert formatted text to the selection using something like:
[VBA]Sub SoAndSo()
With Selection
.TypeText Text:="Sincerely," & vbCrLf & vbCrLf
.Font.Bold = wdToggle
.TypeText Text:="Your Company Name Here"
.Font.Bold = wdToggle
.TypeText Text:=vbCrLf & vbCrLf & vbCrLf & vbCrLf & _
"So and So" & vbCrLf & "Certified Know it all!"
End With
End Sub[/VBA]
Steve
"Nearly all men can stand adversity, but if you want to test a man's character, give him power."
-Abraham Lincoln
how do i insert a page for example i need to insert a page between pages 4 and 5
[VBA]
Sub InsertPage()
Dim r As Range
Set r = ActiveDocument.Range
With r
.Collapse Direction:=wdCollapseEnd
.InsertBreak Type:=wdPageBreak
End With
Set r = Nothing
End Sub
[/VBA]
Steve
"Nearly all men can stand adversity, but if you want to test a man's character, give him power."
-Abraham Lincoln
thanks...but would i need to enter a page number as i need to insert this page before page 5
pages in word are not fixed. It varies according to each machine. It is determined by the printer drivers I believe.
Steve
"Nearly all men can stand adversity, but if you want to test a man's character, give him power."
-Abraham Lincoln
That's why you would want to use bookmarks. That's what they are for.
Steve
"Nearly all men can stand adversity, but if you want to test a man's character, give him power."
-Abraham Lincoln
hmm.. could i not get to a specific page using the following
Selection.GoTo what:=wdGoToPage, which:=wdGoToAbsolute, Count:=4
Then use your code
Dim r As Range
Set r = ActiveDocument.Range
With r
.Collapse Direction:=wdCollapseEnd
.InsertBreak Type:=wdPageBreak
End With
Set r = Nothing
to insert your auto text:
[VBA]Option Explicit
'selection
Sub a()
NormalTemplate.AutoTextEntries("Author, Page #, Date").Insert Where:= _
Selection.Range, RichText:=True
End Sub
'bookmark
Sub b()
NormalTemplate.AutoTextEntries("Author, Page #, Date").Insert Where:= _
ActiveDocument.Bookmarks("example1").Range, RichText:=True
End Sub
[/VBA]
You are going to have to detail out exactly what you are trying to do. I am lost.
Steve
"Nearly all men can stand adversity, but if you want to test a man's character, give him power."
-Abraham Lincoln
okay this is what i need to do
i have a document template with 7 pages (2 of which are fixed)
in the vba userform i have a frame with 5 check boxes (one for each of the required pages)
as an example the user clicks a check box e.g. page 3 it removes it from the document
now if the user want to reinstate that page he clicks the check box again and then the page should be inserted into the document.
so basically it is a checkbox with the ability to remove and replace the page in the document.
if the checkbox is true then the page is to be included in the document
if the checkbox is false then the page is to re removed in the document
Would you post your example minus any important info so we don't have to re create it?
Can the individual page info be autotext?
Steve
"Nearly all men can stand adversity, but if you want to test a man's character, give him power."
-Abraham Lincoln