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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to automate Word using VB code 1

Status
Not open for further replies.

Djackola

Technical User
Jan 31, 2011
7
Hi ALL

i have a database with 150 record. I have a form and inside the form o created 2 buttons , one to Create folder and the other button to view the created folder.
I set up the code that to creat the folder for every client using the first name field and Surename field and the ID number in spicific location . eg if i have client name is John Murphy and his ID is 255 , so the created folder will be JOHN Murphy_255

Now i want to add a third button in my form to creat for me a Word Document usingn the path Firstname Surname_ID_00 and the second on will be Firstname surname_ID_01


Can i do that ???
appreciate your help
REgards
 
Change file name in CreateNewWordDoc
Code:
 .SaveAs (AppPath & "\ MyNewWordDoc.doc")

Code:
Sub CreateNewWordDoc()
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document
    Dim AppPath As String
    AppPath = Application.CurrentProject.Path

    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    Set wrdDoc = wrdApp.Documents.Add
    With wrdDoc
        .SaveAs (AppPath & "\ MyNewWordDoc.doc")
        .Close
    End With
    wrdApp.Quit
    Set wrdDoc = Nothing
    Set wrdApp = Nothing
End Sub

Sub CreateFolder()
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists("C:\scripts") Then
        MsgBox "Folder Exists"
    Else
        objFSO.CreateFolder ("C:\Scripts")
    End If
    Set objFSO = Nothing
    Dim x As Variant
    x = Shell("EXPLORER.EXE C:\Scripts\", vbNormalFocus)
End Sub

Sub OpenFolder()
    Dim x As Variant
    x = Shell("EXPLORER.EXE C:\Scripts\", vbNormalFocus)
End Sub

Hope this helps.
 
Hi Jambai

Thank you very much for your reply .
I think I did wrong step that your code didn’t work for me , however I will try her to post the codes that working for me now ( to create a folder and view folder).

Create folder code:
Private Sub CreateFolder_Click()
Dim strname As String
Dim strfname As String

On Error GoTo 1
strname = MemberName
strfname = MemberFamilyName
MkDir "C:\Users\Moawia\Desktop\sOURCE\" & strname & " " & strfname & "__" & ID
MsgBox (strname & " " & strfname & "__" & ID & " has been created successfully")
Exit Sub
1 MsgBox ("Could not create folder for this Client " & vbNewLine & "Folder may created already or " & vbNewLine & "Please check the Client's name and Client's family name.")
End Sub

Private Sub CreateFolder_Click()
Dim strname As String
Dim strfname As String

On Error GoTo 1
strname = MemberName
strfname = MemberFamilyName
MkDir "C:\Users\Moawia\Desktop\sOURCE\" & strname & " " & strfname & "__" & ID
MsgBox (strname & " " & strfname & "__" & ID & " has been created successfully")
Exit Sub
1 MsgBox ("Could not create folder for this Client " & vbNewLine & "Folder may created already or " & vbNewLine & "Please check the Client's name and Client's family name.")
End Sub

View folder code:
Private Sub ViewFolder_Click()
Dim strname As String
Dim strfname As String
Dim path As String


On Error GoTo 1
strname = MemberName
strfname = MemberFamilyName
path = "Explorer.exe C:\Users\Moawia\Desktop\sOURCE\"
folder = strname & " " & strfname & "__" & ID
filename = path & strname & " " & strfname & "__" & ID

If Dir("C:\Users\Moawia\Desktop\sOURCE\" & folder, vbDirectory) <> "" Then
Shell (filename), vbMaximizedFocus

Else
MsgBox ("Folder not exist")
End If
Exit Sub
1 MsgBox ("Could not view folder for this Client, folder may not exist.")
End Sub

So I need the word document to be created at the same path ( Inside the client folder).


Regards
 
Hi
I have a database with 150 record. I have a form and inside the form o created 2 buttons , one to Create folder and the other button to view the created folder.I set up the code that to creat the folder for every client using the first name field and Surename field and the ID number in spicific location . eg if i have client name is John Murphy and his ID is 255 , so the created folder will be JOHN Murphy_255Now i want to add a third button in my form to creat for me a Word Document usingn the path Firstname Surname_ID_00 and the second on will be Firstname surname_ID_01
here the codes that im using to create and view folders:

Private Sub CreateFolder_Click()
Dim strname As String
Dim strfname As String

On Error GoTo 1
strname = MemberName
strfname = MemberFamilyName
MkDir "C:\Users\Moawia\Desktop\sOURCE\" & strname & " " & strfname & "__" & ID
MsgBox (strname & " " & strfname & "__" & ID & " has been created successfully")
Exit Sub
1 MsgBox ("Could not create folder for this Client " & vbNewLine & "Folder may created already or " & vbNewLine & "Please check the Client's name and Client's family name.")
End Sub

View Folder Code:
Private Sub ViewFolder_Click()
Dim strname As String
Dim strfname As String
Dim path As String


On Error GoTo 1
strname = MemberName
strfname = MemberFamilyName
path = "Explorer.exe C:\Users\Moawia\Desktop\sOURCE\"
folder = strname & " " & strfname & "__" & ID
filename = path & strname & " " & strfname & "__" & ID

If Dir("C:\Users\Moawia\Desktop\sOURCE\" & folder, vbDirectory) <> "" Then
Shell (filename), vbMaximizedFocus

Else
MsgBox ("Folder not exist")
End If
Exit Sub
1 MsgBox ("Could not view folder for this Client, folder may not exist.")
End Sub

Appreciate you help
thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top