Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2007

Tip: Looking for answers? Try searching our database.

Creating a document in VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Glenn Suggs - 21 Feb 2007 16:56 GMT
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.
Signature

Glenn

Helmut Weber - 21 Feb 2007 21:23 GMT
Hi Glenn,

>    Set wrdApp = New Word.Application
>    Documents.Add DocumentType:=wdNewBlankDocument
[quoted text clipped - 4 lines]
>         {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.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Jean-Guy Marcil - 21 Feb 2007 21:30 GMT
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.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.