none
Word 2010 Header/Footer is not exporting to the new drw_notes.docx RRS feed

  • Question

  • I have added code to insert a header/footer into the new document that is created (drw_notes.docx) however it is not working.  Do I have the code in the incorrect spot?

    here is the existing code:

    Private Sub Document_Close()
        Call ExportCheckedItemsToNewFile
    End Sub

    'The main method
    Sub ExportCheckedItemsToNewFile()
        conf = MsgBox("It'll take some time to export checked items into a new file, do you want to continue?", vbYesNo, "Confirmation")
        If conf = vbNo Then
            Exit Sub
        End If

        Dim i%, j%, content$, newFileName$
        newFileName = "C:/Common/drw_notes.docx"
       
        'Determine if the new file exists, if it doesn't, create a new one
        If FileExists(newFileName) = False Then
            Set newWord = CreateObject("Word.Application")
            newWord.Visible = False
            Set newDoc = newWord.Documents.Add
            newDoc.SaveAs (newFileName)
            newDoc.Close
            newWord.Quit
        End If
       
        'Open the new file, prepare to export the checked items into it
        Set newWord = CreateObject("Word.Application")
        newWord.Visible = False
        Set newDoc = newWord.Documents.Open(newFileName)
       
        'Enumerate all the Checkbox content controls
        For i = 1 To ActiveDocument.ContentControls.Count
            If ActiveDocument.ContentControls(i).Type = wdContentControlCheckBox Then
                If ActiveDocument.ContentControls(i).Checked Then
                   
                    'Determine if this is the last Checkbox in the document
                    If i < ActiveDocument.ContentControls.Count Then
                        startRange = ActiveDocument.ContentControls(i).Range.End
                        endRange = ActiveDocument.ContentControls(i + 1).Range.Start
                        content = ActiveDocument.Range(startRange, endRange).Text
                    ElseIf i = ActiveDocument.ContentControls.Count Then
                        startRange = ActiveDocument.ContentControls(i).Range.End
                        endRange = ActiveDocument.Range.End
                        content = ActiveDocument.Range(startRange, endRange).Text
                    End If

                    j = j + 1
                    'Export the checked items into the new file
                    If content Like "FN*" Then
                        content = ReplaceFirstFoundString(content, "FN[0-9]+", "FN" & j)
                    ElseIf content Like "GN*" Then
                        content = ReplaceFirstFoundString(content, "GN[0-9]+", "GN" & j)
                    End If
                   
                    newDoc.content.InsertAfter content & Chr(10)
                    newDoc.Save

                End If
            Else
                'Make sure there're no other type of content controls in the document
                MsgBox "There are Non-Checkbox content controls in this document."
            End If
        Next
       
        With ActiveDocument.Sections(1)
            .Headers(wdHeaderFooterPrimary).Range.Text = "Drawing = 2UXXXXXX      FN = Flag Note Symbol   GN = General Note"
            .Footers(wdHeaderFooterPrimary).Range.Text = "ATK Proprietary"
           
       
        newDoc.Close
        newWord.Quit
        MsgBox "Complete! Please check file:" & newFileName
        ActiveDocument.Save
    End Sub

    'Check if a file exists
    Private Function FileExists(filename As String) As Boolean
        Dim fso As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        If fso.FileExists(filename) Then FileExists = True Else FileExists = False
    End Function

    'Use regular expression to replace a found string to another string
    Private Function ReplaceFirstFoundString(srcString As String, pattern As String, replaceString As String) As String
        Set objRegEx = CreateObject("vbscript.regexp")
        objRegEx.Global = True
        objRegEx.IgnoreCase = True
        objRegEx.MultiLine = True
        objRegEx.pattern = pattern
       
        ReplaceFirstFoundString = objRegEx.Replace(srcString, replaceString)
    End Function

     

    Tuesday, January 28, 2014 7:15 PM

Answers

  • Thank you for your comments and I will check into it.  However, the code is working great other than trying to figure out how to insert the Header/Footer into the new document that is created. 


    well, as I've said twice, now, do a newDoc.SAVE after the lines that insert the header/footer and I imagine it will be there...

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by tridda Monday, February 3, 2014 6:18 PM
    Wednesday, January 29, 2014 6:12 PM
    Moderator

All replies

  • Hello tridda,

    Did you try to use the Macro recorder for generating the required code automatically?

    Tuesday, January 28, 2014 7:20 PM
  • I'm not sure how to do this when the headers and footers are for the new document that is being created.
    Wednesday, January 29, 2014 1:45 PM
  • Hi tridda

    I'd like some more background information on this, please

    1. In what environment is your code running? Is this Word VBA, or something else?

    If Word VBA, why are you creating new instances of the Word.Application, then destroying them? This is resource intensive and, as far as I can see, completely unnecessary...

    2. What do you expect ActiveDocument to be? Is it supposed to be the document that has triggered DocumentBeforeClose?

    3. Is there a reason you're uisng For...Next instead of For Each...Next to loop the content controls?

    4. Are all the content controls in the document checkboxes? At that start, you assume they are not, but later it appears you assume they are ( If i < ActiveDocument.ContentControls.Count Then )?

    EDIT (hit Submit by mistake)

    5. Why do you do this: content = ActiveDocument.Range(startRange, endRange).Text
    Instead of this: content =  ActiveDocument.ContentControls(i).Range.Text

    6. I don't see that you ever SAVE the document after doing the Header/Footer section of code. You simply close, then quit?

    Cindy Meister, VSTO/Word MVP, my blog


    Wednesday, January 29, 2014 2:50 PM
    Moderator
  • 1. In what environment is your code running? Is this Word VBA, or something else?  This is Word VBA

    If Word VBA, why are you creating new instances of the Word.Application, then destroying them? This is resource intensive and, as far as I can see, completely unnecessary...  The intent of this file is to be a template that will be used by several individuals.  The checklist is a list of engineering notes that will be exported to a new document based on the notes that are selected.

    2. What do you expect ActiveDocument to be? Is it supposed to be the document that has triggered DocumentBeforeClose?  I'm not sure about this.  I had another individual help me with the code.

    3. Is there a reason you're uisng For...Next instead of For Each...Next to loop the content controls?  I'm not sure about this.  I had another individual help me with the code.

    4. Are all the content controls in the document checkboxes? At that start, you assume they are not, but later it appears you assume they are ( If i < ActiveDocument.ContentControls.Count Then )?  Yes are all checkboxes

    EDIT (hit Submit by mistake)

    5. Why do you do this: content = ActiveDocument.Range(startRange, endRange).Text
    Instead of this: content =  ActiveDocument.ContentControls(i).Range.Text  I'm not sure about this.  I had another individual help me with the code.

    6. I don't see that you ever SAVE the document after doing the Header/Footer section of code. You simply close, then quit?  the template file does not need to be saved.  The new file is the only one that needs to be saved and it is drw_notes.docx

    Wednesday, January 29, 2014 3:38 PM
  • Hi tridda

    Starting with the answer to your question:

    Point 6: I'm talking about newDoc, not ActiveDocument. You don't issue a newDoc.Save command before you Close and Quit. Take a close look...

    As to the other points, I recommend you look at them with the person who helped you before. Your code is not as efficient or stable as it could/should be.

    If your code is running in VBA then there is absolutely NO reason to use CreateObject to create a new instance of the Word application.

    It's dangerous to use ActiveDocument as this could unexpectedly change. This may the be reason the other person told you to use CreateObject, but it wouldn't be necessary. All that you'd need to do would be to have a Document object pointing at that document with the check boxes. Shouldn't be a problem, really...

    I recommend you consider the points I brought up since this is apparently a tool that will be used by many people, and not just some personal code for your own use. As it stands, it's not really robust.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 29, 2014 4:19 PM
    Moderator
  • Thank you for your comments and I will check into it.  However, the code is working great other than trying to figure out how to insert the Header/Footer into the new document that is created. 

    Wednesday, January 29, 2014 5:34 PM
  • Thank you for your comments and I will check into it.  However, the code is working great other than trying to figure out how to insert the Header/Footer into the new document that is created. 


    well, as I've said twice, now, do a newDoc.SAVE after the lines that insert the header/footer and I imagine it will be there...

    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by tridda Monday, February 3, 2014 6:18 PM
    Wednesday, January 29, 2014 6:12 PM
    Moderator