Discussion:
Search and Insert, Search and Replace, Modify Document Margins
(too old to reply)
instant000
2009-10-26 14:33:31 UTC
Permalink
Basically, I have inherited a macro, and I need to modify it.

How do I do the following in a Macro?

Search for specific text?
Example, if I want to locate the string "FINAL INVOICE"
Insert an image before the string?
How do I insert an image, say c:\logo.jpg ?


Is this correct:

' Find Final Invoice and Set font size for header
' Set myRange = ActiveDocument.Content
Selection.HomeKey Unit:=wdStory
Selection.MoveDown Unit:=wdLine, Count:=20
' Selection.MoveDown Unit:=wdLine, Count:=20
Selection.Find.Text = "FINAL INVOICE"
While Selection.Find.Execute = True

Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.InlineShapes.AddPicture FileName:= _
"M:\logo\logo9.jpg", LinkToFile:=False, SaveWithDocument:= _
True
Selection.MoveDown Unit:=wdLine, Count:=18, Extend:=wdExtend
Selection.Font.Size = 11
Selection.MoveDown Unit:=wdLine, Count:=20
Wend

' Find Commerical Invoice and Set font size for header
' Set myRange = ActiveDocument.Content
Selection.HomeKey Unit:=wdStory
Selection.MoveDown Unit:=wdLine, Count:=20
' Selection.MoveDown Unit:=wdLine, Count:=20
Selection.Find.Text = "COMMERCIAL INVOICE"
While Selection.Find.Execute = True

Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.InlineShapes.AddPicture FileName:= _
"M:\logo\logo9.jpg", LinkToFile:=False, SaveWithDocument:= _
True
Selection.MoveDown Unit:=wdLine, Count:=18, Extend:=wdExtend
Selection.Font.Size = 11
Selection.MoveDown Unit:=wdLine, Count:=20
Wend



How do I replace text I searched for?
Say, I want to replace the text XXuser01 with an image XXuser01.jpg?

Is this correct:

' Place Paul's signature on the commercial invoice
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"
Selection.Find.Text = "XXX USER 97"
While Selection.Find.Execute = True
Selection.TypeBackspace
Selection.InlineShapes.AddPicture FileName:= _
"M:\logo\user97.jpg", LinkToFile:=False, SaveWithDocument:= _
True
Wend

How do I modify document margins?
Originally, the document might fit on A4, but I only have 8.5 x 11 t
print it out on. Is there any way to increase the margins inside th
macro?

Is this it?
' Increase Margin's

With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = InchesToPoints(0.15)
.BottomMargin = InchesToPoints(0.1)
.LeftMargin = InchesToPoints(0.25)
.RightMargin = InchesToPoints(0.15)
.Gutter = InchesToPoints(0)
.HeaderDistance = InchesToPoints(0.5)
.FooterDistance = InchesToPoints(0.5)
.PageWidth = InchesToPoints(8.26)
.PageHeight = InchesToPoints(11.69)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = True
.MirrorMargins = False

.TwoPagesOnOne = False
.GutterPos = wdGutterPosLeft
End With

' Set Font Size

Selection.WholeStory
Selection.Font.Size = 1


--
instant000
Suzanne S. Barnhill
2009-10-26 21:34:30 UTC
Permalink
I don't know how to do this with a macro, but I know how to do it without
one. Copy your image to the Clipboard. Then search for "FINAL INVOICE" and
replace with ^c^&. ^c represents the Clipboard contents and ^& represents
the found text.
--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
http://word.mvps.org
Post by instant000
Basically, I have inherited a macro, and I need to modify it.
How do I do the following in a Macro?
Search for specific text?
Example, if I want to locate the string "FINAL INVOICE"
Insert an image before the string?
How do I insert an image, say c:\logo.jpg ?
' Find Final Invoice and Set font size for header
' Set myRange = ActiveDocument.Content
Selection.HomeKey Unit:=wdStory
Selection.MoveDown Unit:=wdLine, Count:=20
' Selection.MoveDown Unit:=wdLine, Count:=20
Selection.Find.Text = "FINAL INVOICE"
While Selection.Find.Execute = True
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.InlineShapes.AddPicture FileName:= _
"M:\logo\logo9.jpg", LinkToFile:=False, SaveWithDocument:= _
True
Selection.MoveDown Unit:=wdLine, Count:=18, Extend:=wdExtend
Selection.Font.Size = 11
Selection.MoveDown Unit:=wdLine, Count:=20
Wend
' Find Commerical Invoice and Set font size for header
' Set myRange = ActiveDocument.Content
Selection.HomeKey Unit:=wdStory
Selection.MoveDown Unit:=wdLine, Count:=20
' Selection.MoveDown Unit:=wdLine, Count:=20
Selection.Find.Text = "COMMERCIAL INVOICE"
While Selection.Find.Execute = True
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=1
Selection.InlineShapes.AddPicture FileName:= _
"M:\logo\logo9.jpg", LinkToFile:=False, SaveWithDocument:= _
True
Selection.MoveDown Unit:=wdLine, Count:=18, Extend:=wdExtend
Selection.Font.Size = 11
Selection.MoveDown Unit:=wdLine, Count:=20
Wend
How do I replace text I searched for?
Say, I want to replace the text XXuser01 with an image XXuser01.jpg?
' Place Paul's signature on the commercial invoice
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="1"
Selection.Find.Text = "XXX USER 97"
While Selection.Find.Execute = True
Selection.TypeBackspace
Selection.InlineShapes.AddPicture FileName:= _
"M:\logo\user97.jpg", LinkToFile:=False, SaveWithDocument:= _
True
Wend
How do I modify document margins?
Originally, the document might fit on A4, but I only have 8.5 x 11 to
print it out on. Is there any way to increase the margins inside the
macro?
Is this it?
' Increase Margin's
With ActiveDocument.PageSetup
LineNumbering.Active = False
Orientation = wdOrientPortrait
TopMargin = InchesToPoints(0.15)
BottomMargin = InchesToPoints(0.1)
LeftMargin = InchesToPoints(0.25)
RightMargin = InchesToPoints(0.15)
Gutter = InchesToPoints(0)
HeaderDistance = InchesToPoints(0.5)
FooterDistance = InchesToPoints(0.5)
PageWidth = InchesToPoints(8.26)
PageHeight = InchesToPoints(11.69)
FirstPageTray = wdPrinterDefaultBin
OtherPagesTray = wdPrinterDefaultBin
SectionStart = wdSectionNewPage
OddAndEvenPagesHeaderFooter = False
DifferentFirstPageHeaderFooter = False
VerticalAlignment = wdAlignVerticalTop
SuppressEndnotes = True
MirrorMargins = False
TwoPagesOnOne = False
GutterPos = wdGutterPosLeft
End With
' Set Font Size
Selection.WholeStory
Selection.Font.Size = 10
--
instant000
Loading...