Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-18-2019, 01:01 AM
Guessed's Avatar
Guessed Guessed is offline VBA Script - Add Header - Save as PDF Windows 10 VBA Script - Add Header - Save as PDF Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,176
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
To run the script in Windows File Explorer, drag one or more Word documents onto the file. It will create PDFs in the same folder as the source documents.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #2  
Old 11-18-2019, 02:46 PM
chrisx0x chrisx0x is offline VBA Script - Add Header - Save as PDF Windows 10 VBA Script - Add Header - Save as PDF Office 2019
Novice
VBA Script - Add Header - Save as PDF
 
Join Date: Nov 2019
Posts: 5
chrisx0x is on a distinguished road
Default

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
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Script - Add Header - Save as PDF How to save a PDF with header that extends to edge of page? LadyJemima Word 4 03-19-2017 02:58 PM
VBA Script - Add Header - Save as PDF 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
VBA Script - Add Header - Save as PDF 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

Other Forums: Access Forums

All times are GMT -7. The time now is 06:01 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft