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

Saving a MS Word doc filename from a string in the document using a macro

P: 1
Hi everyone,

I wonder if someone can help me. Ive scoured the internet for an answer to what I believe is a simple task but cannot find anything that matches my requirements at all.

Basically I have a document which is created from SharePoint using my Mailmerge web part. This creates 1x Word document with currently 100 pages.

I have written a macro to resave this 100page document into 100 separate 1x page documents and to automaticall convert them to .txt files.
However, they currently are called 'Document_1.txt', 'Document_2.txt' etc etc

What I am trying to achieve is to take either the value of the header of the document or a string found in the document and use this string as the filename of document when saved so every document would have a different filename, e.g 'Reading Policy.txt', 'Writing Policy.txt' etc.

The code I currently have is:

Expand|Select|Wrap|Line Numbers
  1. Sub BreakOnPage()
  2.    ' Used to set criteria for moving through the document by page.
  3.    Application.Browser.Target = wdBrowsePage
  5.    For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
  7.       'Select and copy the text to the clipboard
  8.       ActiveDocument.Bookmarks("\page").Range.Copy
  10.       ' Open new document to paste the content of the clipboard into.
  11.       Documents.Add
  12.       Selection.Paste
  13. ' Removes the break that is copied at the end of the page, if any.
  14.       Selection.TypeBackspace
  15.       ChangeFileOpenDirectory "C:\Documents and Settings\Administrator\Desktop\UploadXML"
  16.       DocNum = DocNum + 1
  17.       ActiveDocument.SaveAs FileName:="Document_" & DocNum & ".txt", FileFormat:=wdFormatText
  18.       ActiveDocument.Close
  20.       ' Move the selection to the next page  in the document
  21.       Application.Browser.Next
  22.    Next i
  23.    ActiveDocument.Close savechanges:=wdDoNotSaveChanges
  24. End Sub

The text that I want to use to name my documents is ALWAYS on the same line in each word document (once parsed into separate documents). That is line 10 and inbetween the text <Value> and </Value>

Can somebody help me with the extra code I need to do this please?

Rick Lister
Aug 8 '12 #1
Share this Question
Share on Google+
1 Reply

P: 3
I think I can help you if you post enough of your text file to indicate where on line 10 and exactly how the text to be used as the target filename is framed. The botton line is you must be able to SELECT the text to be used from the text file in order to use it in the target filename. The normal VB find text will work if it is framed properly (i.e. "|filename|", surrounded by pipe charaters). It could be found by doing a search for "|*|". Once found you can select it as the current range or paragrah and assign it to a string variable. After it is in a string variable it appears you got from there, looking at your code.
Sep 4 '12 #2

Post your reply

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