+ Reply to Thread
Results 1 to 14 of 14

VBA - Create Word Document or Edit Document if exists

  1. #1
    Registered User
    Join Date
    12-06-2012
    Location
    australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    VBA - Create Word Document or Edit Document if exists

    Hi Everyone,

    I have two buttons that I want only 1, the problem I have is the first button overrides any files with the same name, I want it to check if exists then action 2nd button.

    I know it is bad code, any way to simplify would be great

    Thanks Heaps

    Code below

    Private Sub CreateFilenote_Click()
    ' create filenote button
    Dim fso, MyFile, strName, strBranch, WSName, CName, strFile, strInput, strExt
    Dim YesOrNoAnswerToMessageBox As String
    Dim QuestionToMessageBox As String

    QuestionToMessageBox = "Have you created a Filenote before"

    YesOrNoAnswerToMessageBox = MsgBox(QuestionToMessageBox, vbYesNo, "Create New Filenote")

    If YesOrNoAnswerToMessageBox = vbYes Then
    MsgBox "Please edit the original file!"
    Else
    WSName = "Tracker"
    CName = "C1"
    strBranch = Sheets(WSName).Range(CName).Text
    strFile = "G:\" & strBranch & "\name\Filenotes\"
    strName = InputBox("Enter Applicants SURNAME:" & vbCrLf & vbCrLf & "Please enter SURNAME in capitals", _
    "Create File")
    strExt = ".doc"
    strInput = (strFile & strName & strExt)
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set MyFile = fso.CreateTextFile(strInput, True)
    MyFile.WriteLine ("")
    MyFile.Close
    MsgBox "Filenote for " & strName & " Created "

    Exit Sub
    End If

    Private Sub AddFilenote_Click()
    ' add filenote button
    Dim fso, f, ReadAllTextFile, strFile, strName, strExt, strInput, strNotes, strBranch, WSName, CName
    WSName = "Tracker"
    CName = "C1"
    strBranch = Sheets(WSName).Range(CName).Text
    strFile = "G:\" & strBranch & "\name\Filenotes\"
    strName = InputBox("Enter Applicants SURNAME:", _
    "Create File")
    strExt = ".doc"
    strInput = (strFile & strName & strExt)
    filenote.Show
    strNotes = filenote.TextBox1
    Set strNotes = filenote.TextBox1
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile(strInput, 1)
    ReadAllTextFile = f.ReadAll
    Set f = fso.OpenTextFile(strInput, 2, True)
    f.WriteLine (Date & "," & vbCrLf & strNotes)
    f.Write (ReadAllTextFile)

    MsgBox "Filenote Added - Thanks"


    End Sub

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: VBA - Create Word Document or Edit Document if exists

    Your code can create or edit text document, not word document
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    12-06-2012
    Location
    australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: VBA - Create Word Document or Edit Document if exists

    I would prefer it to create Word Documents currently it creates txt file and names it .doc need work around

    Thanks

  4. #4
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: VBA - Create Word Document or Edit Document if exists

    I did not understand, do you want create text files or word documents ?
    if text file why di you put word in the title ?

  5. #5
    Registered User
    Join Date
    12-06-2012
    Location
    australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: VBA - Create Word Document or Edit Document if exists

    I want to create word document not text file that's why I need help

  6. #6
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: VBA - Create Word Document or Edit Document if exists

    try this code, you can arrange it.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    12-06-2012
    Location
    australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: VBA - Create Word Document or Edit Document if exists

    Thanks for the reply are you able to help with the following

    I will have a UserForm with a single line Text Box for the filename.doc and a Multiline Text box for the Text that will be added to the Document. I then want a Button to Check if the filename.doc exists and if it does add contents of the Multiline Textbox to the top of the Document, if the file does not exist I want the filename.doc to be created and then add the Multiline Text to the Documents and save the file.

    I hope this helps clear up what I need help with

    Cheers
    Last edited by unstable81; 01-10-2013 at 10:26 AM.

  8. #8
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: VBA - Create Word Document or Edit Document if exists

    I don't understand, my code works in both cases, if exist or not. Did you try it ?

  9. #9
    Registered User
    Join Date
    12-06-2012
    Location
    australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: VBA - Create Word Document or Edit Document if exists

    I tried it but couldnt get it to add new text to the start of existing document without overwriting the old text in the document. Can you help add textbox1.value for filename and textbox2.value for txt to be added.

    Thanks

  10. #10
    Registered User
    Join Date
    12-06-2012
    Location
    australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: VBA - Create Word Document or Edit Document if exists

    ActiveSheet.UsedRange.Copy ' <<<< put some data in the sheet

    I think this is we're I'm not understanding, how do copy the text from textbox2

  11. #11
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: VBA - Create Word Document or Edit Document if exists

    I attached an example of code, it can add the content of sheet to a word document, if you can not arrange it for your work attach a sample file

  12. #12
    Registered User
    Join Date
    12-06-2012
    Location
    australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: VBA - Create Word Document or Edit Document if exists

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    12-06-2012
    Location
    australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: VBA - Create Word Document or Edit Document if exists

    Please point out any errors in the butched code

    if i change
    strName = UserForm1.TextBox1.Text
    strFilenote = UserForm1.TextBox2.Text
    to
    strName = "text"
    strFilenote = "text2"

    It works

    Cheers
    Last edited by unstable81; 01-11-2013 at 06:24 AM.

  14. #14
    Registered User
    Join Date
    12-06-2012
    Location
    australia
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: VBA - Create Word Document or Edit Document if exists

    This works but I think I there might be a simpler way to achieve what im doing

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1