Consulting

Results 1 to 18 of 18

Thread: saving word file based on first line of text

  1. #1

    saving word file based on first line of text

    I have a large file full of letters that need to be renamed based on the information in the first line of the document. Is there a fast way to do this using MACROS?

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yes.

    Get the text of the first line - and by line I assume you actually mean paragraph - and use that for the filename in your SaveAs.

    That is the simple answer. Yes.

    The real answer is more questions. The first one being: how are you separating the letters? Once you do that, then again, take the text of the first "line" of that chunk, and use as the filename. For example, if you found a way to make each letter a separate Section, then it is easy.[vba]Sub EachYadda()
    Dim oSection As Section
    Dim r As range
    Dim TempDoc As Document
    Dim FirstPara As String

    For Each oSection In ActiveDocument.Sections
    Set r = oSection.Range
    r.End = r.End - 1
    Set TempDoc = Documents.Add
    With TempDoc
    .Range = r
    FirstPara = r.Paragraphs(1).Range.Text
    FirstPara = Left(FirstPara, Len(FirstPara) - 1)
    .SaveAs Filename:= FirstPara & ".doc"
    .Close
    End With
    Set r = Nothing
    Set tempDoc = Nothing
    Next
    End Sub
    [/vba]
    This is code for AFTER you have figured out how you are separating each letter.

    1. declare youir variables and objects
    For Each Section

    2. make a range object of that section
    3. make a new document
    4. make that new document equal the range object
    5. grab the text of the first paragraph
    6. strip off the paragraph mark (it is not valid in a filename)
    7. save the new document using that name (the first paragraph)
    8. clean up the document and range object

    Repeat 2 to 8 until the end of the document.

    Noet the above does NOT deal with what folder you want to save the files in...since you di dnot ask about that.
    Last edited by fumei; 06-07-2010 at 09:28 AM.

  3. #3

    thank you!!

    I had actually seperated the merged file but your solution works just fine on the original merged document. Much thanks!!!!

  4. #4
    I am also having very similar issue, I am dividing a large word document by sections and saving each section individually as a pdf file. I need to compile the name for the PDF using the first line of the section. The code I have so far is this:


    Sub BreakOnSection()
    ' Used to set criteria for moving through the document by section.
    Application.Browser.Target = wdBrowseSection

    'A mailmerge documents ends with a section break next page.
    'Subtracting one from the section count stop error message.
    For i = 1 To ((ActiveDocument.Sections.Count) - 1)

    'Select and copy the section text to the clipboard
    ActiveDocument.Bookmarks("\Section").Range.Copy

    'Create a new document to paste text from clipboard.
    Documents.Add
    Selection.Paste

    ' Removes the break that is copied at the end of the section, if any.
    Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1

    ChangeFileOpenDirectory "C:\Documents and Settings\vadim\My Documents\INVAR Technologies\Clients\Gventer"
    DocNum = DocNum + 1
    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
    FirstPara & DocNum & ".pdf", ExportFormat:= _
    wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
    wdExportOptimizeForPrint, Range:=wdExportAllDocument, FROM:=1, To:=1, _
    Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
    CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
    BitmapMissingFonts:=True, UseISO19005_1:=False

    ActiveDocument.Close SaveChanges:=False

    ' Move the selection to the next section in the document
    Application.Browser.Next
    Next i
    ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
    End Sub


    The header of each section in the big document look like this:
    "Note for Sofia Kogan on 5/10/2010"

    How can I make that into a file name? The date format will probably have to be restructured as file names in windows can't have "/".

    Any help would be greatly appreciate it.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "The header of each section in the big document look like this:
    "Note for Sofia Kogan on 5/10/2010"

    How can I make that into a file name? The date format will probably have to be restructured as file names in windows can't have "/". "

    Figure out how you what the filename to be, and change the string. As you do not give an example, I can not tell what you want.

    But the answer to: "How can I make that into a file name?"

    is: change it.

  6. #6
    This macro is for a document of 30 sections, and each section has a different date. I would like the macro to save file names as "Sofia_Kogan_Note_5_10_2010.pdf" What changes to the code need to be made to be able to do this? Do you have a sample code you can share.

    Thank you in advance,
    Vadim

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I will repeat. State - EXACTLY - what you want to happen.

    It looks like you want to change:

    "Note for Sofia Kogan on 5/10/2010"

    to

    "Sofia_Kogan_Note_5_10_2010"


    but you do NOT say so. Again, please state EXACTLY what you want to happen.

  8. #8
    Gerry, you're right I want the code to read in the first line of each section that has the line like "Note for Sofia Kogan on 5/10/2010" but with a different date for every section. Then change it to following format "Sofia_Kogan_Note_5_10_2010" and then save the file as the PDF with the newly formated file name. Here is the sample of the file i'm working with.

  9. #9
    I can't post a link but go to www . invatech . net/Kogan Sophia Chart_sample.docx there are no spaces in the file name.

  10. #10
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    I can not use the URL you posted.

    " there are no spaces in the file name."

    But your example DOES have underscores. Are you stating you want to replace all spaces with unerscores?

    I will say it a third time, and the last time. State EXACTLY what you want to happen. Take a breath, really look at what you are working with and tell me EXACTLY what you want to happen.

    BTW: it would be much easier if you do not seem to be wanting to reverse some text.

    Note for Sofia Kogan

    into

    Sofia_Kogan_Note_

    This gets complicated. Will there always be a "for" that needs to be removed? Will there always be a "Note" that needs be rearranged? Will there always be a "on" that needs to be removed?

    Exactly what you want to see happen.

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Depending on exactly what you want, it still all comes back to using the standard string manipulation functions in some way. What way exactly, depends on...you guessed it, what exactly you want to happen.

    Replace()
    Left()
    Right()
    InStr()

    and the like.

  12. #12
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Do mean like this? Click "Arranged File Name" on top toolbar.

    PART of the code is doing things like:

    Starting with a variable MyName being made to equal "Note for Sofia Kogan on 5/10/2010"...

    MyName = Replace(MyName, " ", "_")
    becomes "Note_for_Sofia_Kogan_on_5/10/2010"

    MyName = Replace(MyName, "/", "_")
    becomes "Note_for_Sofia_Kogan_on_5_10_2010"

    MyName = Replace(MyName, "on", "")
    becomes "Note_for_Sofia_Kogan__5_10_2010"
    ***note the TWO underscore characters now

    MyName = Replace(MyName, "for", "")
    becomes "Note__Sofia_Kogan__5_10_2010"
    *** another double underscore

    MyName = Replace(MyName, "__", "_")
    becomes "Note_Sofia_Kogan_5_10_2010"

    The rest of the code makes an array of all the words, resorts them to get Note after Sofia_Kogan.

    As you can see string manipulation is fully possible, it just gets complicated and requires one to be EXACT and precise in intention.

  13. #13
    Ok Garry,
    I greatly appreciate you help so far. I looked over everything you said and realized what needs to be done. Please see the link in the next post for the file in reference.

    First the date in the first line is not usable. The only thing I can use from the first line is the name of the person. I need to strip the name and save it as a variable “P_Name” Then I need to go through the text that is pasted by function Selection.Paste (please see the code above) and look for certain key words to determine what kind of content is in the section. The keywords are (not case sensitive) “Chief Complaint”, “Billing Statement”, “Mark Gventer, D.P.M., F.A.C.F.O.”, “daily signature form”, “privacy practices acknowledgement”. There will only be one key word per section. Upon finding the keyword it should be assigned to a variable “keyword”. Then the code should go through IF statement with the following rules.
    If keyword=”chief complaint”, make variable “T_Document”= chart_note
    IF keyword=”billing statemen”t, make variable “T_Document”=billing statement
    IF keyword=“Mark Gventer, D.P.M., F.A.C.F.O.”, make variable “T_Document”=prescription
    If keyword=”DAILY SIGNATURE FORM”, make variable “T_Document”=daily signature
    If keyword=“PRIVACY PRACTICES ACKNOWLEDGEMENT”, make variable “T_Document”=privacy

    Then to put together the name it should say this,
    DocNum = P_Name & T_Document

    I hope my logic is correct, if you can help me with the syntax for these two parts, I think I can handle it from there.

    I greatly appreciate it,
    Vadim

  14. #14
    File attached.

    Attachment 4052

  15. #15
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    First off...[quote]Then the code should go through IF statement with the following rules.
    If keyword=”chief complaint”, make variable “T_Document”= chart_note
    IF keyword=”billing statemen”t, make variable “T_Document”=billing statement
    IF keyword=“Mark Gventer, D.P.M., F.A.C.F.O.”, make variable “T_Document”=prescription
    If keyword=”DAILY SIGNATURE FORM”, make variable “T_Document”=daily signature
    If keyword=“PRIVACY PRACTICES ACKNOWLEDGEMENT”, make variable “T_Document”=privacy
    [/quote}No! Do not use an bu7nch of IF statements. This is precisely wehat Select Case is used for. Here is why.

    ALL your IF statements will be executed, because they are independent instructions - there is NO inherent logic linking then (at least to VBA). Here is the same result using Select Case:[vba]
    ' Then the code should go through with the following rules.
    Select Case keyword
    Case ”chief complaint”
    T_Document = "chart_note"
    Case ”billing statement"
    T_Document = "billing statement"
    Case “Mark Gventer, D.P.M., F.A.C.F.O.”
    T_Document = "prescription"
    Case ”DAILY SIGNATURE FORM”
    T_Document = "daily signature"
    Case "PRIVACY PRACTICES ACKNOWLEDGEMENT”
    T_Document = "privacy"
    End Select
    [/vba]Select Case testing multiple values for the same variable - in this case, the variable keyword (I am not happy with that name though, as it seems close to being a restricted term)

    As for: "Then I need to go through the text that is pasted by function Selection.Paste (please see the code above) "

    I do not follow. There is a Sub above (not a Function). And I am unclear as to what it is doing. Using Selection is not a good idea.

    BTW: I do not know if your posted file has macros, as I do not use 2007, and convertin git strips all code.

  16. #16
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    @ johnvins

    Your question has been moved to The Excel Help folder, in a thread named "Rename Thousands of Recipe *.txt files"

    Link:
    http://www.vbaexpress.com/forum/showthread.php?62286

    I chose the Excel Folder since renaming files can be done easily in any App, but the Excel Folder gets the most traffic here.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  17. #17
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    And I moved it to the Other Applications Help forum, since the topic has no more to do with Excel than any other Office application. I also supplied a solution.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  18. #18
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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