Word Header Text

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,645
Office Version
  1. 365
Platform
  1. Windows
I am using a template Word document that is opened via Excel and then requires either text adding to a header or the text in the header being replaced.

One complication with this is that on the left side of the header, there are two shapes added (the suppliers logo and then the clients logo)
1724226496012.png


The following opens the Word file and then adds the client logo
VBA Code:
strPath = ActiveWorkbook.Path
strClientLogo = Range("FD_ClientLogoPath")

'Word session creation
Set appWord = CreateObject("Word.Application")

'word will be closed while running
appWord.Visible = True

'open the .doc file
Set docWord = appWord.Documents.Open(strPath & "\Pull Calculation Report Template.docx")

'Access the header range
Set rngHeader = docWord.Sections(2).Headers(1).Range

'Insert the image into the header
Set picWord = rngHeader.InlineShapes.AddPicture(strClientLogo)

'Resize the image
With picWord
   .LockAspectRatio = msoTrue
   
   .Height = 34 ' Set the height in points
   
   .ConvertToShape
   
   .Range.ShapeRange.Left = 109.6
   .Range.ShapeRange.Top = -17.55
End With

'change header text
Set picWord = Nothing
At this point I then need to change the text on the right of the header to replace PROJECT_TITLE and DOCUMENT_REF with the appropriate text.

I have tried iterating through the sections (and headers) and when using the Watch function, in one of the sections the .Range.Find.Text shows as
Project Title: PROJECT_TITLE Document No. : DOCUMENT_REF
I am assuming that you can assigned an area and/ore text with a label in Word and the 1st line has a label of 'Project Title' and the 2nd line a lable of 'Document No.' which is why I am seeing this when I watch the object variable.

Essentially however it is done, I need the two shapes to be on the left and the required text to show on the right.


TIA
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Since you are using a template, why don't you use fields in Word (eg document properties or bookmarks) and then just update the values of those? Then it won't matter where they are in the document - all the design and layout is done in Word and set in the template.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top