Access and Word vba


I have the following

Dim objWord As Word.Application
Dim doc As Word.Document
Dim WordHeaderFooter As HeaderFooter

Set objWord = CreateObject("Word.Application")

With objWord
    .Visible = True

    Set doc = .Documents.Add
    doc.SaveAs CurrentProject.Path & "\TestDoc.docx"
End With

At the moment it is saving in the project folder. I am trying to change it so it saves to another location.
I have tried doc.SaveAs "C:\location\" & "\Doc.docx"  but it wont accept. any help appreciated

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>>I have tried doc.SaveAs "C:\location\" & "\Doc.docx"  but it wont accept. any help appreciated

1. make sure the selected folder exist?
2. make sure you have enough permission to write to that folder?
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
and you can try:
doc.SaveAs "C:\location\Doc.docx"

Open in new window

instead of:
doc.SaveAs "C:\location\" & "\Doc.docx"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chesteraAuthor Commented:
Ryan Chong

Hi Ryan When I  use doc.SaveAs "C:\location\" & "\Doc.docx"    I get Error 5152 check the path. I know the path is correct.

Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
as mentioned, have you tried?

>> doc.SaveAs "C:\location\Doc.docx"

is folder: C:\location exist and accessible with write permission?

can you post the full error message for Error 5152 ?
chesteraAuthor Commented:
Ryan Chong

I have fixed the problem. Looking the path all seemed ok so just to make sure I deleted the folder and re-installed and it now works must have been some small unseen corruption. Thank you for your help

chesteraAuthor Commented:
Thank you again
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:

Usually it is spaces in path names (sometimes it is hard to tell if you have more than one) or repeating back slashes, or a problem with delimiters.

 So here's a tip and this applies to anything that you might assemble as a reference  (like a SQL Statement your going to execute in code) from literals or other variables.   Always put it in a variable and execute it like this:

strFullPath = "C:\location\" & "\Doc.docx"  
doc.SaveAs strFullPath

The reason to do it this way (even though it is a little more overhead) is that you can put a break point on the line and inspect the full reference that the statement will use.

This makes it a lot easier to debug and spot mistakes as often when using other variables, the problem is with the data which you don't see.  For example, in a SQL Statement, you might have a last name of "D'Angelo", with that extra apostrophe giving you all kinds of grief.

In this case, it would become obvious that you had:


 Maybe that was a typo, but maybe not.  Anyway, you can see what I am getting at.

chesteraAuthor Commented:
Jim Dettman

Hi Jim

Thank you for that advice most appreciated

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.