How to delete an immage in the header of a word document opened from a word templated using Excel VBA

FrisoKoopmanAarding

New Member
Joined
Feb 29, 2016
Messages
4
Hi MrExcel,

With a macro in Excel I open up a word document from a word template. In this document I have a header which has two named logo's (named through Home->Editing (subsection on the right)-> Select->Selection Pane.... I would like to delete one of these logo's rather than generating two templates for each, as one template is easier to maintain. Also I might need to add more logo's in the future.
The code works fine in Word VBA, but not in Excel. After extensive searching on the web I figures out i might have something to do with late or early binding / object libraries?

Code in Word:
Code:
Selection.HeaderFooter.Shapes("Logo1").Select
Selection.ShapeRange.Delete

Code in Excel:
Code:
Sub Data2Word()
Dim WDApp As Object 
Dim WDDoc As Object 
Dim myWordFile          As String
myWordFile = Sheets("1").Range("Quote_template_path").TextSet WDApp = CreateObject("Word.Application")
    Set WDDoc = WDApp.Documents.Add(Template:=myWordFile)
    WDApp.Visible = True
    WDDoc.ActiveWindow.WindowState = wdWindowStateMaximize

If Sheets("Summary").Range("Letterhead").Value = 1 Then
        WDApp.Activate
            
            WDApp.Selection.HeaderFooter.Shapes("Logo1").Select
            Selection.ShapeRange.Delete


If anyone spots the error or know's a good workaround which still provides a single word file to maintain.

Regards Friso
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi and welcome to the Board

See if this helps:

Code:
' Excel module
Sub Data2Word_b()
' add a reference at Tools>References>Microsoft Word xx.0 Object Library
Dim wdapp As Object, wdoc As Object
Set wdapp = CreateObject("Word.Application")
Set wdoc = wdapp.Documents.Add(Template:="c:\pub\template1.dotx")
wdapp.Visible = True
wdoc.ActiveWindow.WindowState = wdWindowStateMaximize
wdoc.ActiveWindow.View.SeekView = wdSeekCurrentPageHeader
wdapp.Selection.HeaderFooter.Shapes(1).Delete
End Sub
 
Upvote 0
Hi Worf,

Thank you so much. That worked wonderfully! I missed the line to actually open up the headerfooter editor.
Code:
 wdoc.ActiveWindow.View.SeekView = wdSeekCurrentPageHeader

Note sure if I can close this thread myeslf or if that's even required, but you definately solved my problem. You guys are fast!

Thanks again
Friso
 
Upvote 0
You are welcome.

We don´t have a way to formally mark a thread as solved here. The OP will have to declare it solved, as you did.
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,348
Members
452,638
Latest member
Oluwabukunmi

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