#1
|
|||
|
|||
VBA Script - Add Header - Save as PDF
Guys
Firstly thank you in advance for the help! Id like to make a small vba script on my desktop which - Opens up the Select File dialog box - Select a word document - Add a JPG in the header [my company's logo basically] - Safe as PDF in same folder as word Could anyone help! Thanks Chris |
#2
|
||||
|
||||
Do you mean a vbscript? Do you specifically want the file picker or would it be better if you could drag a Word document on top of the script to 'pass in the filename' variable?
Which header does the logo go in to? (Section 1 first page, main or even?) Is there other content in that header that has to remain? This will be easiest if your entire header or logo is saved in the Normal template as a building block.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
hey guessed
thanks for coming back on this. appreciate it. answers to your q's: - yes; apologies, i meant a vbscript - that would also work too. but i thought of it as a small standalone app that resides on the desktop. - header on first page only - no, header in all documents is empty. script should just dump the 'logo.jpg' in header (and print as pdf) - i just want the logo embedded in the PDF file only. In word, i want to be able to print the document onto A4 that already has our logo on it Many thanks Chris |
#4
|
||||
|
||||
The following should work. Open a document in Word and run the macro Installing Macros
Don't forget to change the path in the macro to the location where the logo is saved. The process assumes that the logo is in the centre of the first page header. Code:
Sub LogoAndPDF() 'Graham Mayor - https://www.gmayor.com - Last updated - 18 Nov 2019 Dim oHeader As Range Dim strDocName As String Dim intPos As Integer Const strLogo As String = "C:\Path\Logo.jpg" 'substitute path of logo Start: strDocName = ActiveDocument.FullName intPos = InStrRev(strDocName, ".") If intPos = 0 Then ActiveDocument.Save GoTo Start End If strDocName = Left(strDocName, intPos - 1) ActiveDocument.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True Set oHeader = ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range oHeader.End = oHeader.End - 1 oHeader.InlineShapes.AddPicture FileName:=strLogo oHeader.ParagraphFormat.Alignment = wdAlignParagraphCenter ActiveDocument.ExportAsFixedFormat OutputFilename:=strDocName, _ ExportFormat:=wdExportFormatPDF, _ OpenAfterExport:=False, _ OptimizeFor:=wdExportOptimizeForPrint, _ Range:=wdExportAllDocument, from:=1, to:=1, _ Item:=wdExportDocumentContent, _ IncludeDocProps:=True, _ KeepIRM:=True, _ CreateBookmarks:=wdExportCreateHeadingBookmarks, _ DocStructureTags:=True, _ BitmapMissingFonts:=True, _ UseISO19005_1:=False ActiveDocument.Close SaveChanges:=False lbl_Exit: Set oHeader = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
||||
|
||||
As a VBScript, the following should be saved into a text file and given a name that ends with ".vbs". Make sure you provide a valid path to the logo up the top.
Code:
Dim sLogo, objArgs, objFSO, objWord, I sLogo = "C:\Path\Logo.jpg" 'substitute path to your logo Set objArgs = WScript.Arguments Set objFSO = CreateObject("Scripting.FileSystemObject") If objArgs.Count = 0 then wScript.Echo "You need to drop Word documents onto this script!" Else Set objWord = CreateObject("Word.Application") objWord.Visible = True For I = 0 to objArgs.Count - 1 LogoAndPDF objArgs(I) Next objWord.Quit End If Function LogoAndPDF(sFilePath) Dim sFolder, sBaseName, sExt, sPDF, oDoc, oRng sFolder = objFSO.GetParentFolderName(sFilePath) & "\" sBaseName = objFSO.GetBaseName(sFilePath) sExt = objFSO.GetExtensionName(sFilePath) sPDF = sFolder & sBaseName & ".pdf" Select Case sExt Case "doc", "docx", "docm" Set oDoc = objWord.Documents.Open(sFilePath) oDoc.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True Set oRng = oDoc.Sections(1).Headers(2).Range oRng.Text = "" oRng.InlineShapes.AddPicture sLogo oRng.ParagraphFormat.Alignment = 1 'wdAlignParagraphCenter oDoc.ExportAsFixedFormat sPDF, 17 'wdExportFormatPDF oDoc.Close False End Select End Function
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#6
|
|||
|
|||
Andrew, Graham - you are amazing. Both scripts work beautifully.
Im speechless. This will save me hours of manual work. Thank you guys so much. I wish there was a way to repay you. (if there is, please let me know!) Thank you again Chris |
#7
|
|||
|
|||
Graham - Just made a small donation through your website. Thanks v much.
Andrew - Let me know how I can donate. Quote:
|
#8
|
|||
|
|||
Graham you seem to be based not very far from where I am About a 1 hour drive from Nicosia!
|
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to save a PDF with header that extends to edge of page? | LadyJemima | Word | 4 | 03-19-2017 02:58 PM |
Export Access report as pdf -- save as .rft -- and Word puts some text into the header | louiseword | Word | 1 | 11-21-2016 04:32 PM |
Format of Header changing after save and close | shen_27 | Word | 4 | 03-19-2013 11:41 AM |
How to save the current page in a new file with all the page settings (header, footer | Jamal NUMAN | Word | 6 | 03-15-2012 03:27 PM |
VBA Save as header page name | devcon | Excel Programming | 0 | 11-25-2011 12:02 AM |