Specify Word saving folder from access mail merge

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....


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
 
B

BeWyched

Hi

You should change the Word default file save path before your doc.save
statement:

objWord.Options.DefaultFilePath(wdDocumentsPath) = "path to the folder"

Note that Word will remember this path until its changed again (even if you
open Word outside of Access). Therefore, you should have a routine that
changes it back to the normal default when users exit the Access Form.

Hope this helps.

BW
 
B

BeWyched

Sorry, I didn't read all of your post....

You can save to a named file name using:

doc.SaveAs FileName:="youfile.doc"

Cheers.

BW
 
B

bilbo+

Many thanks for the reply, do you know how I would use that filename command
to get a field from access as the filename? e.g.

doc.SaveAs FileName:="Quote "JobNo".doc" or something liek that? Can it
even be done?

Thanks,

William
 
B

bilbo+

The defaulted folder will be different for every file that is saved since it
saves to a 'Job No'. When a job is created in my database it creates a
folder..... e.g. /data/7415

The 7415 comes from the 'JobNo' field. So I would need to default the save
directory each time to the relvant field in access.... is that possible?

So something like....

objWord.Options.DefaultFilePath(wdDocumentsPath) =
"//server/database/data/("JobNo")/"

Any help much appreciated! Thanks,

William
 
B

BeWyched

Hi - yes, no probs.

Use:

objWord.Options.DefaultFilePath(wdDocumentsPath) =
"//server/database/data/" & Me.JobNo & "/"

Similarly, for your Save path use something like:

doc.SaveAs FileName:= strpath & "\" & Me.JobNo & "\"

Cheers.

BW
 
B

bilbo+

Wow, excellent thanks for your help, ill give that a go in just a second!
Just quickly, in my code below I have set strpath as server/database/new/ but
i need this strpath to go to //server/database/data/ how do i go about
setting a different strpath for that little bit there? I'm a bit self taught
with all this so please bear with me!
thanks,

WK
 
B

bilbo+

Hello again,
on this line

objWord.Options.DefaultFilePath(wdDocumentsPath) = "//server/database/data/"
& Me.JobNo & "/"

an error comes up saying 'invalid use of Me keyword' - i have no idea why.

I also tried this since I realised surely I only need this SaveAs command
since it specifys the whole address therefore no need for a default save
directorY???


doc.SaveAs FileName:="//server/CKS database/CKS/data/" &
Forms!Mjobs!("JobNo") & "/"


That didnt work either, and when i use Me. i get the invalid use of keyword
error.... has this got anythign to do with running it as a function? or word
application being 'active'. should i save as just after it is created but not
active then use doc.save at the end??

help!!!

thanks,,,

Will
 
B

BeWyched

Hi

Use the Replace function:

strPathData = Replace(strPath, "new", "data")

then use strPathData as the new path. WARNING - this will only work if you
are certain that the string "new" will not appear earlier on in the path as
that will get replaced even its part of a bigger string (e.g.
c:\abcnewefg\etc would become c:\abcdataefg\etc). If this is possible then it
would be safer to use something like:

strPathData = Left(strPath, Len(strPath) - 4) & "/data/"

If these don't work then you can debug using, say,
Msgbox strPath
Msgbox strPathData

and see what they look like. Its quite common the get the odd "/" to many or
few, or to be using "/"'s when they sould be "\"'s - trial and error is the
easiest route if they're wrong.

Good luck.

BW
 
B

BeWyched

Hi

1. I assumed that the procedure is being called from a control called
'JobNo' on the active form which is called 'Mjobs'. If this is not the case
then just substitute 'Me' with 'Forms!Mjobs'.

2. Changing the default save path is needed if users are expected to save
the Word document from Word. If not, then you are quite correct - you do not
need to change the default path.

Cheers.

BW
 

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