Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change File>SaveAs Location in Word

Status
Not open for further replies.

jtrapat1

Programmer
Jan 14, 2001
137
US
I'm using Visual Basic 6 against an SQL Server 7 database.
One form contains an OLE object which when double-clicked, will open a word document (Microsoft Word
97) of a Bill.

I had written a macro in a Word template that did what I wanted but I'd like to write the logic on the
VB side.

Basically, the idea is to allow the Users who don't have permission to edit the document ( ReadOnly
= false), to save a copy of this file to a directory on their D:\ drive. So the common dialog defaults
to their D:\ drive. If they do have permission, the SaveAs common dialog box will allow them to saveas
to the current directory, on the network.

Can this be done?
The macro ran fine and did what I wanted but some things in VBA don't transfer over to VB completely.

For example, the error number 4172 was skipped when I stepped thru the code.
On the VB side, I'm checking a Boolean value for readonly:
If the value is false, allow the users to SaveAs to a newly created D: directory on their computer.
If the directory doesn't exist, create it and save the file.
If readonly is true, when the user clicks SaveAs from Word, the common dialog opens to the current directory
and saves the file there, with changes.

Here's my macro:

Sub FileSaveAs()
On Error GoTo Save_Err

BAMDir$ = "D:\BAMS"
CurrentFile$ = ActiveDocument.Name
Application.ChangeFileOpenDirectory BAMDir$

ActiveDocument.SaveAs FileName:=CurrentFile$
MsgBox "File Saved."

Save_End:
Exit Sub

Save_Err:
If Err.Number = 4172 Then
If MsgBox("The Directory " & BAMDir$ _
& " Is Not On This System." & vbCrLf _
& "We Suggest You Use This " _
& "Directory For Your BAM Word Files." & vbCrLf _
& "Would You Like To Create It Now? ", _
vbYesNo, "Bill Tracking") = vbYes Then

MkDir BAMDir$

Resume
Else
Resume Save_End
End If
End If

End Sub

And here's what I have so far in VB:
(Error 4172 is not a valid error in VB, I guess)

Private Sub BamMSWord_Click()
Dim BAMDir As String
Dim CurrentFile As String

On Error GoTo Save_Err:

Set MyWord = New Application
MyWord.Documents.Open BamFileName, , BamReadOnly
CurrentFile = BamFileName

BAMDir = "D:\BAMS"
If BamReadOnly Then
MyWord.Documents.Open BamFileName, , BamReadOnly
Else
MyWord.Visible = True
MyWord.Width = 600
MyWord.Height = 440
MyWord.Left = 0
MyWord.Top = 0

MyWord.ChangeFileOpenDirectory BAMDir
MyWord.ActiveDocument.SaveAs CurrentFile
MsgBox "File Saved."

End If
Save_End:
Exit Sub

Save_Err:
If Err.Number = 4172 Then
If MsgBox("The Directory " & BAMDir _
& " Is Not On This System." & vbCrLf _
& "We Suggest That You Use This" _
& "Directory For Your BAM Word Files." & vbCrLf _
& "Would You Like To Create It Now?", _
vbYesNo, "Bill Tracking") = vbYes Then

MkDir BAMDir
Resume
Else
Resume Save_End
End If
End If

End Sub

Basically, I'd like to change the File>SaveAs default directory in a Word Document, from Visual Basic,
based on a boolean value, if possible.

Is it possible to control the MS Word toolbars from VB like this?
Thanks in Advance
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top