Macro works when step into but doesn't otherwise

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Macro works when step into but doesn't otherwise

Post by Robie »

Word 2003/1007

Hi

I have macro that I run to copy section 1 of a document & then replace the section 1 of the current document. The code works *perfectly well* if I step thru it but just crashes word otherwise. I can't see where it is going wrong since it works fine under debug. The reason I am doing this is that the cover page (section 1) has lots of graphics and is quite huge & therefore didn't want to store it as a Autotext in the template. Basically, it would make the template huge and it already is very large (1.4mb).

Any ideas, highly appreciated.

Code: Select all

Sub UpdateTheCoverPage()
'
' GetCoverPageDetails Function
' Get cover page details from a newly opened invisible document and then close that document
'
    Dim doc As Document
    Dim Rng As Range
    
    ' get a copy of section one (cover page) from template
    WordBasic.DisableAutoMacros 1
    Set doc = Documents.Add(Template:=sLCFidessaDocument, Visible:=False)
    WordBasic.DisableAutoMacros 0
    doc.Sections(1).Range.Copy
    doc.Close SaveChanges:=wdDoNotSaveChanges
    ' paste it into the current document - delete section 1 first
    Set Rng = ActiveDocument.Sections(1).Range
    Rng.Delete
    Rng.End = Rng.Start
    Rng.Paste
    Set Rng = Nothing
    ' clear the clipboard of the copied information
    ClearTheClipboard
End Sub

User avatar
HansV
Administrator
Posts: 78500
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro works when step into but doesn't otherwise

Post by HansV »

Does it work if you change the order of the actions a bit?

Code: Select all

Sub UpdateTheCoverPage()
'
' GetCoverPageDetails Function
' Get cover page details from a newly opened invisible document and then close that document
'
    Dim doc As Document
    Dim docA As Document
    Dim Rng As Range

    Set docA = ActiveDocument
    ' get a copy of section one (cover page) from template
    WordBasic.DisableAutoMacros 1
    Set doc = Documents.Add(Template:=sLCFidessaDocument, Visible:=False)
    WordBasic.DisableAutoMacros 0
    doc.Sections(1).Range.Copy
    ' paste it into the current document
    Set Rng = docA.Sections(1).Range
    Rng.Paste
    Set Rng = Nothing
    doc.Close SaveChanges:=wdDoNotSaveChanges
    ' clear the clipboard of the copied information
    ClearTheClipboard
End Sub
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Macro works when step into but doesn't otherwise

Post by Robie »

HansV wrote:Does it work if you change the order of the actions a bit?

Code: Select all

Sub UpdateTheCoverPage()
'
' GetCoverPageDetails Function
' Get cover page details from a newly opened invisible document and then close that document
'
    Dim doc As Document
    Dim docA As Document
    Dim Rng As Range

    Set docA = ActiveDocument
    ' get a copy of section one (cover page) from template
    WordBasic.DisableAutoMacros 1
    Set doc = Documents.Add(Template:=sLCFidessaDocument, Visible:=False)
    WordBasic.DisableAutoMacros 0
    doc.Sections(1).Range.Copy
    ' paste it into the current document
    Set Rng = docA.Sections(1).Range
    Rng.Paste
    Set Rng = Nothing
    doc.Close SaveChanges:=wdDoNotSaveChanges
    ' clear the clipboard of the copied information
    ClearTheClipboard
End Sub
Thanks Has. It didn't quite work as you had but I moved the close to the end of macro and also put a pause to slow it down a bit. Now, it is working.

BTW: I noticed that you did Set docA = ActiveDocument & then use docA in the macro. Is it a better practice not to use ActiveDocument or was there some other reason?

User avatar
HansV
Administrator
Posts: 78500
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro works when step into but doesn't otherwise

Post by HansV »

In this situation, it doesn't really matter because you create the new document invisibly, but in general I prefer to set a document variable. This ensures that you refer to the same document throughout the macro, even if another document becomes the active document by accident.
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Macro works when step into but doesn't otherwise

Post by Robie »

HansV wrote:In this situation, it doesn't really matter because you create the new document invisibly, but in general I prefer to set a document variable. This ensures that you refer to the same document throughout the macro, even if another document becomes the active document by accident.
That makes sense. Thanks for the tip Hans. Will use a variable in future.