Solved

Manipulate Word document with VBA

Posted on 2004-10-05
13
192 Views
Last Modified: 2008-03-04
I use the following code to export a report to a bookmarked location in a Word document. This works fine when running from an mbd but causes Access to crash when run from a runtime file (with the *send error report* message) after compling and formatting the Word document .

Has anyone any ideas?              

DoCmd.OutputTo acOutputReport, "rptFF", acFormatRTF, "C:\Program Files\JA\JOBSTemp\FFletter.doc"

Dim objWord As Word.Application
If objWord Is Nothing Then
   Set objWord = New Word.Application
   objWord.Visible = True
End If
     
With objWord
  .Documents.Add (mTemplatePath & "JOBSletter.dot")
  .Selection.Goto What:=wdGoToBookmark, Name:="DocumentInsert"
  .Selection.InsertFile FileName:="C:\Program Files\JA\JOBSTemp\FFletter.doc"
  Refresh
End With
Set objWord = Nothing
0
Comment
Question by:cmhcyc
13 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 12226832
check this link

Use Automation to Find a Location in a Word Document in Access
http://support.microsoft.com/?kbid=210032
0
 
LVL 19

Expert Comment

by:akoster
ID: 12227763
you should close the word object with

objWord.Close

before freeing the memory with the set nothing statement
0
 

Author Comment

by:cmhcyc
ID: 12228250
Hi Capricorn,

Thanks for the link. The code there is fairly similar to that which I am using and I also tried using the MS Word 9.0 Object Library and the MS Word 10.00 Object Library too.

All code runs fine when run as an mdb but still crashes out when run as a runtime module :((

akoster - thanks for your comment - I assume that you mean objWord.activedocument.close? Only I cannot enter objWord.Close....
0
 
LVL 10

Expert Comment

by:ALaRiva
ID: 12230449
Is there an error when it crashes?

Is it crashing on your computer or another computer?

HTH

-Anthony
0
 

Author Comment

by:cmhcyc
ID: 12231110
Hi Anthony,

No, the only message is the *send error report* and then Access closes to the desktop.

This happens on both the development machine and the one target machine that I have tested.
0
 

Author Comment

by:cmhcyc
ID: 12236133
Hi All,

I have narrowed the problem down a bit.

The code posted above creates a file FFLetter.doc which is then inserted into a new Word document.

This works fine when run as an mdb but crashes the runtime distribution.

BUT if I do not create FFLetter.doc but just leave a copy of the .doc in the appropriate folder, the routine runs fine!

I guess that it must be something to do with the need to close or otherwisethe FFLetter.doc before inserting it into the new Word document.

Any ideas?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 19

Expert Comment

by:akoster
ID: 12236815
Don't know for sure if this could be the problem,

have you checked that the export process generating the ffletter.doc file actually pauses the macro execution until it is finished ?

when this is not the case your problem comes to word trying to include a file which is open.
try adding a delay of 30 seconds and find out if the problem stays :

wait_time = timer + 30
while timer < wait_time
   doevents
wend
0
 

Author Comment

by:cmhcyc
ID: 12237299
Hi akoster,

Thanks - it just takes 30secs longer to crash :((
0
 

Author Comment

by:cmhcyc
ID: 12257250
Just noticed that *pop up* was ticked in properties of the report that is used for the OutputTo function. Turning this off allows the procedure to run fine in runtime - but I am at a loss to understand why it would run ok from the mdb!

Thanks for all your help out there!
0
 
LVL 19

Expert Comment

by:akoster
ID: 12257726
maybe the runtime doesn't support popups, strange...
0
 

Author Comment

by:cmhcyc
ID: 12257953
...infuriating not seeing the wood for the trees - must have spent a good 2 days on this!
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12898232
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now