#1
|
|||
|
|||
VBA to F+R text + logo in header, body, footer in multiple folders
Hello all,
Thank you for helping out. I am not having much luck in trying to get a code to work. I have tried a few with limited success and then I come across this site. So I will try to make sure I give you all the information I / we are trying to do. We have many policy documents, work safe, induction policies, safety policies swms etc that are in many different folders. This is literally how we have purchased these. Now we need to insert our company details amongst others into word documents plus a logo. Contents of the word docs are We have a header. In the header we would like to delete the existing dark placement box and insert our own logo. In the main body there are many situations where there is repetitive entries. i.e [Insert company name] , [Insert person in charge] and [Insert Company address] In the footer we have [Company name] [Company email] [Company phone number] [Company address] I am unsure if this can be done across multiple folders or One folder with the policy folders within them? If not I could put all the word docs into one folder. The reason i have put so much detail as I can is because reading some of the posts here it is mentioned to provide as much detail as possible. thank you for your assistance. |
#2
|
|||
|
|||
Quote:
(The following should be done by word macro for all documents in a folder) 1) Locate the currently inherent dark placement box in every document's header, remove it, and substitute it with your distinct logo (for instance, in .jpg format). 2) What alterations are expected for the central segment of the document? 3) What modifications are needed for the document's footer? It would be beneficial if you could upload a prototype document and a document showcasing the anticipated result for trial purposes. |
#3
|
|||
|
|||
Quote:
Thank you for taking the time out to respond. Attached is a classic document that has all the issues I mentioned in the OP. Alterations required to the 90 odd documents that are within about 60 folders. |
#4
|
|||
|
|||
Quote:
IMPORTANT: It's prudent to test on a few example documents before full execution. Pending your response to my question, this macro is currently set to only identify and replace the rectangle located in the header with a specific image. You will need to determine the size of the image yourself. Moreover, don't forget to modify the image path as required. Code:
Sub ReplaceShapeInMultipleDocuments() Dim folderPath As String Dim fileInFolder As String Dim doc As Document Dim dialogFolder As FileDialog ' Create a FileDialog object as a Folder Picker dialog box Set dialogFolder = Application.FileDialog(msoFileDialogFolderPicker) ' Allow the user to select multiple folders dialogFolder.AllowMultiSelect = True ' Show the dialog box If dialogFolder.Show = -1 Then ' Loop through each selected folder For i = 1 To dialogFolder.SelectedItems.Count ' Get the path of the folder folderPath = dialogFolder.SelectedItems(i) ' Get the first .docx file in the folder fileInFolder = Dir(folderPath & "\*.docx", vbNormal) ' Loop through each .docx file in the folder While fileInFolder <> "" ' Open the document Set doc = Documents.Open(folderPath & "\" & fileInFolder) ' Call the ReplaceShapeWithImage macro ReplaceShapeWithImage doc ' Close the document and save the changes doc.Close SaveChanges:=wdSaveChanges ' Get the next .docx file in the folder fileInFolder = Dir() Wend Next i End If End Sub Sub ReplaceShapeWithImage(doc As Document) Dim shp As Shape Dim imagePath As String Dim width As Single Dim height As Single ' Set the path to your image imagePath = "C:\Q.png" ' Prompt the user to input the size of the image width = 100 height = 20 ' Loop through each section in the document For Each sec In doc.Sections ' Loop through each header in each section For Each hdr In sec.Headers ' Loop through each shape in each header For Each shp In hdr.Shapes ' Check if the shape is the one we want to replace If shp.Name = "Rectangle 197" Then ' Delete the old shape shp.Delete ' Add the new image Set shp = hdr.Shapes.AddPicture(imagePath, False, True, 0, 0, width, height) ' Set the name of the new shape to the old shape's name shp.Name = "Rectangle 197" ' Center the image in the header shp.Left = (hdr.Range.Sections(1).PageSetup.PageWidth - shp.width) / 2 ' Exit the sub after replacing the shape Exit Sub End If Next shp Next hdr Next sec End Sub |
#5
|
|||
|
|||
Hello Syl,
Thank you. That worked a treat. Some minor issues where the Text moved up the page to then be overlapping the logo in some files. But I can get around that if I have to. I appreciate the help you have provided thus far. I am really trying hard to understand what it is you have done so I understand how it is being done. I am having no luck inserting find and replace text in the body of the doc and also in the footer. Appreciate any help. |
#6
|
|||
|
|||
Not all files have headers and footers.
So now I am thinking I need to start off with remove all headers and footers. Then Insert header on first page with LOGO and footer on last page with company details. i.e address Phone, email address etc. Sorry for being a pain. i am really trying hard to make sure I give all the correct information the first time. |
#7
|
|||
|
|||
Quote:
|
#8
|
|||
|
|||
Thank you SYL. Please give me some time on this. What I assumed was one format of files we recieved has now turned into two at least. I will try to provide more detailed information tomorrow and what I am hoping to achieve. Thank you SYL for your time
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
VBA Insert Image(logo) into header for multiple Word Docs | Axis | Word VBA | 4 | 02-09-2022 10:34 PM |
Adding text to the header and footer without removing all other content in the header and footer | digitalelise | Word VBA | 2 | 10-23-2019 02:58 AM |
How do I lock an editable text box header and stop it moving with the rest of the main body text? | thegaffa | Word | 6 | 09-28-2018 09:21 AM |
Locking Header/Footer on Document but still be able to edit body text (size, font, bold, etc) | Porkie96 | Word | 1 | 06-21-2018 01:56 PM |
Created VBA to Find and Replace in Body, Header and Footer with Highlighting the replacement text | QA_Compliance_Advisor | Word VBA | 11 | 09-23-2014 04:40 AM |