Creating a new document in VBA

G

Glenn Suggs

I'm trying to create a new Word document using VBA from Access and am having
problems when I go to save the document. An error message says that there is
no document currently open (to save).

Here is the way the code begins (after setting a reference to Word):
Set wrdApp = New Word.Application
Documents.Add DocumentType:=wdNewBlankDocument
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="Typing here for the body of the document."
{more typing}
{more typing}
{more typing}

wrdApp.ActiveDocument.SaveAs FileName:=strTempFile, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False

During this "Save" process, that's when the error occurs.
Any suggestions?

Thanks in advance.
 
J

Jean-Guy Marcil

Glenn Suggs was telling us:
Glenn Suggs nous racontait que :
I'm trying to create a new Word document using VBA from Access and am
having problems when I go to save the document. An error message
says that there is no document currently open (to save).

Here is the way the code begins (after setting a reference to Word):
Set wrdApp = New Word.Application
Documents.Add DocumentType:=wdNewBlankDocument
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="Typing here for the body of the
document." {more typing}
{more typing}
{more typing}

wrdApp.ActiveDocument.SaveAs FileName:=strTempFile, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False

During this "Save" process, that's when the error occurs.
Any suggestions?

Thanks in advance.

Try changing

Documents.Add DocumentType:=wdNewBlankDocument

to

wrdApp.Documents.Add DocumentType:=wdNewBlankDocument

You are creating an application object but are not referencing it in the
code:
Set wrdApp = New Word.Application
creates a Word Application, but
Documents.Add DocumentType:=wdNewBlankDocument
adds a document to some Word Application, not necessarily the one you just
created, so, when you get to
wrdApp.ActiveDocument.SaveAs
there may well be no document active/opened in the application.

Overall, I would rewrite the code to make use of objects and of the Range
object, using the selection object and ActiveDocument, especially when
creating document remotely, is asking for trouble.
Something like:

'_______________________________________
Sub CreateDoc()

Dim wrdApp As Word.Application
Dim docCreate As Word.Document
Dim rgeDoc As Word.Range
Dim strTempFile as String

strTempFile = "C:\Documents\MyDoc.doc"

Set wrdApp = New Word.Application
Set docCreate = wrdApp.Documents.Add(DocumentType:=wdNewBlankDocument)
Set rgeDoc = docCreate.Range

With rgeDoc
.InsertParagraphAfter
.InsertParagraphAfter
.InsertAfter "Typing here for the body of the document."
'{more text}
End With

docCreate.SaveAs strTempFile
docCreate.Close wdSaveChanges
wrdApp.Quit

Set wrdApp = Nothing

End Sub
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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