[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

Manipulate Word document with VBA

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
cmhcyc
Asked:
cmhcyc
1 Solution
 
Rey Obrero (Capricorn1)Commented:
check this link

Use Automation to Find a Location in a Word Document in Access
http://support.microsoft.com/?kbid=210032
0
 
Arno KosterCommented:
you should close the word object with

objWord.Close

before freeing the memory with the set nothing statement
0
 
cmhcycAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ALaRivaCommented:
Is there an error when it crashes?

Is it crashing on your computer or another computer?

HTH

-Anthony
0
 
cmhcycAuthor Commented:
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
 
cmhcycAuthor Commented:
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
 
Arno KosterCommented:
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
 
cmhcycAuthor Commented:
Hi akoster,

Thanks - it just takes 30secs longer to crash :((
0
 
cmhcycAuthor Commented:
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
 
Arno KosterCommented:
maybe the runtime doesn't support popups, strange...
0
 
cmhcycAuthor Commented:
...infuriating not seeing the wood for the trees - must have spent a good 2 days on this!
0
 
moduloCommented:
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now