# Insert an Image into Word document in a specified location?? Word/VBA



## dragon-goddess1990 (Sep 17, 2012)

Word 2010

Scenario 1) I would like to be able to get the user to browse for an image (electronic signature) - I dont know how to create a browse control to do this

Scenario 2) I would like to be able to insert a header image into a document in a specific location - I dont know how to reference a specific location / coordinates in the document (bookmark isnt ideal as the header image needs to sit perfect and fill the width etc)

Please help 

Thanks


----------



## Macropod (Sep 18, 2012)

Scenario 1) If you want to be able to browse to a particular location and select the image to insert, what do you want to achieve that the Insert|Picture dialogue doesn't provide?

Scenario 2) Is there a reason for not using a template with the image already located & sized to meet your requirements? Aside from that, is it always the same image and location in the document?


----------



## dragon-goddess1990 (Sep 20, 2012)

1) They will be loading a userform before the document so they need to browze to an image from there and it insert it into a bookmark. It will affectively be their electronic signature.

2) Background of scenario - the user is filling in a userform before they have access to touch the document - they need to choose if it will be either an electronic document or paper. Electronic will insert the header, paper will adjust the margins/print settings. I want to be able to specify where the image goes (i.e. cm from top or whatever)


----------



## Macropod (Sep 23, 2012)

For (1) you could use code like:

```
Sub Demo()
Dim StrPic As String, iShp As InlineShape, Rng As Range
With Application.Dialogs(wdDialogInsertPicture)
  If .Show = -1 Then
    StrPic = .Name
    With ActiveDocument
      .Undo
      Set Rng = .Bookmarks("BkMk").Range
      Rng.Text = vbNullString
      Set iShp = .InlineShapes.AddPicture(FileName:=StrPic, LinkToFile:=False, Range:=Rng)
      With iShp
        .LockAspectRatio = True
        .Height = InchesToPoints(1)
      End With
      With Rng
        .End = .End + 1
      End With
      .Bookmarks.Add "BkMk", Rng
    End With
  End If
End With
End Sub
```
As for inserting a header in a specific location, if you mean somewhere other than on the first page, that can entail a fair bit of work; otherwise, it's best to have a default document (eg the print version) and, if they choose the alternate version, simply create a new document from the alternate version's template and use that. That way, you shouldn't need to worry about where the image goes.


----------



## dragon-goddess1990 (Sep 24, 2012)

1) What controls do I need to achieve this? (i.e. textbox, button etc)

2) Ive debated it - wanted to check if I could. Though it would overlap with another template I am creating which I need them to browze for a 3rd Party/External logo and it be inserted into the document in a specific location - can I create a text frame/object and tell it to go in there?


----------



## Macropod (Sep 24, 2012)

1) Do you already have the userform? If so, add a command button and drive the process from that.

2) Whenever you create a document, it really should be done from a template that's been designed for the purpose, rather than taking different template then trying to modify the document it creates. So, while you could have, say, a table into which you put a logo, it would be better to use a template with the logo and no table.


----------



## dragon-goddess1990 (Sep 24, 2012)

1)Yes I have a userform. So it would be buttonname_click

2) im trying to create 1 template from many one of the key objectives of the project is to reduce duplication (including templates) I guess if I just do the browse idea (1) and add a 3rd party logo bookmark that would do it. My main bug bare was the trying to create the text box for the URL and browse button. 

Taa


----------



## Macropod (Sep 25, 2012)

Re: 2
Yes, you could use the same basic approach as for (1) and, if it's for a letterhead, it's really not particularly complicated to put the logo into the page header (without even using a bookmark) and, say, configure the document with a different first page layout, so the logo only appears on the first page. Of course, if you already know which client you're doing the letterhead for, and the logos are all stored in the same folder, you can do away with the browse code and simply have the code look in the folder for the appropriate client logo. However, things do get complicated if the logos need resizing to fit the space available or they have to be put in different positions for different clients.


----------



## dragon-goddess1990 (Sep 25, 2012)

Hiya

For any headers I do I'd probably start the document off with it and just remove it if they choose paper. 

Unfortunately I don't know what logos they would put in or
where from this is why I needed the browse option 

Thing is our company is large and complicated. We are currently rolling out SharePoint 2010. This is a generic letter template so anyone needs to be able to use it. I've had to for example have the userform go find heir outlook signatures, list them so they can insert it into the signature so that they dont have to keep putting their details in every time or save a copy (defo don't want duplicates :P)

I might have a stab at the bbrowse thing see if I ccan get it to work. When thu browse using the button how will they then know they have chosen one correctly when they ccontinue with the userform? I assume the code you kindly submitted doesn't put the file path in a text box?

Regards


----------



## Macropod (Sep 25, 2012)

OK, given that you're trying to produce something for multiple branches, I'd be inclined to approach the letterhead issue slightly differently.

Assuming each branch has its own group folders at least, if you store the logo file for each branch in the same folder as the template, and the logo files all have the same filename, an INCLUDEPICTURE field can be used to import the logo. Then all you need the macro to do is to unlink the field upon the creation of the new document. If you wanted, the same principle can be extended to the importation of the entire letterhead, via an INCLUDETEXT field.

As for the browser code, you can change it's file display configuration to show file icons (if it's not already doing so), the same as you can do for Windows Explorer. FWIW, I wrote the code to prevent duplicates. Run it a second time and the first image will be replaced.


----------



## dragon-goddess1990 (Sep 17, 2012)

Word 2010

Scenario 1) I would like to be able to get the user to browse for an image (electronic signature) - I dont know how to create a browse control to do this

Scenario 2) I would like to be able to insert a header image into a document in a specific location - I dont know how to reference a specific location / coordinates in the document (bookmark isnt ideal as the header image needs to sit perfect and fill the width etc)

Please help 

Thanks


----------



## dragon-goddess1990 (Sep 28, 2012)

What Class/Library does .Name belong to that you use in the code please?


----------



## Macropod (Sep 28, 2012)

In this case, it belongs to Application.Dialogs(wdDialogInsertPicture). See: Built-in Dialog Box Argument Lists (Word)


----------



## dragon-goddess1990 (Sep 28, 2012)

Thanks for the reply but I found a way 

I split the code. First part is browse button to retrieve file, textbox1.value = strpic.

Secpnd button tells it to put strpic into bookmark


----------



## kaypat (Mar 21, 2013)

You may have already found a solution but here is a bit of code that will do what you need. The only thing is that you will need to add a path to your image where i have highlighted.

Sub Signature()
Application.ScreenUpdating = False
Dim Rng As Range, Shp As Shape, StrImg As String
StrImg = "C:Add path here"
Set Rng = Selection.Range
Rng.Collapse
Set Shp = ActiveDocument.InlineShapes.AddPicture(FileName:=StrImg, _
  SaveWithDocument:=True, Range:=Rng).ConvertToShape
With Shp
  .WrapFormat.Type = wdWrapBehind
End With
Set Rng = Nothing: Set Shp = Nothing
Application.ScreenUpdating = True
End Sub

Let me know how it works for you.



dragon-goddess1990 said:


> Word 2010
> 
> Scenario 1) I would like to be able to get the user to browse for an image (electronic signature) - I dont know how to create a browse control to do this
> 
> ...


----------

