How to use a piece of text from a Wordfile in its filename?

  • Thread starter Joost 'Yoast' Nicasie
  • Start date
J

Joost 'Yoast' Nicasie

Hi,

I've got a large Wordfile, merged, thus divided in sections. I've
found a macro that cuts it into pieces, but... I want them to be saved
as .txt-files, each one in its specific name...

In each file I've made a remark: 'save this one as blahblah.txt' etc.

Is it possible to use this text to save it?

TIA,
Joost
 
J

Jonathan West

Ho Joost.

There are two parts to this problem.

1. identifying the text in the document that contains the filename to be
used.

2. using it to save the file.

If you can describe in ordinary words how a complete idiot can identify the
filename to be used, then it will be possible to produce a VBA equivalent.

For using the name when saving the file, plug the filename into the SaveAs
method. If the filename in the text doesn't include the path, I would
recommend you append the filename to the path of the folder you want the
file saved into, so that there is no mistake about where the file is saved
 
P

Perry

Although rather simple, but here's an example of how you might appraoch
this:

Dim s As Word.Section
Dim d As Word.Document
Dim w As String
Dim i As Integer

For Each s In ActiveDocument.Sections
'you'll have to use your 'remark' for a filename
'in this example, i've used first word in the section
w = s.Range.Words(1)

i = FreeFile
Open "c:\temp\" & w & ".txt" For Output As #i
Print #i, s.Range.Text
Close #i
Next

Krgrds,
Perry
 
J

Joost Nicasie

Perry said:
For Each s In ActiveDocument.Sections
'you'll have to use your 'remark' for a filename
'in this example, i've used first word in the section
w = s.Range.Words(1)

Well, almost there, but still a little problem here. Each section
starts with the same two paragraphs:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-Dit document opslaan als xxxxxxxxxx.html" -->

The filename I intend to use has always the same length: ten
characters and .html.

Forgive me for my complete ignorance here... :-s

Cheers,
Joost
 
P

Perry

Try below sequence

For Each s In ActiveDocument.Sections
'second paragraph
Set r = s.Range.Paragraphs(2).Range

'validate text of 2nd paragraph
If InStr(r, "<!-Dit document opslaan als") Then

'retrieve filename
sFileName = Mid(r, 28, Len(r) - InStr(r, ".html") - 1)
sFileName = Replace(sFileName, ".html", ".txt")

'check whether filename is correctly extracted
MsgBox sFileName
End If
Next

Krgrds,
Perry
 
J

Joost Nicasie

Perry said:
Try below sequence

OK... now we have the solution! It's:

Sub KnipSamengevoegdBestandInHTMLStukjes()
For Each s In ActiveDocument.Sections
'second paragraph
Set r = s.Range.Paragraphs(2).Range

'validate text of 2nd paragraph
If InStr(r, "<!--Dit document opslaan als ") Then

'retrieve filename
sFilename = Mid(r, 30, Len(r) - InStr(r, ".html") + 1)

i = FreeFile
Open "c:\temp\" & sFilename & ".html" For Output As #i
Print #i, s.Range.Text
Close #i

End If

Next
End Sub

And this does the trick... which used to cost me at least one full day
of work... thank you soo much!

Joost (his weekend made)

--
"I believe the light that shines on you
Will shine on you forever
And though I can't guarantee
There's nothing scary hiding under your bed
I’m gonna stand guard
Like a postcard of a Golden Retriever"
--Paul Simon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top