troubleshooting Question

My VBA code loses Headers when it opens Word Docs

Avatar of RobMeans
RobMeansFlag for United States of America asked on
Microsoft OfficeMicrosoft ExcelMicrosoft Applications
4 Comments1 Solution403 ViewsLast Modified:
Hi All, The code below runs and is supposed to open documents and save them to their temporary home on the file system.  Their current location is as blobs on SQL. The addresses are a bit archane. Here is a sample: "http://.../.../DocReader.asp?DocumentID=4&Select=1"

If I were to individually open the documents with word, the headers and footers are visible and save.  

BTW: The header/footer starts on the second page.

Sadly, the code below loses the headers/footers.  How can I get it to preserve the headres/footers?
Public Function funOpenDoc()
Dim tmpArray As Variant
Dim LinksList As Variant
Dim appWD As Word.Application
Dim LastRow As Long, i As Long
Dim strAddy As String, strFilename As String, strTargFile As String
Dim strCode As String, strDocNo As String, strDocVer As String

 ' Find the last row with data in the spreadsheet
With Sheets("Sheet1")
    LastRow = .Range("E" & .Rows.Count).End(xlUp).Row
End With

' Create a new instance of Word and make it visible
Set appWD = CreateObject("Word.Application")
        appWD.Visible = True
             For i = 1 To LastRow
                 	strCode = Range("B" & i).Value 'Departmental code, 3 char
                 	strDocNo = Range("C" & i).Value 'Departmental document ID, 3 char with leading zeros
                 	strDocVer = Range("D" & i).Value 'Document version number, 3 char with leading zeros
                 	strFilename = strCode & strDocNo & strDocVer
                 	strAddy = Range("F" & i).Value 'the document hyperlink address
                 	strTargFile = "C:\DocFolder\" & strFilename & ".doc"
        ' Tell Word to create a new document, then put in print view so headers & footers can show
        appWD.Documents.Open (strAddy)
        appWD.ActiveDocument.ActiveWindow.View.Type = wdPrintView

        ' Save the new document with a sequential file name.
        appWD.ActiveDocument.SaveAs Filename:=strTargFile
        ' Close the new Word document.
    Next i
' Close the Word application.

End Function

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros