Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-06-2018, 08:51 AM
lars-daniel lars-daniel is offline VBA is crashing after about 350 pages Windows 7 64bit VBA is crashing after about 350 pages Office 2010 32bit
Novice
VBA is crashing after about 350 pages
 
Join Date: Mar 2018
Posts: 6
lars-daniel is on a distinguished road
Default VBA is crashing after about 350 pages

Hi there,

I'm having a huge mailmerge document with 6,000 pages and many tables and columns. Each cell with "-1" inside it should get a grey background. Since I was not able to do this using IF function, I'm trying it with VBA (see below).

Description of the workflow: I'm running a very simple code to get through all the columns of all tables in my word document, make its background to "grey" and remove the content, if its numeric content is "-1".

On a 250 page document (mailmerge page 1-250), anything works fine. At 350 pages or more, the whole thing crashes at
Code:
c.Range.Text = ""
My Office 2010 is 32-bit, but it's far from memory limited. I also tried this code on Office 2016, but the code never completed... seems like VBA integration got worse there.

Anyone with an idea on how to make it better or fix it? "Conditionally Shade Table Cells" trick doesn't work here, since the margins of some cells can't be set to zero.



Code:
Sub grey()
  Application.ScreenUpdating = False
  Dim tbl As Table
  Dim c As Word.Cell
  For Each tbl In ActiveDocument.Tables
    For Each c In tbl.Range.Cells
      If Val(c.Range.Text) = -1 Then
        c.Shading.BackgroundPatternColor = wdColorGray10
        c.Range.Text = ""
      End If
    Next
    Set c = Nothing
  Next
  Application.ScreenUpdating = True
End Sub
Best,
Lars-Daniel
Reply With Quote
  #2  
Old 03-06-2018, 01:53 PM
macropod's Avatar
macropod macropod is offline VBA is crashing after about 350 pages Windows 7 64bit VBA is crashing after about 350 pages Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You might try giving Word some breathing space for its own housekeeping:
Code:
Sub Grey()
Application.ScreenUpdating = False
Dim Tbl As Table, Cll As Cell, i As Long
For Each Tbl In ActiveDocument.Tables
  For Each Cll In Tbl.Range.Cells
    i = i + 1: If i Mod 500 = 0 Then DoEvents
    With Cll
      If Split(.Range.Text, vbCr)(0) = "-1" Then
        .Shading.BackgroundPatternColor = wdColorGray10
        .Range.Text = ""
      End If
    End With
  Next
Next
Set Cll = Nothing: Set Tbl = Nothing
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 03-06-2018, 03:06 PM
lars-daniel lars-daniel is offline VBA is crashing after about 350 pages Windows 7 64bit VBA is crashing after about 350 pages Office 2010 32bit
Novice
VBA is crashing after about 350 pages
 
Join Date: Mar 2018
Posts: 6
lars-daniel is on a distinguished road
Default

Thanks a lot ! It's still running - can't tell you about the result. But since it's still running, you seem to have solved the problem. Latest (and biggest) Intel i7 with latest Office still uses 1 core only, three cores (+ hyperthreading) are idling. I should better split it into single files and run in parallel

Do you suggest to set use .Range.Text to remove the value or shall I do it the "replace"-way?
Code:
With ActiveDocument.Content.Find
    .Execute FindText:="-1", ReplaceWith:="", Format:=True, Replace:=wdReplaceAll, MatchWholeWord:=False
End With
Shall I also add this to each loop?
Code:
ActiveDocument.UndoClear
EDIT:
Oops, it doesn't work every time. I've got very much cells, which haven't get touched at all.
Reply With Quote
  #4  
Old 03-06-2018, 03:41 PM
macropod's Avatar
macropod macropod is offline VBA is crashing after about 350 pages Windows 7 64bit VBA is crashing after about 350 pages Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by lars-daniel View Post
Latest (and biggest) Intel i7 with latest Office still uses 1 core only, three cores (+ hyperthreading) are idling. I should better split it into single files and run in parallel
Splitting the document into multiple files won't help unless you also start multiple independent Word sessions.
Quote:
Originally Posted by lars-daniel View Post
Oops, it doesn't work every time. I've got very much cells, which haven't get touched at all.
In that case, there's more than just -1 in the cells (e.g. some spaces). You might try changing:
Split(.Range.Text, vbCr)(0)
to:
Trim(Split(.Range.Text, vbCr)(0))
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 03-06-2018, 03:48 PM
lars-daniel lars-daniel is offline VBA is crashing after about 350 pages Windows 7 64bit VBA is crashing after about 350 pages Office 2010 32bit
Novice
VBA is crashing after about 350 pages
 
Join Date: Mar 2018
Posts: 6
lars-daniel is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Splitting the document into multiple files won't help unless you also start multiple independent Word sessions.
Sure. I don't like the rise of multi-core processors in recent years. Only a fraction of the end-user applications support this, but fortunately Excel is one of them. For Server it's clear, there's something in it.

Quote:
Originally Posted by macropod View Post
In that case, there's more than just -1 in the cells (e.g. some spaces). You might try changing:
Split(.Range.Text, vbCr)(0)
to:
Trim(Split(.Range.Text, vbCr)(0))
Thanks, let me try. Maybe that's why I used Val() I think. What do you think about replace and removing my undo buffer?

Edit:
Didn't work. I'm still having many "-1". I've changed it back to "Val(.Range.Text) = -1". This works safe in my case.
Reply With Quote
  #6  
Old 03-06-2018, 03:55 PM
macropod's Avatar
macropod macropod is offline VBA is crashing after about 350 pages Windows 7 64bit VBA is crashing after about 350 pages Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by lars-daniel View Post
What do you think about replace and removing my undo buffer?
That would be of little or no benefit; the time taken to clear the buffer may actually slow down the process.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 03-06-2018, 04:02 PM
lars-daniel lars-daniel is offline VBA is crashing after about 350 pages Windows 7 64bit VBA is crashing after about 350 pages Office 2010 32bit
Novice
VBA is crashing after about 350 pages
 
Join Date: Mar 2018
Posts: 6
lars-daniel is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
That would be of little or no benefit; the time taken to clear the buffer may actually slow down the process.
Ah okay, same as unset($var) in PHP. Thanks a lot.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Numbering with heading is mixed up in odd pages in regard to even pages Baflla Word 0 09-11-2016 05:25 AM
Multiple Master Pages dont sync with content pages generatorjoe Publisher 0 07-28-2016 10:12 AM
Spreadsheet keeps crashing SavGDK Excel 8 06-28-2016 08:27 AM
VBA is crashing after about 350 pages Office 2013 is crashing and renaming files when crashing Brewski Office 1 09-21-2015 09:04 PM
VBA is crashing after about 350 pages Advanced page numbering: section pages in header, document pages in footer Albus Word 12 12-12-2014 01:36 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:54 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft