Creating a 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.
 
H

Helmut Weber

Hi Glenn,
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}

the Word-object is missing in your code.
wrdapp.Documents.Add DocumentType:=wdNewBlankDocument

Still better like this:

Public Sub Test80()
Dim YourText As String
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = New Word.Application
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add(DocumentType:=wdNewBlankDocument)
YourText = Chr(13) & Chr(13)
YourText = YourText & "Typing here for the body of the document."
wrdDoc.Range.InsertBefore YourText
wrdDoc.Range.Select
wrdDoc.ActiveWindow.Selection.Collapse Direction:=wdCollapseEnd
' ...
End Sub

Note, that the selection is a property of a window,
not of the application and not of the document,
which does not matter much when working within Word.

But that's another story.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
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).


Already answered in vba.general.

Please do not post multiple copies of the same message to different groups.
It just wastes everybody's time and uses bandwidth for nothing.

--

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