Worked in 2003, not in 2007

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
 
D

Doug Robbins - Word MVP

Has the Att Number2 style been defined in your 2007 template? Maybe it is
that line of code that is causing the problem.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
F

Fumei2 via OfficeKB.com

I refuse to use 2007 so I can not check this, but is it possible that it is
crashing on this

Application.Browser.Next

rather than the previous line of Selection.Copy????
 
A

April

I'm not sure if that line of code is the problem. The error message says:
"This method or property is not available because no text is selected." And
then it highlights the Selection.Copy line.

I'm just not sure why it's not selecting anything now when it worked fine in
the 2003 document. I've got another template with slightly different
formatting, and the code works fine in that one. The only difference between
them is that the text it's trying to select is in bold and mixed case in the
other document while it's uppercase and not bold in this one.
 
D

Doug Robbins - Word MVP

The Selection.Copy command fails if only a numeral is entered into the input
box. If the number of the attachment AND a title for it are entered into
that box, the failure does not occur.

After commenting our the Selection.Style commands and running the code and
entering

1 This is a test

into the Input box, a page is added to the document with the following on
it:

Page 1 of 2
ATTACHMENT 1 this is a test this is a test (Continued)

It seems like your code may be missing the commands to insert a page break,
though probably your code should be entering a Next Page Section Break and
all of this should be being entered into the first page/primary header of
the new Section.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
A

April

I solved it. Something you said set something off in my head and the light
went on. I had some dummy text in the template to mark where the Attachments
were. One of the pieces of dummy text was "Attachment 2(a field), Second
Attachment". Since the macro searched for the word "Attachment" it found the
word "Attachment" in the phrase "Second Attachment" and then couldn't the
"Attachment 2" part.

I have seriously been kicking myself for missing that.

Thanks for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top