Page 1 of 3 123 LastLast
Results 1 to 15 of 37
  1. #1
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    Our templates contain a company logo. The company changes logo now and then (unfortunately) and would like to be able to insert a new logo (picture/.gif, .tif file) by e. g. using a command placed in a user form. the file representing the logo should be placed in the ...Workgroup-templates folder on the C-drive.

    I think it should be done by placing a "field code" representing the place on the document where I want the logo to be placed. So how can I make this possible in VBA?

    Thanks in advance.

    regards
    Bjorn

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    What you could do is place the logo in the template by Insert | Picture | From File..., browse to the logo file, then click the dropdown arrow next to the Insert button and click Insert and Link. Next, select Edit | Links... and lock the link, so that it won't be updated. Since the logo has been inserted, it will be available even on computers on which the logo file is not available.

    When you need to update the logo, replace the original logo with a file of the same name. Use code to unlock the link, update it and lock it again. For example:

    With ActiveDocument.Sections(1).Headers(wdHeaderFooterF irstPage).Range.Fields(1)
    .Locked = False
    .Update
    .Locked = True
    End With

    You will have to adapt this to your situation.

  3. #3
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    Thanks HansV

    I will try this asap.

    It is amazing how often companies change their logo now adays.

    Bjorn

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    Bjorn,

    Just to clarify: do you need to update the logos in the templates only (Hans' method should do fine), or do you need to give the user the option to update an existing logo in some existing documents? - the latter scenario would take more doing.

    Gary

  5. #5
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    Thanks Gary,

    Yes I do; Our documents has a logo inserted to day. I plan to make a new template such that the logo will be inserted (automatically) when I create a new document. Then the logo is placed in the document and can be viewed on any computer.

    If the company logo is changed at a later time the user should be able to insert (click a button) the new logo and thus replace the old one at the same place.
    I would very much appreciate a code to do so!!

    Bjorn

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    Bjorn,

    I think you can use the method I proposed for this: if you insert the logo with Insert and Link, the image will be stored in the document itself, so that it can be viewed and printed on any computer, but it still "knows" where the original file was, so that it can be updated if needed.

    You could put the code snippet I posted to get the up-to-date logo into a macro in a general module in the template.

    You can call this macro in the Document_New macro in the ThisDocument module of the template, so that a new document always gets the current logo.
    You could also make the macro available through a custom toolbar button, keyboard shortcut and/or custom menu item (all stored in the template), so that the user can update an existing document if needed.

    If you need to move the logo file to another folder, this won't work. Take a look at macropod's Star Post <!post=Update paths in INCLUDETEXT et al. (Word 2000/XP),261488>Update paths in INCLUDETEXT et al. (Word 2000/XP)<!/post> for a way to update the path of a linked image.

  7. #7
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    Thanks HansV,

    I will try this out as soon as I get some time free. Though I'm not sure how the insert macro knows where to put the new logo? Or does it know through the code?

    bjorn

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    Bjorn,

    My idea was to place the logo in the template where you want it (probably in a header or footer) "by hand". After that, it would only be updated by the macro I proposed, not deleted and inserted anew. You would have to modify ActiveDocument.Sections(1).Headers(wdHeaderFooterF irstPage).Range.Fields(1) to make the macro update the correct field - in this example, it is the first field in the first-page header in the first section; change this according to your situation.

  9. #9
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    Thanks HansV,
    Correct I have my logo in the Header.

    As I understand it, it is the term Fields(1) that "knows" where the picture/object is located and which picture/object it is and thus can replace it.
    I also understand that logos in other sections are controlled through the term Sections(1).

    What if I have another logo/picture in the text?

    How can I know which number the logo/picture has got, if there are several pictures?

    Thanks for quick answers!!!

    Bjorn

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    Bjorn,

    If you have several fields in the header, you can loop through them and test if they are of type msoIncludePicture (the type of field inserted when you link a picture):

    Dim i As Long
    Dim rng As Range
    Set rng = ActiveDocument.Sections(1).Headers(wdHeaderFooterF irstPage).Range
    For i = 1 To rng.Fields.Count
    If rng.Fields(i).Type = wdFieldIncludePicture Then
    ...
    End If
    Next i

    You'd have to repeat this for other headers and other sections if necessary; you can loop through them if you like.

  11. #11
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    Thanks Hans,

    I shall try this.

    Here it is the end of the day and I will be on work tomorrow again.

    Thanks a lot for very good help!!!

    Regards
    Bjorn

  12. #12
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    Hi,

    I have tried this, but the update function does not work. I get the link unlocked and locked again when running through the code but nothing happens with the .UPDATE

    My code is as follows:

    Private Sub UpdateLogo()
    With ActiveDocument.Sections(1).Headers(wdHeaderFooterF irstPage).Range.Fields(1)
    .Locked = False
    .Update
    .Locked = True
    End With
    End Sub

    What am I missing?

    bjorn

  13. #13
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    Hello Bjorn,

    The code that I posted earlier was just an example. As it is, it looks at a field in the first page header. Does the first section in your document have a "different first page" header? Check this in File | Page Setup..., Layout tab. If you just have a single header, use wdHeaderFooterPrimary instead of wdHeaderFooterFirstPage.
    Also, did you insert the logo with Insert and Link?

    If I change the picture file and then run the macro in my test document, the logo in the header does get updated, so the code itself is OK...

  14. #14
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    Hi,
    I have "different first page" header. Have tried with both "wdHeaderFooterFirstPage" and "wdHeaderFooterPrimery". Have also tried after I made a section continuous break.

    I inserted with Insert/Picture/From File and then used "Insert and Link". Then I did as you said; opened Edit/Links... , Locked it and "OK".
    I replaced the picture file with a different picture with the same name.

    I can watch that the link for the picture is being unlocked and locked again, but the picture is not updated.

    I'll try more this night and see what I can come up with.

    Thanks Hans, you're a patient person!

    Bjorn

  15. #15
    3 Star Lounger
    Join Date
    Jul 2001
    Location
    ASKER, Norway
    Posts
    277
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Re: How to insert a logo using VBA (Office XP, Word 2002 SP-2)

    Hi Hans,

    I have tried more and now it looks as it will work. It is a matter of choosing "FistPage" or "Primary" or rather the correct property.

    I had to use FirstPage for the first page section 1 which is defined as "different first page".

    Anyway, hope I have the principles ready for a real trial tomorrow on the documents that need to be supplied with a new logo.

    Thanks a lot for your help!

    Regards
    Bjorn

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •