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