Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

Error VBA Automation from Excel (4158)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alex St-Pierre - 10 Mar 2008 22:10 GMT
Hi !
I create a report in Word using Excel VBA. All Excel tables are copied into
Word and then formatted. The report is about 25 pages and countains 20
tables. If I execute the first part (about 10 tables) or the last part (about
10 tables), it works well. If I execute all the report, the report stop at
the following line (around table #16). Any idea?

Example #1 (executed from Excel):
   Dim oXlRng As Range
   Dim appWord As Word.Application
   Dim docWord As Word.Document
   Dim oRange As Word.Range
   Set appWord = New Word.Application
   appWord.Visible = True
   Set docWord = appWord.Documents.Add
   Set oXlRng = ThisWorkbook.Sheets(T(iTab).SheetName).Range("TableOutput")
   ....
   ThisWorkbook.Activate
   oXlRng.Copy
   docWord.Activate
   iP = docWord.Paragraphs.Count
   Set oRange = docWord.Paragraphs(iP).Range
   oRange.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False,
RTF:=False 'This returns an error 4198 and the table is not copied. ???
'If I try to re-execute the line, it still gives the same error..

Example #2 (An other example that happens only when I put the 20 tables
together is the following):
   iP = DocWord.Paragraphs.Count
   Set oRange = docWord.Paragraphs(iP).Range
   oRange.InsertAfter "MyText"
   oRange.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
   oRange.Borders(wdBorderTop).LineWidth = wdLineWidth050pt 'Gives error
5843 outside limit since there's no line..?
   oRange.Borders.DistanceFromTop = 4

Thanks!
Alex
Signature

Alex St-Pierre

Jean-Guy Marcil - 11 Mar 2008 14:12 GMT
> Hi !
> I create a report in Word using Excel VBA. All Excel tables are copied into
> Word and then formatted. The report is about 25 pages and countains 20
> tables. If I execute the first part (about 10 tables) or the last part (about
> 10 tables), it works well. If I execute all the report, the report stop at
> the following line (around table #16). Any idea?

I am a bit confused. You state that the code runs fine until the nth table.
But the code you post seems to indicate that you have two different errors
from the get go...
Or, the errors you pointed out only happen after a while... Why two errors?
Which one comes first... Is it random?

Also, since you are using objects and range, why do you keep using Activate?
As long as your objects are explicitly declared and used appropriately, you
do not need to keep switching between Word and Excel by Activating. Also,
unless you are using properties or methods that rely on the document being
visible, the code will run faster if the Word application stays invisible.

It might alos help to insert the following line at the end of each loop:
   docWord.UndoClear
to clear the undo stack, which has been linked to problems with macros
performing lots of editing operations on a document.

Finally, to make then code run marginally faster, use With blocks. As in:

   Set oRange = docWord.Paragraphs(iP).Range
   oRange.InsertAfter "MyText"
   oRange.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
   oRange.Borders(wdBorderTop).LineWidth = wdLineWidth050pt
   oRange.Borders.DistanceFromTop = 4

   Set oRange = docWord.Paragraphs(iP).Range
   With oRange
         .InsertAfter "MyText"
         With .Borders(wdBorderTop)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth050pt
         End With
         .Borders.DistanceFromTop = 4
  End With

> Example #1 (executed from Excel):
>     Dim oXlRng As Range
[quoted text clipped - 14 lines]
> RTF:=False 'This returns an error 4198 and the table is not copied. ???
> 'If I try to re-execute the line, it still gives the same error..

I tried the following code without any errors:

Sub Test()

Dim oRange As Range
Dim docWord As Document
Dim iP As Long

Set docWord = ActiveDocument
iP = docWord.Paragraphs.Count

Set oRange = docWord.Paragraphs(iP).Range
oRange.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False

End Sub

Since you snipped some of the code... It is hard to tell why you get the
error.
How do you loop through all the tables in the Excel Workbook?
Note that your code, as is, will replace the content of the last paragraph
by the table. And, since I do not code that will prevent this, all tables
will be joined (no space between them).

Try with my suggestions (no Activate, undo, invisible...)
But, of course, if you want to debug, you will need to make the app visible.

> Example #2 (An other example that happens only when I put the 20 tables
> together is the following):
[quoted text clipped - 5 lines]
> 5843 outside limit since there's no line..?
>     oRange.Borders.DistanceFromTop = 4

I ran this code as is without any problems.
I guess it depends on the content of the last paragraph in the document...
Alex St-Pierre - 11 Mar 2008 20:06 GMT
Thanks a lot for the answer! The solution is to put the line:
docWord.UndoClear before the macro that create each table.

I had the same error last year (happens randomly) with paragraphformat, ...
when our report contained only 10 tables. At that time, I realized that it
happens only when there were other word files open. So, I replaced:
Set appWord = Word.Application
If appWord Is Nothing Then Set appWord = New Word.Application
by Set appWord = New Word.Application
and the problem never came back until I add the second part of the report
(the appendix). So, now, I have a permanent solution! :)

When I use appWord.visible = False, the report takes 2 minutes instead of 5
to be created. This is a real gain. I tried it before and I had problems with
table border formatting. But now, it seems to work well (probably because I
refer to object only)

> unless you are using properties or methods that rely on the document being
> visible, the code will run faster if the Word application stays invisible.
I use a lot of properties for each tables, how to know if there's something
that does work when appWord is hidden?

I will probably hide the form and put a box that will show the creation %.
If there's a bug inside the macro when appWord is hidden, how do you work
with appWord. Probably better to unhide it.

I'll have removed the line: ThisWorkbook.Activate and the macro works well.

Thanks a lot!
Alex
Signature

Alex St-Pierre

> > Hi !
> > I create a report in Word using Excel VBA. All Excel tables are copied into
[quoted text clipped - 95 lines]
> I ran this code as is without any problems.
> I guess it depends on the content of the last paragraph in the document...
Jean-Guy Marcil - 12 Mar 2008 13:54 GMT
> Thanks a lot for the answer! The solution is to put the line:
> docWord.UndoClear before the macro that create each table.
[quoted text clipped - 17 lines]
> I use a lot of properties for each tables, how to know if there's something
> that does work when appWord is hidden?

If yo do not use the Selection object you should be OK with the app being
invisible.
Also, proper testing should tell...

> I will probably hide the form and put a box that will show the creation %.
> If there's a bug inside the macro when appWord is hidden, how do you work
> with appWord. Probably better to unhide it.

Use error trapping. When an error is detected, then, and only then, make the
app visible.

But, again, with thorough testing, errors should not occur all that often...
;-)
Julian - 13 Mar 2008 13:06 GMT
<snip>
> If yo do not use the Selection object you should be OK with the app being
> invisible.
> Also, proper testing should tell...

Slight difference in mileage here...

In my experience (Word 2002 on an XP PC, I think but possibly also under NT
on a rather large server workstation) keeping the app invisible *can* cause
"random" errors that do not occur if the application remains visible - which
is a shame and a pain in various bodily parts...

I would start with the app visible (turn off screenupdating if you want) -
then when it is working properly try it with the app invisible; if it still
works relaibly, good - if you get "random" errors with the invisible app
don't waste time trying to debug it... just bite the bullet and keep it
visible.

Problems with visibility specifically occurred for me when working with a
very large number of files (~100,000; don't ask!) to be processed
sequentially. Because of the overall complexity I had to pay careful
attention to memory usage (despite having as much memory as could be fitted
in the machine!) so as not to run out, so there might be an interaction
affecting stability between resource usage and application visibility - but
I was never able to nail it down so that's just an "informed" guess <g> But
the point stands - app invisibility *can* cause problems.

HTH
Signature

Julian I-Do-Stuff

Some Vista stuff, but mostly just Stuff at http://berossus,blogspot.com

Jean-Guy Marcil - 13 Mar 2008 13:34 GMT
> <snip>
> > If yo do not use the Selection object you should be OK with the app being
[quoted text clipped - 22 lines]
> I was never able to nail it down so that's just an "informed" guess <g> But
> the point stands - app invisibility *can* cause problems.

I have not written code for handling >100,000 documents...
In my more limited experience, the only problems I have had with Word being
invisible was when I wrote code using certain methods/properties associated
with the Selection object. Whenever I had those errors, if I could code
without the Selection Object (which I do now as a standard by always using
the Range object) then the error(s) went away.
Of course, there are some instances when you must use the Selection object
(when using the Information property for example), then, of course, keeping
the App invisble is not an option...

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.