[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

06/30/2009 at 02:55PM PDT, ID: 24534904
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

VBA to copy Word header/footers from one doc to another is inserting unwanted paragraph at the end

Asked by WarCrimes in Microsoft Excel Spreadsheet Software, Microsoft Word

Tags: Excel VBA, Word

Hi there,

I'm pretty good with Excel VBA, but not so much when it comes to Word.  I'm using Excel VBA to do the following.  I'm copying Headers and Footers from template documents into another document, but upon doing so, there is an extra paragraph added to the end of the contents inside the header/footer.  

I tried fixing this by using the awful Select method, then doing a Selection.Paste, but that was even worse, as there were tabs inserted before the desired header/footer and it wrapped lines.

Basically, what I'd like to know is how to copy the header/footers without getting this extra paragraph, or how to easily remove it afterwards.

Below is the code I'm using, and I've attached the 3 template workbooks.  You will have to edit the paths, if you want to test it though.

Thanks in advance,
WC
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
Const template_dir As String = "C:\Documents and Settings\cvandenberg\My Documents\ALI\Word\"
Sub WriteReport()
    Const ERR_APP_NOT_RUNNING As Long = 429
    
    '*********** Use for development of code **************
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Dim wdDoc As Word.Document
    Dim wrdPDoc As Word.Document
    Dim wrdLDoc As Word.Document
    '*********** Use for deployment of code **************
    'Dim wrdApp As Object
    'Dim wrdDoc As Object
    'Dim wdDoc As Object
    '*****************************************************
    
    On Error Resume Next
    Set wrdApp = GetObject(, "Word.Application")
    If Err = ERR_APP_NOT_RUNNING Then Set wrdApp = CreateObject("Word.Application")
    Set wrdDoc = wrdApp.Documents("Template_Title_Page.doc")
    'If wrdDoc Is Nothing Then Set wrdDoc = wrdApp.Documents.Open("C:\Documents and Settings\cvandenberg\My Documents\ALI\Word\Template_Title_Page.doc")
    If Err = 4160 Then Set wrdDoc = wrdApp.Documents.Open("C:\Documents and Settings\cvandenberg\My Documents\ALI\Word\Template_Title_Page.doc")
    wrdApp.Visible = True
    Err.Clear
    
    With wrdDoc
        If PublicFunctions.FileExists("C:\Documents and Settings\cvandenberg\My Documents\MyNewWordDoc.doc") Then
            Set wdDoc = wrdApp.Documents("MyNewWordDoc.doc")
            'If Not wdDoc Is Nothing Then wdDoc.Close
            If Err <> 4160 Then wdDoc.Close
            Set wdDoc = Nothing
        End If
        
        Call insertPage(wrdApp, wrdDoc, "Landscape")
        Call insertPage(wrdApp, wrdDoc, "Portrait")
        .saveAs ("C:\Documents and Settings\cvandenberg\My Documents\MyNewWordDoc.doc")
        .Close ' close the document
    End With
    Set wrdDoc = Nothing
    
    'Only closes Word if the instance was created by this code
    If Not wrdApp.UserControl Then
        wrdApp.Quit ' close the Word application
        Set wrdApp = Nothing
    End If
    On Error GoTo 0
End Sub
 
Sub insertPage(wrdApp As Object, wrdToDoc As Object, orient As String)
    Dim wrdFromDoc As Object
    Dim pOrient As Integer
    Dim itemIndex As Integer
    
    If orient = "Landscape" Then
        pOrient = 1
        itemIndex = 1
    Else
        pOrient = 0
        itemIndex = 1
    End If
    
    On Error Resume Next
    Set wrdFromDoc = wrdApp.Documents("Template_" & orient & "_Page.doc")
    'If wrdFromDoc Is Nothing Then Set wrdFromDoc = wrdApp.Documents.Open("C:\Documents and Settings\cvandenberg\My Documents\ALI\Word\Template_" & orient & "_Page.doc")
    If Err = 4160 Then Set wrdFromDoc = wrdApp.Documents.Open(template_dir & "Template_" & orient & "_Page.doc")
    Err.Clear
 
    With wrdToDoc
        'Page Break
        With .Paragraphs(.Paragraphs.Count).Range
            .Collapse Direction:=0
            .InsertParagraphAfter
            .InsertBreak Type:=2
        End With
        With .Sections(.Sections.Count)
            With .PageSetup
                .Orientation = pOrient
                .BottomMargin = wrdFromDoc.Sections(1).PageSetup.BottomMargin
                .TopMargin = wrdFromDoc.Sections(1).PageSetup.TopMargin
                .LeftMargin = wrdFromDoc.Sections(1).PageSetup.LeftMargin
                .RightMargin = wrdFromDoc.Sections(1).PageSetup.RightMargin
            End With
            .Headers(2).LinkToPrevious = False
            .Footers(2).LinkToPrevious = False
            wrdFromDoc.Sections(1).Headers(itemIndex).Range.Copy
            .Headers(2).Range.Paste
            wrdFromDoc.Sections(1).Footers(itemIndex).Range.Copy
            .Footers(2).Range.Paste
        End With
    End With
    On Error GoTo 0
End Sub
Attachments:
 
 
 
Blank Landscape page
 
 
Blank Portrait page
 
[+][-]06/30/09 03:39 PM, ID: 24750280

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06/30/09 04:15 PM, ID: 24750481

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/30/09 04:17 PM, ID: 24750490

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/30/09 04:19 PM, ID: 24750509

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/30/09 04:26 PM, ID: 24750550

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06/30/09 06:08 PM, ID: 24750981

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Excel Spreadsheet Software, Microsoft Word
Tags: Excel VBA, Word
Sign Up Now!
Solution Provided By: WarCrimes
Participating Experts: 1
Solution Grade: A
 
 
[+][-]07/01/09 06:07 AM, ID: 24753899

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/01/09 06:25 AM, ID: 24754050

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/01/09 06:27 AM, ID: 24754059

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07/01/09 08:10 AM, ID: 24755162

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091028-EE-VQP-85 - Hierarchy / EE_QW_4_20070622