Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lock picture in Word 1

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,567
US
How can I lock the picture - a company logo - in Word document?

I need the logo at certain position at the top of the first page only. So I created a simple document where I inserted a picture at the top of the page, positioned it and set some properties of the picture. I need later on add some text to the logo (Address, telephone. Fax, etc. ) that’s why I have the logo as pictures and I want to type text over it.

Now, when I type text ‘by hand’ all works fine, then I can Insert – Break…. – Page Break, and I am on the second page, logo stays on top of the first page. That’s the way I want.

But if I record the Macro with exactly the same steps, as soon as I insert a page break thru the macro, logo from the top of the first page jumps to the top of the second page. How can I keep the logo where I want it – on the top of first page?

I will be pulling information from database and populate entire Word document thru VB (using VBA) so the user does not have to type anything.

I did my research on the Web, but no luck. And none of the stuff from Format Picture: Layout, Lock Anchor, Move picture with text, and so on, nothing makes any difference.

Word 2003, SP2
Word 2002, SP3
Windows 2000
Windows XP

---- Andy
 
Simple:
Code:
Selection.TypeText Text:="My Company address"
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="Some text on the first page"
Selection.TypeParagraph
Selection.InsertBreak Type:=wdPageBreak
[b]'this line moves picture ^^^[/b]
Selection.TypeText Text:="Second page"

--- Andy
 
OK, I was under the (I guess false impression) that the image was put in by code. Could you describe precisely what you do to the image? I ran your code with an image on the first page, and it worked fine - although I did do it using a With statement, and vbCrLf instead of TypeParagraph.
Code:
With Selection
  .TypeText Text:="My Company address" & vbCrlf & vbcrLf & _
      vbCrLf & "Some text on the first page" & vbCrLf
  .InsertBreak Type:=wdPageBreak
  'this line moves picture ^^^
  .TypeText Text:="Second page"
End With

Gerry
My paintings and sculpture
 

Open Word, blank document shows up.

Insert - Picture - From File...
Choose any file, I have a logo in TIF format, but that does not matter.

Double clik on the picture, that brings Picture Format window.
Layout tab - choose 'Behind Text' icon.

Advance... command button, on Picture Position tab I unchecked "Move object with text" option, I don't want it to move - but I don't know if this actually makes any difference.
I tried the other 2 checkboxes ('Allow overlap' and 'Layout in table cell') but no difference, either.

Then position (just move) logo on the top of the page in the document.

I run the macro (even yours) and logo jumps on the second page after PageBreak

---- Andy
 
OK, this is ticking me off. I walked through the exact steps you describe above. I run the code. It does NOT make the image jump to the next page. I can not duplicate this. However, I am darn well curious as to why yours does.

Is there anything secret in your normal.dot? That is, I am assuming this new blank document IS created from normal.dot. If it is not, and it is created from a different template, then you need to say so. If it is from normal.dot, if you are willing:

Send me a copy of your normal.dot, and a sample of a file in which this is happening. Only if this is OK with you. You can send them to - myhandle at telus dot net

I'd like to see if I can possibly duplicate this. Other than that, I have no suggestions. It works fine for me. Anyone else??

Gerry
My paintings and sculpture
 
Hi Andy. Thanks for sending the file. Hopefully by now you have the "fixed" file back.

As stated in my email back to you, I am posting the details here in the thread so everyone can see what the situation is/was.

OK. This is a both a simple thing, and an odd thing peculiar with Word.

1. Your graphic was locked to the last paragraph. So quite naturally when that last paragraph was moved to the next page (after your .InsertBreak) the graphic came along with it....as it should. Simple, I think. Unlock it and move the anchor to the FIRST paragraph. So I did, and removed the text.

I ran the code...and it moved the graphic again! Hmmmmm. I tried it again. Nope...it moved the graphic. Well it stumped me for a couple of minutes. There was only ONE paragraph, the graphic is NOT locked to it, it is set for do not move with text....what could it be? Then I tried...

2. Ctrl-End. This moves the Selection to the very very last character of the document. It did not look any different - after all there was only one paragraph. I ran the code...and it DID work. I used undo to back up. I manually put the Selection at the last (and only) paragraph). I ran the code...it did NOT work, the graphic moved to the next page.

I used undo to back up. Hmmmm. There is the graphic, and ONE paragraph mark. The selection is blinking just before the pilcrow (paragraph mark). I ran the code...it did NOT work, the graphic moved to the next page.

I used undo to back up. There is the graphic, and ONE paragraph. I did Ctrl-End. There was absolutely no visible change - the cursor (Selection) seems to be at the same position, at the last (ONLY) paragraph. I ran the code...it DID work.

And here is why. The paragraph mark is an oddball. Technically speaking, it is, AFAIK, the only "character" in Word that is actually TWO - repeat, TWO - ASCII characters. It is Chr(13) AND Chr(7). This is very important, especially when taking string (text) out of a cell in a table. Here is a simple test to demonstrate.

Make a table - say two rows, three columns, but it does not really matter. Have a couple of blank paragraphs after it.

Put some text in Cell(1,2) - the second cell, row 1. Run the following:
Code:
Sub CopyCell2()
Dim strIn As String
strIn = ActiveDocument.Tables(1).Cell(1, 2).Range.Text
Selection.EndKey unit:=wdStory
Selection.TypeText Text:=strIn
End Sub
It will put the text of Cell(2) at the end of the document. That text will be the text of the cell AND - a paragraph mark. In other words, there will now be a paragraph mark following the text - even though you inserted the text at the end of the document.

Ah ha! you say. Fine, I will chop of the last character of the range, like this:
Code:
Sub CopyCell2()
Dim strIn As String
strIn = ActiveDocument.Tables(1).Cell(1, 2).Range.Text
strIn = Left(strIn, Len(strIn) - 1)
Selection.EndKey unit:=wdStory
Selection.TypeText Text:=strIn
End Sub
Run the code. Nope. There is STILL an extra paragraph mark. Hmmmmmmmm. But if you chop off TWO characters, like this:
Code:
Sub CopyCell2()
Dim strIn As String
strIn = ActiveDocument.Tables(1).Cell(1, 2).Range.Text
strIn = Left(strIn, Len(strIn) - [b][COLOR=red]2[/color red][/b])
Selection.EndKey unit:=wdStory
Selection.TypeText Text:=strIn
End Sub
It works. The text - and ONLY the text - from Cell(2) is placed at the end of the document (and of course you could be putting it anywhere).

And this is what was happening with your document Andy. Even manually placing the Selection (cursor) at the paragraph mark would not work because doing so does NOT place the Selection after BOTH - I guess sort of internal - ASCII characters of the paragraph mark. Even using the right arrow key will not make it work.

But Ctrl-End DOES.

So...to fix your problem I first unlocked the graphic, dragged the anchor to the first paragraph mark, removed your sample text, pressed Ctl-End - to explicitly move the Selection to the very last character - and your code runs perfectly.

BTW: just in case you don't know...you need to have Show/Hide ON to see the anchor. You need to see the anchor. In your original document you can see it has a wee lock beside it. First of all, unlock it - double click the graphic then Layout > Advanced and uncheck Lock Anchor. Remove your sample text. Press Ctrl-End. NOW run your code. It works.

BTW: again, you do not need to use all those separate Selection.TypeParagraph instructions. You can use vbCrLf as part of your string.

Gerry
My paintings and sculpture
 
Hi Andy. Well, well, well. I just learned something myself.

For everyone else...Andy just emailed me back saying the "fixed" document did not work. The document opens with - again, ONE paragraph. The graphic is already there. The code (adding some text, making a page break, adding more text) does NOT work.

This is because Word, when it opens a document places the Selection at the very START. Because the problem here is the Selection needs to be at the very END...the code does not work. Now, it looks like the Selection is at the end - there is, after all, only the ONE paragraph. However...just like if you manually place the Selection at the end...it is NOT really the end.

So I added at the start of the code, an explicit instruction - Selection.EndKey Unit:=wdStory - to go to the END.

It now works.....I hope.

Andy?

Gerry
My paintings and sculpture
 
Thanks Gerry,

So, all I had to do, in simple terms, was to add to the begining of my Macro:
Code:
Selection.EndKey Unit:=wdStory
which is a Word code for Ctrl-End and all my probblems are gone.

Thank you for the (long) explanation, it helped me a lot.

And yes, I do use With statement - makes my code a lot easier to read. As far as vbNewLine or vbCrlf - VBA has some problems with them, but I can live with it.

Now, I can continue to program.

--- Andy

PS. Just FYI, Gerry - now, they (users) don't want to have a logo in a letter at all. It is good I am a very easy going person and will not go postal on them. But it is tempting....
 
So, all I had to do, in simple terms, was to add to the begining of my Macro:

CODE
Selection.EndKey Unit:=wdStory
which is a Word code for Ctrl-End and all my probblems are gone.
Not quite. There was still the issue that the anchor was locked to the last paragraph. You were adding text prior to the last paragraph - as one always does. You have to be careful about the difference between the START and the END. It my look like only one paragraph, but in terms of location there are TWO points.

As for no longer wanting the logo...shrug...what can I say? There are worse things in life. That kind of stuff happens all the time. It is a bit annoying after one has worked on something for days (or weeks, or months, or even years) though....

I worked on a project for two years - more than $20 million pumped into it - and then it was scrapped. Nothing left. Nada. Zip. Such is the tech world.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top