Insert Graphics and other objects in a Word Document

C

cefrancke

I've been reading the help and I'm still confused.
I'm trying to get a grip on the Word Document Hierarchy, but to no
avail.

Section, Range, HeaderFooter ???????

The specific issue is, I want to insert a graphic at a specific point
in an existing Word document. In my case, the graphic will go in the
header of the first page, where from there it should of course show up
on the remaining page's header area.

I'm using a VBA in an Access module, opening the document....
from there I would like to insert the graphic (from a file) then
position and resize, etc.
....save the document and move on to the next.

I would like to start with a document where there is a pre-existing
graphic that says "Insert Graphic Here". This "blank" graphic will be
in the exact same place where the new graphic will be inserted.

So...I would like to open the doc, find the "blank" graphic, delete it,
insert the new graphic, position and resize (if necessary), save, and
move on to the next doc.

The "blank" graphic is not really necessary because the following code
works (though I'm sure, it cant be proper method), without a "blank".
However, there are some docs where there is a graphic in the header
already (btw there are sometimes 2-3 total, even distributed, ie
non-overlapping), so I would need a method to "find" the existing
graphic and delete it.

Here's the code I'm using now

'The app and doc are open already, at this point...

Dim objShp As Word.Shape
Dim objRng As Word.Range
Set objRng = objDoc.StoryRanges.Item(wdPrimaryHeaderStory) '
I'm guessing here...
Set objShp = objDoc.Shapes.AddPicture(filename:="C:\My.gif",
LinkToFile:=False, Anchor:=objRng)
With objShp
.Height = InchesToPoints(1)
.Width = InchesToPoints(1)
.Left = InchesToPoints(0.25)
.Top = InchesToPoints(0.25)

'Do I need to use relative positioning?

End With

I'm expecting a way to insert an object at any "coordinate" onthe
document in general or within a specific "section" (a word I use to
describe, Header, Footer, or Page Body)

I'm just not sure how to specify the Header, Footer, Body, or just the
document in general.

The general idea is to start with a base set of documents with a
"blank" graphic in the header.
For any reason (a new project, client, etc), create a new set of
documents from the originals where the appropriate graphic is inserted
in the same place where the "blank" graphic was, thus creating a set of
docs specific to a client or project or whatever.

The point is, I feel that my current method is a lucky guess, or a
"hack" and I can't seem to glean any proper methods from the confusing,
help.

Any ideas or references would be appreciated.

TIA
 
G

Greg Maxey

cefrancke,

I can't said that I am anything other than a novice in this area, however
this may get you on track:

Sub Test()
Dim objShp As Word.Shape
Dim objRng As Word.Range
Dim objDoc As Document
Set objDoc = ActiveDocument
'Define the range
Set objRng = objDoc.Sections(1).Headers(wdHeaderFooterPrimary).Range
'Check for other pictures
On Error GoTo Handler:
For Each objShp In objRng.ShapeRange
If objShp.Type = msoPicture Then
MsgBox "Process " & objShp.Name
End If
Next
ReEntry:
Set objShp = objDoc.Shapes.AddPicture(FileName:="C:\My.gif",
LinkToFile:=False, Anchor:=objRng)
With objShp
.Height = InchesToPoints(1)
.Width = InchesToPoints(1)
.Left = InchesToPoints(0.25)
.Top = InchesToPoints(0.25)
End With
Exit Sub
Handler:
Resume ReEntry
End Sub

I really don't understand why the "Check for other pictures" piece will
generate an error on:

For Each objShp In objRng.ShapeRange

if the the range doesn't contain at least one item but it does. Hopefully
someone will be along to explain it better.
 
G

Greg Maxey

Ok,

I found a way around the error. Still not perfectly clear what is going on
here:
Sub TryAgain()
Dim oHeader As Word.HeaderFooter
Dim oShape As Word.Shape
Dim oDoc As Document
Set oDoc = ActiveDocument
Application.ScreenUpdating = False
Set oHeader = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
For Each oShape In oHeader.Shapes
If oShape.Type = msoPicture Then
MsgBox "Process " & oShape.Name
End If
Next
Set oShape = oDoc.Shapes.AddPicture(FileName:="C:\My.gif", _
LinkToFile:=False, Anchor:=oHeader.Range)
With oShape
.Height = InchesToPoints(1)
.Width = InchesToPoints(1)
.Left = InchesToPoints(0.25)
.Top = InchesToPoints(0.25)
End With
End Sub
 
C

cefrancke

Thanks Greg,

That gave me more to think about.

I'm getting better control, I'm just not sure about a few things.
With your code I can insert an object into the Header, but what about
the Footer?

Is there an explicit way to reference the Header or Footer?

And, what is this Section thing all about?
What's it's purpose, how does a normal user create one.....?????

It's just hard to figure out from the help all the Who, What, When,
Where....

Thanks for the advice, so far....

cefrancke
 
G

Greg

cefrancke,

Well the TryAgain example specifically referenced the section 1 header.
To reference the footer you would need something like:

Dim oFooter as Word.Headerfooter
Set oFooter = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)


The section is just one of the eggs in the Word Chinese egg set.
Characters are in words, words in paragraphs, etc.

Every new document starts out with one section. The each section can
have up to three distinct headers or footer (firstpage, primary, even
pages).

You would use a new section for example when you want to start a new
chapter in a document. You could insert the new section using the UI
with Insert>Break>Section Break Types>Next page.

By default, Headers and Footers are linked to the the previous section
header or footer. So in our example above, if we inserted a new
section, the graphic would by default appear in the section 2 header
identical the section 1 header.

Let's say you want to add a new section to your document and put the
graphic in the foot of the new secion only. You could use something
like:

Sub TryAgainII()
Dim oFooter As Word.HeaderFooter
Dim oShape As Word.Shape
Dim oRng As Word.Range
Dim oDoc As Document
Set oRng = ActiveDocument.Range
Set oDoc = ActiveDocument

Application.ScreenUpdating = False
With oRng
'Go to the end of the document
.Collapse wdCollapseEnd
.InsertBreak wdSectionBreakNextPage
'Unlink the new (last section since it is at the end of the document)
section
'from the previous one
oDoc.Sections.Last.Footers(wdHeaderFooterPrimary).LinkToPrevious =
False
'Set the reference to the footer
Set oFooter = oDoc.Sections.Last.Footers(wdHeaderFooterPrimary)
End With
Set oShape = oDoc.Shapes.AddPicture(FileName:="C:\My.gif", _
LinkToFile:=False, Anchor:=oFooter.Range)
With oShape
.Height = InchesToPoints(1)
.Width = InchesToPoints(1)
.Left = InchesToPoints(0.25)
.Top = InchesToPoints(0.25)
End With
End Sub
 

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