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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stylus
    New Member
    • Aug 2012
    • 1

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

    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:

    Code:
    Sub BreakOnPage()
       ' Used to set criteria for moving through the document by page.
       Application.Browser.Target = wdBrowsePage
    
       For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")
          
          'Select and copy the text to the clipboard
          ActiveDocument.Bookmarks("\page").Range.Copy
    
          ' Open new document to paste the content of the clipboard into.
          Documents.Add
          Selection.Paste
    ' Removes the break that is copied at the end of the page, if any.
          Selection.TypeBackspace
          ChangeFileOpenDirectory "C:\Documents and Settings\Administrator\Desktop\UploadXML"
          DocNum = DocNum + 1
          ActiveDocument.SaveAs FileName:="Document_" & DocNum & ".txt", FileFormat:=wdFormatText
          ActiveDocument.Close
    
          ' Move the selection to the next page  in the document
          Application.Browser.Next
       Next i
       ActiveDocument.Close savechanges:=wdDoNotSaveChanges
    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
  • genek
    New Member
    • Sep 2012
    • 3

    #2
    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.

    Comment

    Working...