specifying word folder to save in from VB in access

B

bilbo+

Hi there, I am currently filling in a word template from access when I click
a button,

when saving (doc.save) it saves to the default directory, however is there a
way of using one of the fields in my form as the save folder ( already
created).


objWord.Activate

On Error Resume Next

doc.Save


but i want it to choose strpath then \ Forms!Mjobs!("JobNo") \


any ideas?


also if there was a way of specifying the actual save filename as well
that'd be handy...

Thanks in advance...

WK

full code so far with explanation.... (in access VB)


Option Compare Database
Public Function fProcedure()

'Dimension variables to hold pointers to the Word Application and

' Word Document we will be working with.

'Early Binding Dimensioning

' To use these declarations, reference the Word Object Library

Dim objWord As Word.Application

Dim doc As Word.Document

'Late Binding Dimensioning

' To use these declarations, un-reference the Word Object Library

'Dim objWord As Object

'Dim doc As Object

'Flag to indicate We Opened Word

Dim bolOpenedWord As Boolean



'Get pointer to Word Object

' Handle Error In-Line

On Error Resume Next

Set objWord = GetObject(, "Word.Application")

If Err.Number = 429 Then

'If we got an error, that means there was no Word Instance

Set objWord = CreateObject("Word.Application")

'Set Flag to let us know we opened Word

bolOpenedWord = True

End If

'Make Word Instance visible

objWord.Visible = True

'Reset Error Handler

On Error GoTo 0



'Create New Blank Document from template

'Get Path of Current DB

strPath = CurrentDb().Name

'Strip FileName to Get Path to Doc

Do

lngInStr = InStr(lngInStr + 1, strPath, "\")

Loop While (InStr(lngInStr + 1, strPath, "\") <> 0)

'Get path up to the last ‘\’

strPath = Left(strPath, lngInStr)

'Append document name onto the end of the stripped path

strPath = strPath & "quotetemplate.dot"

Set doc = objWord.Documents.Add(strPath)

ActiveDocument.ActiveWindow.View.Type = wdNormalView



'adding Text

doc.Bookmarks("JobNo").Select

objWord.Selection.TypeText Forms!Mjobs!("JobNo")

doc.Bookmarks("Firstname").Select

objWord.Selection.TypeText Forms!Mjobs!("FirstName")

doc.Bookmarks("Lastname").Select

objWord.Selection.TypeText Forms!Mjobs!("LastName")

doc.Bookmarks("Company").Select

objWord.Selection.TypeText Forms!Mjobs!("CompanyName")

doc.Bookmarks("Hiredays").Select

objWord.Selection.TypeText Forms!Mjobs!("Days")

doc.Bookmarks("User").Select

objWord.Selection.TypeText CurrentUser()

'This will shift the focus to Word

objWord.Activate

'Save New Document

On Error Resume Next

doc.Save

'If the cancel button in the SaveAs dialog was presses

' You will receinve and error

' .Number - 4198

' .Description - Command Failed

On Error GoTo 0

'Close and release pointers

doc.Close False

Set doc = Nothing

'Did we create the Word instance we are using

' or did we reuse an open instance?

If bolOpenedWord = True Then

'We created an instance, so now we need to close it.

objWord.Quit

End If

Set objWord = Nothing

End Function
 
J

Jay Freedman

Instead of the .Save method, use the .SaveAs method. It takes a lot of
optional arguments, but the first one is FileName, which can include a path.

I'm not sure of the syntax you'd need in Access, but something along the
lines of

doc.SaveAs strpath & "\" & Forms!Mjobs!("JobNo") & "\report.doc"

should get you there.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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