mytfein
asked on
Access 2010 executing Word VBA: Page X of Y AutoText entry in Header.... would like to replace this code
Hi EE experts,
Am revisiting a post from last year with a different question because now want to run under Windows 7/Access 2010 as mdb.
The post from last year is here:
https://www.experts-exchange.com/questions/28461432/From-Access-using-word-vba-gettting-error-462.html?searchSuccess=true&searchTop10=true
The topic is about executing from Access a Word mail merge using an Excel file as the data source.
Below attached is a zip file with the: 1) .mdb
2) excel input file
3) word mail merge template file
Today the question is about Page X OF Y.
This autotext entry does not exist in our Word 2010 installation.
Therefore the code to place Page X of Y in the Word Header VBA is aborting with error, that this item is not found in the collection.
Truthfully, I google for VBA code and cut and paste alot.... so I have gaps in my knowledge.
I found this that I cold use to replace using Autotext entry.
http://www.java2s.com/Code/VBA-Excel-Access-Word/Word/CreatingPageXofYPageNumbers.htm
They have code to do page numbering instead of Auto text, am copying the code below:
Sub pageNumber()
ActiveDocument.Sections(Ac tiveDocume nt.Section s.Count) _
.Headers(wdHeaderFooterPri mary).Rang e.Select
With Selection
.Paragraphs(1).Alignment = wdAlignParagraphCenter
.TypeText Text:="Page "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"PAGE ", PreserveFormatting:=True
.TypeText Text:=" of "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"NUMPAGES ", PreserveFormatting:=True
End With
End Sub
Can you please help me place this code in my Access module.
I tried, but getting error msg: Obect vaiable or with block variable not set.
Am uploading a zip file ... please un on Windows 7/Access 2010/Word 2010
1) mdb file
I removed forms etc from mdb to keep it simple.
Please go to module: 500_word_mail_merge
Please execute: sub_test
2) excel data file
3)word template file
The logic takes the whole merge file, and subdivides into smaller docs per each student (an EE expert helped me with the subdividing logic in the past)
4) 3 sample student files that were subdivided from the big merge document:
1) Thomas Jefferson
2) Abraham Lincoln
3) George Washington
5) An xp version of (4) where you see how would like the header to look like:
nothing on the first page
the each subsequent page to have in the header Page n of o Re: Student's name
If you can only get the logic to work if you do place on the first page header, too.... that would be fine.
** At this time I could not upload the zip file bec getting msg: The archive could not be scanned stream closed.
The zip file from last year in the link listed at the top of this post could be used.
This is the new code I am trying to add that's giving me an error.
Set myRange = appWord.ActiveDocument.Sec tions(1).H eaders(wdH eaderFoote rPrimary). Range
' With myRange
' ' put page number in header
' appWord.NormalTemplate.Aut oTextEntri es("PAGE x of y").Insert _
' Where:=myRange, _
' RichText:=True
' .Move Unit:=wdstory, Count:=1
' ' put student name in header
' .InsertAfter " " & strHeaderText
'
' End With
With myRange
With appWord.Selection
'put page number in header
.Paragraphs(1).Alignment = wdAlignParagraphCenter
.TypeText Text:="Page: "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"PAGE ", PreserveFormatting:=True
.TypeText Text:=" of "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"NUMPAGES ", PreserveFormatting:=True
' .Move Unit:=wdstory, Count:=1
' put student name in header
.InsertAfter " " & strHeaderText
End With
End With
Thx so much for any help you can provide,
sandra
Am revisiting a post from last year with a different question because now want to run under Windows 7/Access 2010 as mdb.
The post from last year is here:
https://www.experts-exchange.com/questions/28461432/From-Access-using-word-vba-gettting-error-462.html?searchSuccess=true&searchTop10=true
The topic is about executing from Access a Word mail merge using an Excel file as the data source.
Below attached is a zip file with the: 1) .mdb
2) excel input file
3) word mail merge template file
Today the question is about Page X OF Y.
This autotext entry does not exist in our Word 2010 installation.
Therefore the code to place Page X of Y in the Word Header VBA is aborting with error, that this item is not found in the collection.
Truthfully, I google for VBA code and cut and paste alot.... so I have gaps in my knowledge.
I found this that I cold use to replace using Autotext entry.
http://www.java2s.com/Code/VBA-Excel-Access-Word/Word/CreatingPageXofYPageNumbers.htm
They have code to do page numbering instead of Auto text, am copying the code below:
Sub pageNumber()
ActiveDocument.Sections(Ac
.Headers(wdHeaderFooterPri
With Selection
.Paragraphs(1).Alignment = wdAlignParagraphCenter
.TypeText Text:="Page "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"PAGE ", PreserveFormatting:=True
.TypeText Text:=" of "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"NUMPAGES ", PreserveFormatting:=True
End With
End Sub
Can you please help me place this code in my Access module.
I tried, but getting error msg: Obect vaiable or with block variable not set.
Am uploading a zip file ... please un on Windows 7/Access 2010/Word 2010
1) mdb file
I removed forms etc from mdb to keep it simple.
Please go to module: 500_word_mail_merge
Please execute: sub_test
2) excel data file
3)word template file
The logic takes the whole merge file, and subdivides into smaller docs per each student (an EE expert helped me with the subdividing logic in the past)
4) 3 sample student files that were subdivided from the big merge document:
1) Thomas Jefferson
2) Abraham Lincoln
3) George Washington
5) An xp version of (4) where you see how would like the header to look like:
nothing on the first page
the each subsequent page to have in the header Page n of o Re: Student's name
If you can only get the logic to work if you do place on the first page header, too.... that would be fine.
** At this time I could not upload the zip file bec getting msg: The archive could not be scanned stream closed.
The zip file from last year in the link listed at the top of this post could be used.
This is the new code I am trying to add that's giving me an error.
Set myRange = appWord.ActiveDocument.Sec
' With myRange
' ' put page number in header
' appWord.NormalTemplate.Aut
' Where:=myRange, _
' RichText:=True
' .Move Unit:=wdstory, Count:=1
' ' put student name in header
' .InsertAfter " " & strHeaderText
'
' End With
With myRange
With appWord.Selection
'put page number in header
.Paragraphs(1).Alignment = wdAlignParagraphCenter
.TypeText Text:="Page: "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"PAGE ", PreserveFormatting:=True
.TypeText Text:=" of "
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"NUMPAGES ", PreserveFormatting:=True
' .Move Unit:=wdstory, Count:=1
' put student name in header
.InsertAfter " " & strHeaderText
End With
End With
Thx so much for any help you can provide,
sandra
ASKER
Friggin' Ribbon :(
This autotext entry does not exist in our Word 2010 installation.
Since it does exist in Word 2013, it is very likely that it is still there in 2010.
And I checked, and it is.
But it has been brutally hidden.
The Insert Ribbon has a Page Number dropdown.
Choose where you want Page X of Y
Now there look to be a bazillion formats
Look VERY CAREFULLY at each item
They start with Simple
and then Page X
And then you'll see that somewhere along the way they switch to formats for PAGE X of Y
So your initial premise is wrong.
Does that change what you want to do?
This autotext entry does not exist in our Word 2010 installation.
Since it does exist in Word 2013, it is very likely that it is still there in 2010.
And I checked, and it is.
But it has been brutally hidden.
The Insert Ribbon has a Page Number dropdown.
Choose where you want Page X of Y
Now there look to be a bazillion formats
Look VERY CAREFULLY at each item
They start with Simple
and then Page X
And then you'll see that somewhere along the way they switch to formats for PAGE X of Y
So your initial premise is wrong.
Does that change what you want to do?
ASKER
Hi Nick,
tx for replying,
Do not want to use the AutoText anymore....
found another sample how to code the page numbers using VBA
it works better, but still having problems
this is the code snippet so far:
With appWord.ActiveDocument.Sec tions(1)
Set myRange = .Headers(wdHeaderFooterPri mary).Rang e
myRange.InsertAfter Text:="Page: "
' myRange.Collapse Direction:=wdCollapseEnd
myRange.Fields.Add Range:=myRange, Type:=wdFieldPage
myRange.InsertAfter Text:=" of "
' myRange.Collapse Direction:=wdCollapseEnd
myRange.Fields.Add Range:=myRange, Type:=wdFieldNumPages
End With
I was inspired by:
http://windowssecrets.com/forums/showthread.php/114424-VBA-Word-Footer-(VBA-Word-2007)
I was about to google what collapse does, bec it's coming out as Page: of 52.... it should be Page: 2 of 5
Below is a screen shot of what header looks like with the above code, it should be Page: 2 of 5
Header-pages.bmp
tx for replying,
Do not want to use the AutoText anymore....
found another sample how to code the page numbers using VBA
it works better, but still having problems
this is the code snippet so far:
With appWord.ActiveDocument.Sec
Set myRange = .Headers(wdHeaderFooterPri
myRange.InsertAfter Text:="Page: "
' myRange.Collapse Direction:=wdCollapseEnd
myRange.Fields.Add Range:=myRange, Type:=wdFieldPage
myRange.InsertAfter Text:=" of "
' myRange.Collapse Direction:=wdCollapseEnd
myRange.Fields.Add Range:=myRange, Type:=wdFieldNumPages
End With
I was inspired by:
http://windowssecrets.com/forums/showthread.php/114424-VBA-Word-Footer-(VBA-Word-2007)
I was about to google what collapse does, bec it's coming out as Page: of 52.... it should be Page: 2 of 5
Below is a screen shot of what header looks like with the above code, it should be Page: 2 of 5
Header-pages.bmp
ASKER
When I added code to place Student's name, it got more jumbled in the header
With appWord.ActiveDocument.Sec tions(1)
Set myRange = .Headers(wdHeaderFooterPri mary).Rang e
myRange.InsertAfter Text:="Page: "
myRange.Collapse Direction:=wdCollapseEnd
myRange.Fields.Add Range:=myRange, Type:=wdFieldPage
myRange.InsertAfter Text:=" of "
myRange.Collapse Direction:=wdCollapseEnd
myRange.Fields.Add Range:=myRange, Type:=wdFieldNumPages
myRange.InsertAfter Text:=strHeaderText
End With
I do not know how to control the placement of the fields.... If you have any info how to do, would greatly appreciate....
will google in the meantime... tx,
sandra
With appWord.ActiveDocument.Sec
Set myRange = .Headers(wdHeaderFooterPri
myRange.InsertAfter Text:="Page: "
myRange.Collapse Direction:=wdCollapseEnd
myRange.Fields.Add Range:=myRange, Type:=wdFieldPage
myRange.InsertAfter Text:=" of "
myRange.Collapse Direction:=wdCollapseEnd
myRange.Fields.Add Range:=myRange, Type:=wdFieldNumPages
myRange.InsertAfter Text:=strHeaderText
End With
I do not know how to control the placement of the fields.... If you have any info how to do, would greatly appreciate....
will google in the meantime... tx,
sandra
ASKER
Hi Nick,
Found this link, and followed the recommendations.....
It works for me now....
It's just not clear to me why have to do collapse to the end
and the link below, he recommends after adding a field, to set the range again before adding another field
' http://stackoverflow.com/questions/14747261/inserting-text-and-fields-in-word-2010-header-without-using-select
With appWord.ActiveDocument.Sec tions(1)
Set myRange = .Headers(wdHeaderFooterPri mary).Rang e
myRange.InsertAfter Text:="Page: "
myRange.Collapse Direction:=wdCollapseEnd
myRange.InsertAfter Text:=" "
myRange.Collapse Direction:=wdCollapseEnd
myRange.Fields.Add Range:=myRange, Type:=wdFieldPage
myRange.Collapse Direction:=wdCollapseEnd
myRange.InsertAfter Text:=" "
myRange.Collapse Direction:=wdCollapseEnd
Set myRange = .Headers(wdHeaderFooterPri mary).Rang e
myRange.InsertAfter Text:=" of "
myRange.Collapse Direction:=wdCollapseEnd
myRange.InsertAfter Text:=" "
myRange.Collapse Direction:=wdCollapseEnd
myRange.Fields.Add Range:=myRange, Type:=wdFieldNumPages
myRange.Collapse Direction:=wdCollapseEnd
myRange.InsertAfter Text:=" "
myRange.Collapse Direction:=wdCollapseEnd
Set myRange = .Headers(wdHeaderFooterPri mary).Rang e
myRange.InsertAfter Text:=" " & strHeaderText
myRange.Collapse Direction:=wdCollapseEnd
myRange.Fields.Update
End With
Below is the screen shot.... creating pages in header without using the autotextentry.... using more vba code....
Found this link, and followed the recommendations.....
It works for me now....
It's just not clear to me why have to do collapse to the end
and the link below, he recommends after adding a field, to set the range again before adding another field
' http://stackoverflow.com/questions/14747261/inserting-text-and-fields-in-word-2010-header-without-using-select
With appWord.ActiveDocument.Sec
Set myRange = .Headers(wdHeaderFooterPri
myRange.InsertAfter Text:="Page: "
myRange.Collapse Direction:=wdCollapseEnd
myRange.InsertAfter Text:=" "
myRange.Collapse Direction:=wdCollapseEnd
myRange.Fields.Add Range:=myRange, Type:=wdFieldPage
myRange.Collapse Direction:=wdCollapseEnd
myRange.InsertAfter Text:=" "
myRange.Collapse Direction:=wdCollapseEnd
Set myRange = .Headers(wdHeaderFooterPri
myRange.InsertAfter Text:=" of "
myRange.Collapse Direction:=wdCollapseEnd
myRange.InsertAfter Text:=" "
myRange.Collapse Direction:=wdCollapseEnd
myRange.Fields.Add Range:=myRange, Type:=wdFieldNumPages
myRange.Collapse Direction:=wdCollapseEnd
myRange.InsertAfter Text:=" "
myRange.Collapse Direction:=wdCollapseEnd
Set myRange = .Headers(wdHeaderFooterPri
myRange.InsertAfter Text:=" " & strHeaderText
myRange.Collapse Direction:=wdCollapseEnd
myRange.Fields.Update
End With
Below is the screen shot.... creating pages in header without using the autotextentry.... using more vba code....
WOW!
MS should be shot!
This nice code works in Word 2003
This is it's Word 2013 replacement
ActiveWindow.View.SplitSpe cial = wdPaneNone
ActiveWindow.ActivePane.Vi ew.Type = wdPrintView
ActiveWindow.ActivePane.Vi ew.SeekVie w = wdSeekCurrentPageHeader
Selection.ParagraphFormat. Alignment = wdAlignParagraphCenter
Application.Templates( _
"C:\Users\username\AppData\Roaming\Microsoft \Document Building Blocks\1033\15\Built-In Building Blocks.dotx" _
).BuildingBlockEntries("Bo ld Numbers 1").Insert Where:=Selection.Range, RichText:=True
Just WOW!
Ok, this works in Word
Does that help
MS should be shot!
This nice code works in Word 2003
ActiveWindow.View.SplitSpecial = wdPaneNone
ActiveWindow.ActivePane.View.Type = wdPrintView
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
NormalTemplate.AutoTextEntries("Page X of Y").Insert Where:=Selection.Range, RichText:=True
This is it's Word 2013 replacement
ActiveWindow.View.SplitSpe
ActiveWindow.ActivePane.Vi
ActiveWindow.ActivePane.Vi
Selection.ParagraphFormat.
Application.Templates( _
"C:\Users\username\AppData\Roaming\Microsoft
).BuildingBlockEntries("Bo
Just WOW!
Ok, this works in Word
ActiveWindow.View.SplitSpecial = wdPaneNone
ActiveWindow.ActivePane.View.Type = wdPrintView
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.TypeText Text:="Page "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="PAGE ", PreserveFormatting:=True
Selection.TypeText Text:=" of "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="NUMPAGES ", PreserveFormatting:=True
Does that help
ASKER
Hi Nick,
Yes you are right when you mentioned before that the AutoText is in the ribbon, with a gazillion variations.
But, I am rusty and new to Office 2010 so working with it felt overwhelming.
Another website mentioned, if you delete the Normal.dot (now Normal.dotm?), it can pull it in.... but when I discovered that the Normal.dot sits in each person's individual user folder, preferred to create the page number using vba code.
Question:
Did you simply turn on the macro recorder, and pick a choice from one of the gazillion page options, and then copy the code(bec i forgot that approach) ?
I get into trouble when I used the vba behind the macro recorder. Because I do not know how to accurately defie object in Access, so that it does not tie up memory on the next iteration of running the code, which is what you solved last year.
So it's almost the end of the day here, so will try your technique tommorrow.
I may or might not have success with transforming it for Access vba module.
Will be in touch....
I loved Access 97, 2000, 2003..... :-)
tx, sandra
Yes you are right when you mentioned before that the AutoText is in the ribbon, with a gazillion variations.
But, I am rusty and new to Office 2010 so working with it felt overwhelming.
Another website mentioned, if you delete the Normal.dot (now Normal.dotm?), it can pull it in.... but when I discovered that the Normal.dot sits in each person's individual user folder, preferred to create the page number using vba code.
Question:
Did you simply turn on the macro recorder, and pick a choice from one of the gazillion page options, and then copy the code(bec i forgot that approach) ?
I get into trouble when I used the vba behind the macro recorder. Because I do not know how to accurately defie object in Access, so that it does not tie up memory on the next iteration of running the code, which is what you solved last year.
So it's almost the end of the day here, so will try your technique tommorrow.
I may or might not have success with transforming it for Access vba module.
Will be in touch....
I loved Access 97, 2000, 2003..... :-)
tx, sandra
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Nick,
Totally agree and understand what you wrote above about word/excel vba and google is one's friend :-)
I tested the mdb that you graciously provided and it works!
The things is, in the past my user does not want a header on first page, just subsequent pages which the vba that i posted yesterday does....
I have to ask user if header on first page is ok....
I was told that on Jun 1 work is cutting off XP pcs from the network. My users are remoting in from their Windows 7 to an XP pc to use my Access 2003 system. I am testing the mdbs on A2010 and am finding some issues. Most of testing is done....
So hope it's ok with you if i retain the logic that works from yesterday so that I can continue testing.
(I see already the next bug for the same mail merge project that I need to post again)
thx soooo much for your help, always learning something new :-)
sandra
Totally agree and understand what you wrote above about word/excel vba and google is one's friend :-)
I tested the mdb that you graciously provided and it works!
The things is, in the past my user does not want a header on first page, just subsequent pages which the vba that i posted yesterday does....
I have to ask user if header on first page is ok....
I was told that on Jun 1 work is cutting off XP pcs from the network. My users are remoting in from their Windows 7 to an XP pc to use my Access 2003 system. I am testing the mdbs on A2010 and am finding some issues. Most of testing is done....
So hope it's ok with you if i retain the logic that works from yesterday so that I can continue testing.
(I see already the next bug for the same mail merge project that I need to post again)
thx soooo much for your help, always learning something new :-)
sandra
ASKER
I've requested that this question be closed as follows:
Accepted answer: 500 points for Nick67's comment #a40799269
Assisted answer: 0 points for mytfein's comment #a40799112
for the following reason:
The code that I created using various websites as inspiration is another approach to headers ( a little clunky, but let's you control if you do not want a header on first page)
Accepted answer: 500 points for Nick67's comment #a40799269
Assisted answer: 0 points for mytfein's comment #a40799112
for the following reason:
The code that I created using various websites as inspiration is another approach to headers ( a little clunky, but let's you control if you do not want a header on first page)
ASKER
2015-05-25-EE-ARG-05-May-MSPE-frontend.m