Unable to save using SaveAs

M

Mr. Smart

Initially it may seem i am posting to a wrong group, but I am convined I am
not. I know the problem is coming from VBA's SaveAs. This is what I am trying
to do: I am writing a resume-generating program which I want to use to
respond to different jobs. For example, let's say there are 5 jobs. I want to
save the resumes using a name such as Adenigba Smart's Resume.doc. Here is a
code that I used. I used Foxpro to drive Word (it is part of a larger
program).

....
SCAN ** Scans thru the database

....

THIS.CVName = THIS.DocumentFolder + "\" + TRIM(Surname) + "\"
Trim(Firstname) + "'s Resume.doc"

....
ENDSCAN
For those that are not familiar with foxpro, I am sure you will be able to
follow thru. It simply gives a name to the document based on applicant's
name. The first time thru, it works OK, but after this it fails. The problem
occurs in the THIS.oWordoc.SaveAs(by the way, it simply calls VBA's SaveAs).
It returns the error message: OLE IDispatch exception code 0 from Microsoft
Word: This file is read-only and will not overwrite it. I tried to delete the
file using Foxpro's DELETE FILE. It failed. Since it contained of being read
only, i tried to change the read only status by using FileSystemObject (FSO )
to change to normal before deleting, but did not work (by the way, I used
Explorer to examine it, but it was not read-only). Since this code always
work the first time, I also try shutting down Word and re-starting, it also
failed. Please thus anybody out there know how to do this seemingly simply
task. How do I save this file successfully? By the way, I use Foxpro 6 on
Windows XP version 2002. Word XP. Thanks 4 your anticipated response
 
J

Jezebel

SaveAs works as expected from VBA; so the problem would appear to lie with
FoxPro. As an alternative approach, you could try saving the document to an
arbitrary temp file, then re-name it.
 
J

Jonathan West

You have shown us the line of code by which you build the filename. Could
you provide the line of code which you use to actually save the file?

Also, does the folder THIS.DocumentFolder + "\" + TRIM(Surname) exist at the
time you attempt to save the file. If not, then you need to create it before
you attempt to save the document, as otherwise you will be trying to save
the document into a nonexistent folder.

I assume you are also taking care to ensure that neither the surname nor the
firstname contain any characters prohibited in filenames.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
K

Kodeworks

Mr. Smart

Without knowing much about Foxpro, here are a couple of suggestions to
track down your problem.

1. When you create the Word application object to save the document
the first time, is the Word application visible? When the Word
application process is first created, it is invisible. To make it
visible, we would have run something (in VBA) like
objWord.Visible = True

2. With the Word application visible, is the document that you saved
the first time still open? If the document is still open then it
should be closed before attempting to save another document with the
same name, with something (again, in VBA) like
objDocument.Close SaveChanges:=wdDoNotSaveChanges 'or
objDocument.Close SaveChanges:=wdSaveChanges 'as the
case might be

3. If steps 1 and 2 reveal nothing out of the ordinary, using the Word
application object created by your Foxpro application, are you able to
do all those things manually that you wish to do in code - ie, create a
document, save a document, etc. Often, this step will give some clues
as to what's not working correctly.


Sunil Jadwani
Kodeworks - Business Automation Solutions
www.kodeworks.com
 
M

Mr. Smart

Thanks all for taking the time to reply. As I indicated earlier, there is
nothing wrong with the FoxPro code. The folder exists and the surname does
not contain illegal xters. Since it saved the first time, the folder must
have existed. Here is what I did:

THIS.oWordoc = THIS.oWord.Documents.Add(ALLTRIM(THIS.Templates) + “\†+
tempfile ,,, .T.)
…
THIS.oWordoc.WriteStandard(Applicants.Address,…)
THIS.oWordoc.WriteData(“Objectiveâ€, MatchedVacancies.Objective)
THIS.oWordoc.WriteData(“Executive Summaryâ€,Applicants.ExecutiveSummary)
…
THIS.oWord.SaveAs(THIS.CVName, FileFormat, LockComments, …, AddBIDIMarks)
THIS.OWord.ActiveDocument.Close()
THIS.RestartWord

I created many document templates and I called them Template1, Template2
etc. They contained exactly same code but I tried to change the visual
appearance as much as possible. So the first line of code opens any of those
template documents. They are stored in a folder called Templates, and that is
stored in THIS.Templates. tempfile can be Template1, Template 2, Template 3
etc. Each of the template documents contains Public Functions WriteStandard,
WriteData etc. Assuming I opened a document based on Template3 (say), those
functions are available. THIS.oWordoc refers to the active document.
WriteStandard writes the applicants address, e-mail, phone number etc.
centered and bold at the center of the document. WriteData accepts 2
parameters and writes them out e.g. OBJECTIVE Looking For The Position Of
Senior Programmer …. It does that until the end and saves the document.
Initially, this is what I have (and this is contained inside the document
file:
Public Function SaveCV(CVName As String)
ActiveDocument.SaveAs FileName := cvName, _
FileFormat := WdFormatDocument, _
AddToRecetFiles := False
ActiveDocument.Close
Then I called this function from Foxpro as THIS.oWordoc.SaveCV. Later I
decideded to change to the code above (THIS.oWord.SaveAs). I made sure I
closed the document as it is indicated above, but that did not solve the
problem and I felt it was bcos Word still retained the document’s handle,
thinking that by shutting down Word and restarting it, it is going to be OK.
However this did not solve the problem. Here is the code that I put in
ReStartWord.
THIS.oWord.Quit(0,,)
THIS.oWord=CREATEOBJECT(‘Word.Application’)
THIS.oWord.Visible = .T.
NOTE: I don’t need to make Word visible. I only did this for debugging
purpose so as to monitor things. The first time I opened word, I made it
visible to follow the progress. Perhaps this is a bug in Word and now what I
did is to assign random name and the code works OK, but I would really have
preferred something like Adenigba Smart’s resume.
Thanks all for taking time to assist.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top