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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding a logo image to word documents 1

Status
Not open for further replies.

peterpcu

MIS
Sep 15, 2005
31
0
0
GB

Hi,
Could anyone advise how I can use VBA (which would be included in an Excel VBA module) to add the same logo image to the front page of number of word documents that are stored in a folder
eg
[ul]
[li]select the folder where the word docs are stored[/li]
[li]open the word docs one at a time[/li]
[li]add the logo to the front page in a specific position[/li]
[li]save the word doc[/li]
[li]close the word doc[/li]
[/ul]

Many thanks in advance

Regards

Pete

 
For that you might use a macro like:
Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String
Dim wdDoc As Document, strDocNm As String
Const StrPic As String = "C:\Path\FileName.ext"
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, _
      AddToRecentFiles:=False, Visible:=False)
    With wdDoc
      .Shapes.AddPicture FileName:=StrPic, _
        Left:=InchesToPoints(1), _
        Top:=InchesToPoints(1), _
        Height:=InchesToPoints(3), _
        Width:=InchesToPoints(2)
      .Close SaveChanges:=True
    End With
  End If
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub

Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
replacing 'C:\Path\FileName.ext' with the full name & path of your image and using the various InchesToPoints settings to define the image's size & position in inches.

For PC macro installation & usage instructions, see:
The macro includes a folder browser that lets you select the document folder you want to process.

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Paul,

Excellent - Many thanks for your swift reply - I'll have a look at the content

Best Regards

peter

 
peterpcu,

Did the code work for your situation? Did you have any issues implementing it or using pieces of it?

macropod,

Any reason to use the Dir() function for looping through files rather than the FileSystemObject? Just curious, as I've used the FileSystemObject for practically every instance of looping through files.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Any reason to use the Dir() function for looping through files rather than the FileSystemObject?
Because it's simple and effective, with none of the FSO overhead.

Cheers
Paul Edstein
[MS MVP - Word]
 
OK, that's what I thought, and I definitely see the less overhead portion. FSO just gives more flexibility, at least where I've used the two. Either more flexibility or just plain easier to code.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Dir isn't reentrant, so it is less useful if you are writing a typical recursive directory walk ...
 
Gotcha - yeah, that's how I've used it vs FSO. If just a quick action or something, I'll use Dir(), but if I need to do something multiple times with something like "all files of type X in a folder" or especially if need search subfolders, I'll use FSO.

Thanks for the thoughts.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top