Avatar of RobMeans
RobMeansFlag 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.
        appWD.ActiveDocument.Close
    Next i
' Close the Word application.
appWD.Quit

End Function

Open in new window

Microsoft ApplicationsMicrosoft OfficeMicrosoft Excel

Avatar of undefined
Last Comment
RobMeans
Avatar of RobMeans
RobMeans
Flag of United States of America image

ASKER

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.
Avatar of RobMeans
RobMeans
Flag of United States of America image

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of RobMeans
RobMeans
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of RobMeans
RobMeans
Flag of United States of America image

ASKER

Where were you guys on this?
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo