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

Automate Word(Insert text in a specific place)

Status
Not open for further replies.

shavon

Programmer
Jun 18, 2001
102
CA
Good day:

I have a Word template that I would like to customize based certain criteria. There are several places where a name needs to be changed and also a logo needs to be inserted. Is there a way to put markers in the template in the place where the name and date need to be changed and then have VB Change those markers. Also, how do I point to a logo(bitmap file) and have it inserted in a specific place in my document? Thank you.
 
Use a textBox, via the "INSERT" menu. Then, whenever you want VBA to change the data in that text box you simply say

Textbox1.text = "your text"

Here is some code which you can try. I use it to print labels with different titles, product number etc. It will ask you at each step which text to enter into the various text boxes, then it will print a quantity of labels based on the "quantity" variable. You will have to customize it to your application...

LF
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 10/7/04 by LFordham

Dim Quantity As String
Dim StrtQuantity As Long
Dim title As String
Dim PO As String
Dim Part As String
Dim loop1 As Long
Dim dt As Date
Dim perbox As String
Dim PrtJbs As Long
Dim test As Long

title = InputBox("Enter the Title in the box below", "Please enter the Title...", "Title goes here")

ActiveDocument.Shapes("Text Box 6").Select
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 23
Selection.Font.Bold = True

Selection.TypeText Text:=title
PO = InputBox("Enter the PO Number in the box below", "I need the PO number...", "PO Number goes here")

ActiveDocument.Shapes("Text Box 5").Select
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 23
Selection.Font.Bold = True
Selection.TypeText Text:=PO
Part = InputBox("Enter the Part number in the box below", "Part Number Please...", "Part number goes here")

ActiveDocument.Shapes("Text Box 4").Select
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 23
Selection.Font.Bold = True
Selection.TypeText Text:=Part

perbox = InputBox("Enter the Quantity Per Box in the box below", "Quantity Per Box...", "Enter Quantity Per Box Here")
ActiveDocument.Shapes("Text Box 8").Select
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 23
Selection.Font.Bold = True
Selection.TypeText Text:=perbox

Quantity = InputBox("Enter the number of labels you wish to print in the box below.", "How many labels...", 1)

ActiveDocument.Shapes("Text Box 3").Select
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 23
Selection.Font.Bold = True
Selection.TypeText Text:=Quantity
dt = Date

ActiveDocument.Shapes("Text Box 7").Select
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 23
Selection.Font.Bold = True
Selection.TypeText Text:=CStr(dt)

StrtQuantity = Quantity
Application.ScreenRefresh

ActivePrinter = "\\packverxpwhse\sato cl 608"
If Options.PrintBackground = True Then
If Application.BackgroundPrintingStatus > 0 Then
DoEvents
PrtJbs = MsgBox("There are currently " & Application.BackgroundPrintingStatus & " Print jobs waiting, do you wish to continue?", vbYesNo, "There are print jobs waiting...")
If PrtJbs = vbNo Then
Exit Sub
ElseIf PrtJbs = vbYes Then
End If
Else
End If
Else
End If
DoEvents

ActiveDocument.Shapes("Text Box 2").Select
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 23
Selection.Font.Bold = True
Selection.TypeText Text:=StrtQuantity & " Of " & Quantity
Selection.TypeParagraph
Application.ScreenRefresh
Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
Collate:=True, Background:=True, PrintToFile:=False, PrintZoomColumn:=0, _
PrintZoomRow:=0, PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
test = MsgBox("Did the test label print correctly?", vbYesNo, "Test Label Printed...")
If test = vbNo Then
Exit Sub
ElseIf test = vbYes Then
End If

Do Until loop1 = Quantity
ActiveDocument.Shapes("Text Box 2").Select
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 23
Selection.Font.Bold = True
Selection.TypeText Text:=StrtQuantity & " Of " & Quantity
Selection.TypeParagraph
Application.ScreenRefresh

Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
Collate:=True, Background:=True, PrintToFile:=False, PrintZoomColumn:=0, _
PrintZoomRow:=0, PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0

StrtQuantity = StrtQuantity - 1
loop1 = loop1 + 1
Loop



End Sub



 
Sorry, I didn't address the BMP part of the question... The following code will insert a picture into a text box...

ActiveDocument.Shapes("Text Box 1").Select
Selection.InlineShapes.AddPicture FileName:= _
"C:\muffler.jpg", LinkToFile _
:=False, SaveWithDocument:=True

You can make the path to the picture a variable to make the code more flexable...

LF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top