By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,594 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,594 IT Pros & Developers. It's quick & easy.

writing to an external file

P: 21
hey guys,

just a quickie that's probably been asked a million times, but i couldn't find it when searching so here goes:

i need to copy the contents of a word document and paste it to an external file (.txt) using VBA how do i go about doing this?

cheers Black Box
Nov 22 '06 #1
Share this Question
Share on Google+
11 Replies


100+
P: 267
hey guys,

just a quickie that's probably been asked a million times, but i couldn't find it when searching so here goes:

i need to copy the contents of a word document and paste it to an external file (.txt) using VBA how do i go about doing this?

cheers Black Box
hi

try using menu Extra | macro | record New Macro
then perform a 'save as ...' as txt file.
close the recording and look what word has written (Alt+F11)

something like
Expand|Select|Wrap|Line Numbers
  1.  
  2.    ActiveDocument.SaveAs FileName:="NewName.txt", FileFormat:=wdFormatText, _
  3.         LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
  4.         :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
  5.         SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
  6.         False
  7.  
  8.  
Nov 22 '06 #2

P: 21
that's fine, but later i need to refer back to that text. how would i do that?

black box
Nov 22 '06 #3

100+
P: 267
that's fine, but later i need to refer back to that text. how would i do that?

black box
hi if you want it accessed by VB you need only to use OPEN ...

Open "Newname.txt" For Input As #1
While Not EOF(1)
Line Input #1, p
..
.. put any nice code here ..
..
Wend
Close #1
Nov 22 '06 #4

P: 21
hate to be a pain but could you explan what each line of the code does, i don't just copy and paste code, i have to learn what it does.

Black box
Nov 22 '06 #5

100+
P: 267
hate to be a pain but could you explan what each line of the code does, i don't just copy and paste code, i have to learn what it does.

Black box
hi here you go

Open "Newname.txt" For Input As #1
opens the file you just wrote using the macro from previous mail for reading and call that #1

While Not EOF(1)
if the file has not ended (eof=end of file)

Line Input #1, p
then read a line and call that p

..
.. put any nice code here ..
don't know what you want to do with p
..

Wend
do so until you don't have any more lines in your file

Close #1
and don't forget to close, otherwise you can't open it again.
Nov 22 '06 #6

P: 21
ok how can i apply this method to writing to the text file? the save as option doesn't quite do what i want.

black box
Nov 22 '06 #7

100+
P: 267
ok how can i apply this method to writing to the text file? the save as option doesn't quite do what i want.

black box
hi

to avoid endless communication, can you provide me with info what you want to do in the textfile and how you want to save it again?
Nov 22 '06 #8

P: 21
apollogies,

when i'm working on the text in a word document i want to make a backup so if i make a mistake i can come back to the original.

I need to copy the text out of the document, carry on working in that document and then copy the text back in.

any clearer?

black box
Nov 22 '06 #9

100+
P: 267
apollogies,

when i'm working on the text in a word document i want to make a backup so if i make a mistake i can come back to the original.

I need to copy the text out of the document, carry on working in that document and then copy the text back in.

any clearer?

black box
yes ok, but why you want a backup as .txt file i don't see.
disadvantage of a textfile from word is, that you only save plain text.
why not set option to create always a backup file ?


[
Nov 22 '06 #10

P: 21
ok maybe backup was the wrong choice of words,

i'm making a cryptanalysis script which involves guessing to an extent, so if i guess the wrong set of characters and VB carries on manipulating the text and writing to the document, i want to be able to go back to the old text. so plain text is the only thing i want to be able to recover.

Black box
Nov 22 '06 #11

100+
P: 267
ok maybe backup was the wrong choice of words,

i'm making a cryptanalysis script which involves guessing to an extent, so if i guess the wrong set of characters and VB carries on manipulating the text and writing to the document, i want to be able to go back to the old text. so plain text is the only thing i want to be able to recover.

Black box

hi

Expand|Select|Wrap|Line Numbers
  1. Private Sub document_close()
  2. ActiveDocument.SaveAs FileName:="Original.doc", fileformat:=wdFormatDocument
  3. ActiveDocument.SaveAs FileName:="BackUp.txt", fileformat:=wdFormatText
  4. End Sub
  5.  
put this code in your codepage belonging to ThisDocument (NOT the module)


the document will be saved as "backup.txt" and as "original.doc", when closing the document
Nov 22 '06 #12

Post your reply

Sign in to post your reply or Sign up for a free account.