Word2007 VBA:change wdHeaderFooterPrimary fails for .docm, ok for

D

dpomt

I am using the VBA code below two insert different pictures on first page
header and primary header.
This codes works pretty fine in Word 2003 and also in Word 2007 as long as
document is either not saved or daved as .doc or .dot. As soon as I save the
document as .docm/.docm, the code works incorrect: the second picture is not
inserted on second page (primarypage) as intended but also on first page.


---
Dim oSec As Section
Dim r1 as Range
Dim r2 as Range
Dim hfFirstPage As HeaderFooter
Dim hfPrimaryPage As HeaderFooter

Set oSec = ActiveDocument.Sections(1)
Set hfFirstPage = oSec.Headers(wdHeaderFooterFirstPage)
Set hfPrimaryPage = oSec.Headers(wdHeaderFooterPrimary)

Set r1 = hfFirstPage.Shapes.AddPicture(FileName:=strImageWithPath1,
LinkToFile:=True, SaveWithDocument:=False)
Set r2 = hfPrimaryPage.Shapes.AddPicture(FileName:=strImageWithPath2,
LinkToFile:=True, SaveWithDocument:=False)
....
---

Right now, I am using the workaround to save my document as .doc. But this
will not let me store word 2007 specialities in the document.
Any suggestions what goes wrong in .docm/.dotm with my code?
I have found other posts describing exactly that behavior but no solution
was presented.

Any suggestion?
Thanks,
dpomt.
 
J

\Ji Zhou [MSFT]\

Hello dpomt,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

-----------Research in my side-----------
I have a quick test with your codes in my side and I can reproduce the
exact scenario as you described. The following is the detailed information
from my research,

In Word 2007,

1.When the Section.PageSetup.DifferentFirstPageHeaderFooter is set to
FALSE, also the default value, the HeaderFooter.Shapes.Add() will add
pictures into the first page, no matter what we specify in the Headers
index, the wdHeaderFooterFirstPage or wdHeaderFooterPrimary.

2.When the Section.PageSetup.DifferentFirstPageHeaderFooter is set to TRUE,
the HeaderFooter.Shapes.Add() will add pictures into the primary pages, no
matter what we specify for the Headers index.

That is to say, the wdHeaderFooterFirstPage and the wdHeaderFooterPrimary
always have the same effect.


----------Workaround--------------------
The workaround is using the HeaderFooter.Range.InlineShapes instead of the
HeaderFooter.Shapes. I have tried the following codes and they worked on my
side. Would you like to run it on your machine and let me know the result.
If you need any future help on this, please feel free to let me know. I
will be more than happy to provide future assistance.


------------VBA Codes-------------------
Dim oSec As Section
Dim r1 As InlineShape
Dim r2 As InlineShape
Dim hfFirstPage As HeaderFooter
Dim hfPrimaryPage As HeaderFooter

Set oSec = ActiveDocument.Sections(1)
oSec.PageSetup.DifferentFirstPageHeaderFooter = True
Set hfFirstPage = oSec.Headers(wdHeaderFooterFirstPage)
Set hfPrimaryPage = oSec.Headers(wdHeaderFooterPrimary)

Dim strImageWithPath1 As String
strImageWithPath1 = "C:\Users\v-jzho\Pictures\1.jpg"

Dim strImageWithPath2 As String
strImageWithPath2 = "C:\Users\v-jzho\Pictures\2.JPG"

Set r1 =
hfFirstPage.Range.InlineShapes.AddPicture(FileName:=strImageWithPath1,
LinkToFile:=True, SaveWithDocument:=False)
Set r2 =
hfPrimaryPage.Range.InlineShapes.AddPicture(FileName:=strImageWithPath2,
LinkToFile:=True, SaveWithDocument:=False)
----------------------------------------

Have a nice day!


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jay Freedman

Hi dpomt,

Further to Ji Zhou's workaround, since you want Shape objects instead of
InlineShape objects to be able to position them, make the following additions:

- Declare Shape objects

Dim s1 As Shape
Dim s2 As Shape

- After the InlineShapes are inserted, convert them to Shapes

Set s1 = r1.ConvertToShape
Set s2 = r2.ConvertToShape

- Proceed to format and position the Shapes as before.

The same code will work in both Word 2007 and 2003.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.

Hello dpomt,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

-----------Research in my side-----------
I have a quick test with your codes in my side and I can reproduce the
exact scenario as you described. The following is the detailed information
from my research,

In Word 2007,

1.When the Section.PageSetup.DifferentFirstPageHeaderFooter is set to
FALSE, also the default value, the HeaderFooter.Shapes.Add() will add
pictures into the first page, no matter what we specify in the Headers
index, the wdHeaderFooterFirstPage or wdHeaderFooterPrimary.

2.When the Section.PageSetup.DifferentFirstPageHeaderFooter is set to TRUE,
the HeaderFooter.Shapes.Add() will add pictures into the primary pages, no
matter what we specify for the Headers index.

That is to say, the wdHeaderFooterFirstPage and the wdHeaderFooterPrimary
always have the same effect.


----------Workaround--------------------
The workaround is using the HeaderFooter.Range.InlineShapes instead of the
HeaderFooter.Shapes. I have tried the following codes and they worked on my
side. Would you like to run it on your machine and let me know the result.
If you need any future help on this, please feel free to let me know. I
will be more than happy to provide future assistance.


------------VBA Codes-------------------
Dim oSec As Section
Dim r1 As InlineShape
Dim r2 As InlineShape
Dim hfFirstPage As HeaderFooter
Dim hfPrimaryPage As HeaderFooter

Set oSec = ActiveDocument.Sections(1)
oSec.PageSetup.DifferentFirstPageHeaderFooter = True
Set hfFirstPage = oSec.Headers(wdHeaderFooterFirstPage)
Set hfPrimaryPage = oSec.Headers(wdHeaderFooterPrimary)

Dim strImageWithPath1 As String
strImageWithPath1 = "C:\Users\v-jzho\Pictures\1.jpg"

Dim strImageWithPath2 As String
strImageWithPath2 = "C:\Users\v-jzho\Pictures\2.JPG"

Set r1 =
hfFirstPage.Range.InlineShapes.AddPicture(FileName:=strImageWithPath1,
LinkToFile:=True, SaveWithDocument:=False)
Set r2 =
hfPrimaryPage.Range.InlineShapes.AddPicture(FileName:=strImageWithPath2,
LinkToFile:=True, SaveWithDocument:=False)
----------------------------------------

Have a nice day!


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

dpomt

Hello Jay,

thanks for your follow. This was exactly what I needed. It works fine now.

Best regards
dpomt

Jay Freedman said:
Hi dpomt,

Further to Ji Zhou's workaround, since you want Shape objects instead of
InlineShape objects to be able to position them, make the following additions:

- Declare Shape objects

Dim s1 As Shape
Dim s2 As Shape

- After the InlineShapes are inserted, convert them to Shapes

Set s1 = r1.ConvertToShape
Set s2 = r2.ConvertToShape

- Proceed to format and position the Shapes as before.

The same code will work in both Word 2007 and 2003.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.

Hello dpomt,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

-----------Research in my side-----------
I have a quick test with your codes in my side and I can reproduce the
exact scenario as you described. The following is the detailed information
from my research,

In Word 2007,

1.When the Section.PageSetup.DifferentFirstPageHeaderFooter is set to
FALSE, also the default value, the HeaderFooter.Shapes.Add() will add
pictures into the first page, no matter what we specify in the Headers
index, the wdHeaderFooterFirstPage or wdHeaderFooterPrimary.

2.When the Section.PageSetup.DifferentFirstPageHeaderFooter is set to TRUE,
the HeaderFooter.Shapes.Add() will add pictures into the primary pages, no
matter what we specify for the Headers index.

That is to say, the wdHeaderFooterFirstPage and the wdHeaderFooterPrimary
always have the same effect.


----------Workaround--------------------
The workaround is using the HeaderFooter.Range.InlineShapes instead of the
HeaderFooter.Shapes. I have tried the following codes and they worked on my
side. Would you like to run it on your machine and let me know the result.
If you need any future help on this, please feel free to let me know. I
will be more than happy to provide future assistance.


------------VBA Codes-------------------
Dim oSec As Section
Dim r1 As InlineShape
Dim r2 As InlineShape
Dim hfFirstPage As HeaderFooter
Dim hfPrimaryPage As HeaderFooter

Set oSec = ActiveDocument.Sections(1)
oSec.PageSetup.DifferentFirstPageHeaderFooter = True
Set hfFirstPage = oSec.Headers(wdHeaderFooterFirstPage)
Set hfPrimaryPage = oSec.Headers(wdHeaderFooterPrimary)

Dim strImageWithPath1 As String
strImageWithPath1 = "C:\Users\v-jzho\Pictures\1.jpg"

Dim strImageWithPath2 As String
strImageWithPath2 = "C:\Users\v-jzho\Pictures\2.JPG"

Set r1 =
hfFirstPage.Range.InlineShapes.AddPicture(FileName:=strImageWithPath1,
LinkToFile:=True, SaveWithDocument:=False)
Set r2 =
hfPrimaryPage.Range.InlineShapes.AddPicture(FileName:=strImageWithPath2,
LinkToFile:=True, SaveWithDocument:=False)
----------------------------------------

Have a nice day!


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

dpomt

Hello Ji,

thanks a lot for your reply.
Your workaround along with Jays reply (to use ConvertToShape on the insered
inline shape) worked fine for be.

Just a last question:
is the desribed behavior a bug of by design?

Best regards
dpomt
""Ji Zhou [MSFT]"" said:
Hello dpomt,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

-----------Research in my side-----------
I have a quick test with your codes in my side and I can reproduce the
exact scenario as you described. The following is the detailed information
from my research,

In Word 2007,

1.When the Section.PageSetup.DifferentFirstPageHeaderFooter is set to
FALSE, also the default value, the HeaderFooter.Shapes.Add() will add
pictures into the first page, no matter what we specify in the Headers
index, the wdHeaderFooterFirstPage or wdHeaderFooterPrimary.

2.When the Section.PageSetup.DifferentFirstPageHeaderFooter is set to TRUE,
the HeaderFooter.Shapes.Add() will add pictures into the primary pages, no
matter what we specify for the Headers index.

That is to say, the wdHeaderFooterFirstPage and the wdHeaderFooterPrimary
always have the same effect.


----------Workaround--------------------
The workaround is using the HeaderFooter.Range.InlineShapes instead of the
HeaderFooter.Shapes. I have tried the following codes and they worked on my
side. Would you like to run it on your machine and let me know the result.
If you need any future help on this, please feel free to let me know. I
will be more than happy to provide future assistance.


------------VBA Codes-------------------
Dim oSec As Section
Dim r1 As InlineShape
Dim r2 As InlineShape
Dim hfFirstPage As HeaderFooter
Dim hfPrimaryPage As HeaderFooter

Set oSec = ActiveDocument.Sections(1)
oSec.PageSetup.DifferentFirstPageHeaderFooter = True
Set hfFirstPage = oSec.Headers(wdHeaderFooterFirstPage)
Set hfPrimaryPage = oSec.Headers(wdHeaderFooterPrimary)

Dim strImageWithPath1 As String
strImageWithPath1 = "C:\Users\v-jzho\Pictures\1.jpg"

Dim strImageWithPath2 As String
strImageWithPath2 = "C:\Users\v-jzho\Pictures\2.JPG"

Set r1 =
hfFirstPage.Range.InlineShapes.AddPicture(FileName:=strImageWithPath1,
LinkToFile:=True, SaveWithDocument:=False)
Set r2 =
hfPrimaryPage.Range.InlineShapes.AddPicture(FileName:=strImageWithPath2,
LinkToFile:=True, SaveWithDocument:=False)
----------------------------------------

Have a nice day!


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

\Ji Zhou [MSFT]\

Hello dpomt,

In my opinion, this is a by design behavior of Word 2007. From the
following Word 2007 document about the HeaderFooter.Shapes
http://msdn.microsoft.com/en-us/library/bb241914.aspx. We can see that,

"The Shapes property, when applied to a document, returns all the Shape
objects in the main story of the document, excluding the headers and
footers. When applied to a HeaderFooter object, the Shapes property returns
all the Shape objects found in all the headers and footers in the
document." That is to say, the Shapes collection of HeaderFooter will not
distinguish whether the target page is the first one or the primary one.

But the HeaderFooter.Range represents the portion of a document that is
contained within the specified header or footer.
(http://msdn.microsoft.com/en-us/library/bb241912.aspx). So the
HeaderFooter.Range.InlineShapes collection is specific to the particular
type pages.

If you have any future concerns or questions, please feel free to let me
know. Have a nice day!


Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top