Results 1 to 13 of 13

Thread: WORD VBA - Open and save file to new location

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #9
    Quote Originally Posted by fumei
    This is not what you asked. What does:

    (1) Read available text files in the current directory
    (2) Choose one file (e.g., mTextFile.txt) and open it
    (3) Save the mTextFile.txt to a text box (editing file in text box).
    (4) Save the changes to either the same text file or to a new textfile

    have do with Active Document? If nothing, please state a bit more precisely what you actually do want to do. For example #4 is rather vague...that "or" make quite a bit of difference.

    Plus you do not state if the textbox exists already, or are you making a new textbox. I really do not know what you mean by "editing file in text box". You do not edit any files in text boxes, you can change a string in a textbox.
    Sorry for the confusion, I did not put as much detail in as I sould have. Let me be more clear.
    - I have a user form that pops up automatically when the word document (userform.doc) is opened.
    - The user can click a button on the form called "readRIS" which opens the wdDialogFileOpen dialog. It shows the current directory where the userform.doc is located.
    - The user can click on a file to select it. The file to open is a text file but has a .ris extension (e.g., myreferences.ris)
    - NOTE: I use ".Display" instead of ".Show" to avoid executing the open command. I just want to get the file name so I can read the file in.

    Code:
    ---------------------------
    With Application.Dialogs(wdDialogFileOpen)
    .Display
    sFilename = .Name
    End With
    ---------------------------
    - I copy the contents of the textfile as a string to a textbox (called fText) on the form (already existing).
    - I send the string containing the contents of "myreferences.ris" in the textbox to a parser which cleans up the data.
    - The user can edit the text in the textbox (ftext).

    Code
    ---------------------------
    Public Sub ReadRIS_Click()
    Dim sPath As String
    With ActiveDocument
    ' Get filename
    With Application.Dialogs(wdDialogFileOpen)
    .Display
    sFilename = .Name
    End With
    ' Read .ris file
    fText = ReadTextFile(sFilename)
    ' parse .ris file
    fileStr = ParseRIS(fText)
    ' display .ris file and file name
    fText = fileStr ' write parsed file to textbox
    RISFileName = "source: " & sFilename ' show filename in textbox
    End With
    End Sub
    ---------------------------
    - I now want the user to click save button (already existing) that saves the edited info in the textbox (ftext) to a different text file called myreferences.txt.
    - Now there are two text files existing with the same name but different extensions. The first is the original myreferences.ris file and the second is the cleaned and edited version with a myreferences.txt extension.

    It is working up to the point of saving the Textbox (ftext) to a new file called myreferences.txt. This is the part I am having trouble with.
    Last edited by jspattavina; 09-07-2010 at 10:07 AM.

Posting Permissions

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