jasoncw
07-27-2007, 10:45 AM
Hi, everyone. New guy here. I am mildly proficient in VBA, although most of my programming has been in Excel. I am having an issue in Word trying to merge in a letterhead template.
Here is a quick explanation of what I am trying to accomplish:
We have a generic document that is going to be used by many different offices. Each office has their own letterhead, each of which are stored on our intranet. I am trying to develop a procedure that will allow the user to select their office, then apply the letterhead to the document. The letterhead templates are nothing more than blank Word templates with an image file in the header.
What I was trying to do is open the template (*.dot), copy the image from the header, and paste into the current document. Here is the code I am using:
Private Sub cmdLetterhead_Click()
Dim tmpDir As String, tmpFile As String, tmpFileRev As String
Dim thisDoc As Document
Set thisDoc = ActiveDocument
tmpDir = "...myintranet/letterhead/" 'edited for forum use
frmOffice.Show 'user form for users to select office
If strOffice = "" Then 'no office selected
MsgBox "You must select an office in order to apply the letterhead.", vbCritical, _
"Letterhead Error"
Exit Sub
End If
Select Case strOffice
Case "Bohemia, NY": tmpFile = "NY%20PA%2025%20855.4356.dot"
Case "Camarillo, CA": tmpFile = "CA%205151%20380.4745.dot"
Case "Charleston, WV": tmpFile = "WV%20PA%20300%20340.5251.dot"
Case "Chicago, IL": tmpFile = "IL%20PA%20225%20419.9457.dot"
Case "Cincinnati, OH": tmpFile = "OH%20PA%20801A%20419.9457.dot"
Case "Cleveland, OH": tmpFile = "OH%20PA%208333%20419.9457.dot"
Case "Indianapolis, IN": tmpFile = "IN%20PA%208115%20419.9457.dot"
Case "Jericho, NY": tmpFile = "NY%20PA%20100%20855.4356.dot"
Case "Lansing, MI": tmpFile = "MI%20PA%203815%20702.3240.dot"
Case "Louisville, KY": tmpFile = "KY%20PA%209901%20419.9457.dot"
Case "Middletown, CT": tmpFile = "CT%20PA%20100%20855.4356.dot"
Case "Milwaukee, WI": tmpFile = "WI%20PA%206775%20883.0130%20IVR.dot"
Case "Mt. Vernon, IL": tmpFile = "IL%20PA%204203%20244.4770.dot"
Case "Oakland, CA": tmpFile = "CA%20PA%207677%20633.4200.dot"
Case "Portland, ME": tmpFile = "ME%20PA%20PO919.dot"
Case "Richmond, VA": tmpFile = "VA%20PA%208002%20521.2300.dot"
Case "Springfield, IL": tmpFile = "MI%20PA%2026555%20355.7850.dot"
Case "Southfield, MI": tmpFile = "IL%20PA%203200%20419.9457.dot"
Case "Syracuse, NY": tmpFile = "NY%20PA%20400%20855.4356.dot"
Case "Toledo, OH": tmpFile = "OH%20PA%206056%20419.9457.dot"
Case "Yorktown Heights, NY": tmpFile = "NY%20PA%202651%20855.4356.dot"
End Select
Application.ScreenUpdating = False
Documents.Open tmpDir & tmpFile
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.Shapes(1).Select
Selection.Copy
ActiveDocument.Close False
Documents(thisDoc).Activate
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.Paste
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Application.ScreenUpdating = True
End Sub
The bolded lines above are where I am stuck. The problem is that when the template is opened, and I try to access the header, the header is accessed on the original document, rather than the template. Therefore, there is no image file, so it obviously can't copy. I have stepped through this, and the template is the active window, so I'm not sure why it will not allow me to access the header in the template.
I hope I explained this adequately. If not, please let me know. Also, if there is an easier or more efficient way of accomplishing this, I'm all ears. :)
TIA
Jason
Here is a quick explanation of what I am trying to accomplish:
We have a generic document that is going to be used by many different offices. Each office has their own letterhead, each of which are stored on our intranet. I am trying to develop a procedure that will allow the user to select their office, then apply the letterhead to the document. The letterhead templates are nothing more than blank Word templates with an image file in the header.
What I was trying to do is open the template (*.dot), copy the image from the header, and paste into the current document. Here is the code I am using:
Private Sub cmdLetterhead_Click()
Dim tmpDir As String, tmpFile As String, tmpFileRev As String
Dim thisDoc As Document
Set thisDoc = ActiveDocument
tmpDir = "...myintranet/letterhead/" 'edited for forum use
frmOffice.Show 'user form for users to select office
If strOffice = "" Then 'no office selected
MsgBox "You must select an office in order to apply the letterhead.", vbCritical, _
"Letterhead Error"
Exit Sub
End If
Select Case strOffice
Case "Bohemia, NY": tmpFile = "NY%20PA%2025%20855.4356.dot"
Case "Camarillo, CA": tmpFile = "CA%205151%20380.4745.dot"
Case "Charleston, WV": tmpFile = "WV%20PA%20300%20340.5251.dot"
Case "Chicago, IL": tmpFile = "IL%20PA%20225%20419.9457.dot"
Case "Cincinnati, OH": tmpFile = "OH%20PA%20801A%20419.9457.dot"
Case "Cleveland, OH": tmpFile = "OH%20PA%208333%20419.9457.dot"
Case "Indianapolis, IN": tmpFile = "IN%20PA%208115%20419.9457.dot"
Case "Jericho, NY": tmpFile = "NY%20PA%20100%20855.4356.dot"
Case "Lansing, MI": tmpFile = "MI%20PA%203815%20702.3240.dot"
Case "Louisville, KY": tmpFile = "KY%20PA%209901%20419.9457.dot"
Case "Middletown, CT": tmpFile = "CT%20PA%20100%20855.4356.dot"
Case "Milwaukee, WI": tmpFile = "WI%20PA%206775%20883.0130%20IVR.dot"
Case "Mt. Vernon, IL": tmpFile = "IL%20PA%204203%20244.4770.dot"
Case "Oakland, CA": tmpFile = "CA%20PA%207677%20633.4200.dot"
Case "Portland, ME": tmpFile = "ME%20PA%20PO919.dot"
Case "Richmond, VA": tmpFile = "VA%20PA%208002%20521.2300.dot"
Case "Springfield, IL": tmpFile = "MI%20PA%2026555%20355.7850.dot"
Case "Southfield, MI": tmpFile = "IL%20PA%203200%20419.9457.dot"
Case "Syracuse, NY": tmpFile = "NY%20PA%20400%20855.4356.dot"
Case "Toledo, OH": tmpFile = "OH%20PA%206056%20419.9457.dot"
Case "Yorktown Heights, NY": tmpFile = "NY%20PA%202651%20855.4356.dot"
End Select
Application.ScreenUpdating = False
Documents.Open tmpDir & tmpFile
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.Shapes(1).Select
Selection.Copy
ActiveDocument.Close False
Documents(thisDoc).Activate
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.Paste
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Application.ScreenUpdating = True
End Sub
The bolded lines above are where I am stuck. The problem is that when the template is opened, and I try to access the header, the header is accessed on the original document, rather than the template. Therefore, there is no image file, so it obviously can't copy. I have stepped through this, and the template is the active window, so I'm not sure why it will not allow me to access the header in the template.
I hope I explained this adequately. If not, please let me know. Also, if there is an easier or more efficient way of accomplishing this, I'm all ears. :)
TIA
Jason