A
April
I have a macro that was in my template which started life as a Word 2003
document. I've created a new template in Word 2007 and transferred all the
code into the new document. Everything seems to work except one macro. This
is supposed to grab the title of a given attachment, copy it into a new page
of the attachment, and include the title. It always crashes on the
Selection.Copy line. The only thing I changed was making the word
"Attachment" all caps. But even if I change that back and use nothing but the
original code, it crashes on that same line.
Any ideas?
Sub AttachAdd()
' Adds page for given attachment
Application.ScreenUpdating = True
Dim Attach$, Message1, Title1
Dim Flag, Response$, Message2, Title2, AttachTL$, InputBox2
' ask for attachment #
Message1 = "Enter the Attachment Number."
Title1 = "Attachment Information"
Attach$ = InputBox(Message1, Title1)
If Attach$ = "" Then
Response$ = MsgBox("An attachment number must be provided.", vbOK +
vbCancel, "Attachment Information")
If Response = vbOK Then
Attach$ = InputBox(Message1, Title1)
End If
End If
Selection.InsertBreak Type:=wdPageBreak
ActiveWindow.View.Type = wdPageView
'This adds Page number references
Selection.Style = ActiveDocument.Styles("Att Sheet Number")
Selection.TypeText Text:="Page "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"Seq AttachmentPgNo \n ", PreserveFormatting:=True
Selection.TypeText Text:=" of "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"SECTIONPAGES \* Arabic ", PreserveFormatting:=True
Selection.TypeParagraph
'This adds attachment number entered
Selection.Style = ActiveDocument.Styles("Att Number2")
Selection.TypeText Text:="ATTACHMENT "
Selection.TypeText Text:=(Attach$)
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Attachment"
.Replacement.Text = ""
.Forward = False
.Wrap = False
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Application.Browser.Previous
Selection.MoveRight Unit:=wdWord, Count:=2
Selection.Extend
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Copy
Application.Browser.Next
Selection.EndKey Unit:=wdLine
Selection.Paste
Selection.TypeParagraph
Selection.Style = ActiveDocument.Styles("normal")
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" (Continued)"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(Continued) (Continued)"
.Replacement.Text = "(Continued)"
.Forward = False
.Wrap = False
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
Application.ScreenUpdating = True
Bye:
End Sub
document. I've created a new template in Word 2007 and transferred all the
code into the new document. Everything seems to work except one macro. This
is supposed to grab the title of a given attachment, copy it into a new page
of the attachment, and include the title. It always crashes on the
Selection.Copy line. The only thing I changed was making the word
"Attachment" all caps. But even if I change that back and use nothing but the
original code, it crashes on that same line.
Any ideas?
Sub AttachAdd()
' Adds page for given attachment
Application.ScreenUpdating = True
Dim Attach$, Message1, Title1
Dim Flag, Response$, Message2, Title2, AttachTL$, InputBox2
' ask for attachment #
Message1 = "Enter the Attachment Number."
Title1 = "Attachment Information"
Attach$ = InputBox(Message1, Title1)
If Attach$ = "" Then
Response$ = MsgBox("An attachment number must be provided.", vbOK +
vbCancel, "Attachment Information")
If Response = vbOK Then
Attach$ = InputBox(Message1, Title1)
End If
End If
Selection.InsertBreak Type:=wdPageBreak
ActiveWindow.View.Type = wdPageView
'This adds Page number references
Selection.Style = ActiveDocument.Styles("Att Sheet Number")
Selection.TypeText Text:="Page "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"Seq AttachmentPgNo \n ", PreserveFormatting:=True
Selection.TypeText Text:=" of "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"SECTIONPAGES \* Arabic ", PreserveFormatting:=True
Selection.TypeParagraph
'This adds attachment number entered
Selection.Style = ActiveDocument.Styles("Att Number2")
Selection.TypeText Text:="ATTACHMENT "
Selection.TypeText Text:=(Attach$)
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Attachment"
.Replacement.Text = ""
.Forward = False
.Wrap = False
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Application.Browser.Previous
Selection.MoveRight Unit:=wdWord, Count:=2
Selection.Extend
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.Copy
Application.Browser.Next
Selection.EndKey Unit:=wdLine
Selection.Paste
Selection.TypeParagraph
Selection.Style = ActiveDocument.Styles("normal")
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=" (Continued)"
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(Continued) (Continued)"
.Replacement.Text = "(Continued)"
.Forward = False
.Wrap = False
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
Application.ScreenUpdating = True
Bye:
End Sub