PDA

View Full Version : Template-VBA code to initialise a specific template



josie
04-02-2012, 10:54 PM
Hi,

I have created 3 templates for 3 business units.

I would like to have just one template that prompted a user for thier business unit which would insert the correct image of the business unit.

The three units are "Accounting", "Business" and "Financial". I have an image which each of these titles and would like to insert the corresponding image as selected by the user.

Is there any way to do this? Thank you all so much in advance for any time you have to reply.

Jose

MacroShadow
04-03-2012, 12:21 AM
NOT tested

For an Inline Picture try something along these lines:
Sub InsertPictures()

Dim strResponse As String

strResponse = InputBox("Which Unit do you belong to?", "Type Your Unit")

If strResponse = "" Then
Exit Sub
End If

Select Case Unit
Case "Accounting"
ActiveDocument.InlineShapes.AddPicture _
FileName:="C:\Program Files\Microsoft Office\Office\Bitmaps\Styles\stone.bmp", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
Case "Business"
ActiveDocument.InlineShapes.AddPicture _
FileName:="C:\Program Files\Microsoft Office\Office\Bitmaps\Styles\stone.bmp", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
Case "Financial"
ActiveDocument.InlineShapes.AddPicture _
FileName:="C:\Program Files\Microsoft Office\Office\Bitmaps\Styles\stone.bmp", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range
Case Else
MsgBox ("Your unit is currently not supported" _
& vbCrLf & "Please contact the IT department."), vbCritical, "Unsuported Unit"
End Select

End Sub
For a Floating Picture change this line
ActiveDocument.InlineShapes.AddPicture _
FileName:="C:\Program Files\Microsoft Office\Office\Bitmaps\Styles\stone.bmp", _
LinkToFile:=False, SaveWithDocument:=True, Range:=Selection.Range with this one
ActiveDocument.Shapes.AddPicture FileName:=.Name, _
LinkToFile:=False, _
SaveWithDocument:=True, _
Left:=120, _
Top:=20, _
Width:=150, _
Height:=150, _
Anchor:=Selection.Range

If your trying to add a picture to the Header you would have to use this, modifying the above procedure
Sub Add_File_Header()

Set docActive = Word.ActiveDocument
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.InlineShape s.AddPicture "C:\My Documents\My Pictures\MYPicture.bmp"
docActive.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = "Header text"
objWord.ActiveDocument.ActiveWindow.View.SeekView = wdSeekMainDocument
With docActive.PageSetup
.DifferentFirstPageHeaderFooter = False 'Set this to false will put text on first page, else will not.
End With

End Sub

josie
04-03-2012, 12:42 AM
oh my gosh u are amazing I am just gob smacked!

Thank you so much!

I'm going to put this into action right now. Thank you again for your time and effort to reply! Jo

MacroShadow
04-03-2012, 12:53 AM
Glad to be of help.

p.s.
Don't forget to replace the file name and path with the real one.

fumei
04-03-2012, 01:08 AM
"insert the correct image of the business unit."

It would help, when you post, if you gave more detailed in formation. Insert...WHERE?

macroshadow: your code re: putting image in header is confusing.
objWord.ActiveDocument.ActiveWindow.View.SeekView = wdSeekMainDocument
objWord???
if you declare docActive as ActiveDocument, why are you using ActiveDocument?
Why are you even using SeekView????
DifferentFirstPageHeaderFooter = False
'Set this to false will put text on first page, else will not.
Really??? That is not accurate.

fumei
04-03-2012, 01:13 AM
BTW: you are good enough to perhaps start using numerical values for property values. Maybe not for posting, as text is helpful fo others. But maybe for your own use (if you are not doing so already).
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = "Header text" could be:ActiveDocument.Sections(1).Headers1(1).Range.Text = "Header text"
Range.Collapse 0is another common example. Just a thought. It makes the code lines shorter.