Avatar of RobMeans
Flag for United States of America asked on

My VBA code loses Headers when it opens Word Docs

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

Open in new window

Microsoft ApplicationsMicrosoft OfficeMicrosoft Excel

Avatar of undefined
Last Comment

8/22/2022 - Mon

Just a quick follow up: When Word opens, right before I call the named document, it has a blank ribbon. The ribbon area stays blank when the doc is open.  I'll be looking into whether Word is loading correctly

Updated.  Word now has ribbon items when opened by the above, but the end result is the same.

Since the project has to go forward I have tried opening the docs via IE.App.  

using "IE.Navigate strAddy", forces a dialog box with a choice of: Open or Save As or Cancel
that cannot be ignored.  I have tried SendKeys, but that just messes up my code in debug mode.  I am currently slogging through this by manually clicking SAVE, pressing ^v, pressing ENTER, then F5 to continue. I can meet the initial deadline this way...but the next stage calls for 368 files to be processed.  I would like to avoid all the manual interaction.

            ToTheClipboard strTargFile ' Load the file name for the downloaded document to the clipboard.
            ' Create InternetExplorer Object
            Set IE = CreateObject("InternetExplorer.Application")
            '.... This opens a dialog box with a choice of: Open or Save As or Cancel
            IE.Navigate strAddy
                '... when the dialog box opens do these steps manually:
                '1 click save as
                '2 paste the file name that was loaded to clipboard
                '3 press enter, the file will save
                '4 Press F5 to get to the next document and repeat 1-4...only 244 documents to go :-(
            ' Clean up internet explorer
            Set IE = Nothing

Open in new window


Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Where were you guys on this?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck