Log in

View Full Version : [SOLVED:] Setting Default file path in VBA code



Prajith_VN
06-18-2012, 07:07 AM
Hi,

I am a new member in VBA forum. I have created a userform with word VBA to accept data. I have a 'SAVE' command button which when clicked should open a new word document and put all the data from the userform into it and save the document. The problem i face is that I am not able to save the document to the specified location. when I click save the word document is open with the data from the userform but it is not saved to the exact location mentioned in the code. I have created a folder in C:\MyDocuments called VBA and gave the file path in the program. But the word document is getting saved in MyDocuments and not in VBA. and the strange part is that the Folder name VBA is added as prefix to the file name. Please look at the code pasted below:


Private Sub save1_Click()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim filePath As String
Dim i As Integer
Dim strPath As String
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add
strPath = "C:\Users\Admin. RTC\Documents\VBA" & Name1.Text & "_" & refnum1.Text & "_" & date1.Text
With wrdApp
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeParagraph
.Selection.TypeText Text:=XName.Caption & ":" & Space(5) & Name1.Text
.Selection.TypeParagraph
If XMale1.Value = True Then
.Selection.TypeText Text:=XSex.Caption & ":" & Space(5) & "Male"
ElseIf XFemale1.Value = True Then
.Selection.TypeText Text:=XSex.Caption & ":" & Space(5) & "Female"
End If
End With
wrdApp.ActiveDocument.SaveAs FileName:=strPath
End Sub

Tinbendr
06-18-2012, 08:14 AM
Welcome to the board!

You can't have colon in filename.

Try this.


Private Sub save1_Click()
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim filePath As String
Dim i As Integer
Dim strPath As String
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add
filePath = "C:\Users\Admin. RTC\Documents\VBA" & Name1.Text & "_" & refnum1.Text & "_" & date1.Text
With wrdApp
.Range(0, 0).Text = XName.Caption & ":" & Space(5) & Name1.Text
If XMale1.Value = True Then
strPath = XSex.Caption & Space(5) & "Male"
ElseIf XFemale1.Value = True Then
strPath = XSex.Caption & Space(5) & "Female"
End If
End With
wrdApp.ActiveDocument.SaveAs Filename:=filePath & "\" & strPath
End Sub

Prajith_VN
06-19-2012, 12:31 AM
Hi David,

I think you got me all wrong. I did not use colon for file name. the part of code with colon actually writes the data entered in the user form to a new word document. For example if I enter a name 'Prajith' in the name text box in the userform & when i click save it should open a new word document in which the entered data is dispayed as 'Name : Prajith' , I used colon for this, not for the file path or name.

Regards,
Prajith

IanFScott
06-19-2012, 05:36 AM
You are just missing an extra [\] after VBA for:

filePath = "C:\Users\Admin. RTC\Documents\VBA" & Name1.Text & "_" & refnum1.Text & "_" & date1.Text
Use:
filePath = "C:\Users\Admin. RTC\Documents\VBA\" & Name1.Text & "_" & refnum1.Text & "_" & date1.Text
Currently you are saving a file named VBAPrajith_Ref_date into Documents folder

Prajith_VN
06-19-2012, 05:40 AM
It Works!!! Thank you very much Ian. Appreciate your timely help.

fumei
06-19-2012, 02:55 PM
As a best practice, whenever you are using a string variable for a folder, make sure it terminates with a path separator.