stil need help with my macro. now i'm optimizing it. i have done it the way it should be, but it is too long. i came from 884 lines down to 444 lines of code. before, it send out the error, the microsoft alert: Send/Don't Send report. dont know why.(this happend when i ran it at: open the template. i inserted AutoNew module and run 4 macros in it. it copies the documents, sets table of contents, sets styles and reapplies styles)
so i optimized it a bit.
here is where it crashes:
For Each styyle In docStyyle.Styles
If styyle.InUse = True Then
With docStyyle.Content.Find
.ClearFormatting
.Style = styyle ' ----!!!!!!------ ERROR
If .Found = True Then
With docStyyle.Content.Find
.Replacement.ClearFormatting
.Replacement.Style = styyle
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
End If
End With
Selection.Find.Execute Replace:=wdReplaceAll
End If
Next styyle
ERROR: Item with specified name does not exist
i have tried a similar one from microsoft visual bacis help just for example and it crashes also at that point.
here is what my code looked like before

884 lines)
' course acronym slug - the style
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Course Acronym Slug")
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles("Course Acronym Slug")
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
and so on for over 10 of them so i had to optimize it.
-------------------------------------------------------
** the other problem:
as said the macro starts at opening the template. it copies the files, sets table of contents, sets styles and reapplies styles.(or it should)
i run 4 macros:
1.) copy files
2.) table of contents
3.) set styles
4.) apply styles (the first problem)
example for 3.) (this one is also too long. also for over 10 of them - styles) this too needs optimizing.(the other problem)
'---------------------------------------------------
' course acronym slug
With ActiveDocument.Content.Find.Style = "Course Acronym Slug"
If ActiveDocument.Content.Find.Style = True Then
With ActiveDocument.Styles("Course Acronym Slug").Font
.Name = "Arial"
.Size = 16
.Bold = True
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = wdColorAutomatic
.Engrave = False
.Superscript = False
.Subscript = False
.Scaling = 100
.Kerning = 0
.Animation = wdAnimationNone
End With
With ActiveDocument.Styles("Course Acronym Slug").ParagraphFormat
.LeftIndent = CentimetersToPoints(0)
.RightIndent = CentimetersToPoints(12.65)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceExactly
.LineSpacing = 19
.Alignment = wdAlignParagraphRight
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = True
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = CentimetersToPoints(0)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
With .Borders
.DistanceFromTop = 1
.DistanceFromLeft = 4
.DistanceFromBottom = 4
.DistanceFromRight = 4
.Shadow = False
End With
End With
ActiveDocument.Styles("Course Acronym Slug"). _
NoSpaceBetweenParagraphsOfSameStyle = False
ActiveDocument.Styles("Course Acronym Slug").ParagraphFormat.TabStops. _
ClearAll
ActiveDocument.Styles("Course Acronym Slug").ParagraphFormat.TabStops.Add _
Position:=CentimetersToPoints(4), Alignment:=wdAlignTabRight, Leader:= _
wdTabLeaderSpaces
End If
End With
it can be a bit optimized. not using False statements, but it is stil long.
help if can, thx.