PDA

View Full Version : saving word file based on first line of text



mr, whipple
06-07-2010, 06:27 AM
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?

fumei
06-07-2010, 09:11 AM
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.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

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.

mr, whipple
06-07-2010, 10:18 AM
I had actually seperated the merged file but your solution works just fine on the original merged document. Much thanks!!!!:rotlaugh: :rotlaugh: :friends:

Vadim S.
06-30-2010, 10:22 AM
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.

fumei
06-30-2010, 10:50 AM
"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.

Vadim S.
06-30-2010, 10:55 AM
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

fumei
06-30-2010, 11:21 AM
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.

Vadim S.
06-30-2010, 11:38 AM
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.

Vadim S.
06-30-2010, 11:43 AM
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.

fumei
06-30-2010, 11:52 AM
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.

fumei
06-30-2010, 12:01 PM
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.

fumei
06-30-2010, 12:20 PM
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.

Vadim S.
07-08-2010, 07:27 AM
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

Vadim S.
07-08-2010, 07:29 AM
File attached.

4052

fumei
07-12-2010, 09:21 AM
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:
' 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
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.

SamT
03-17-2018, 01:56 PM
@ 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.

macropod
03-18-2018, 02:13 PM
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.

SamT
03-18-2018, 05:01 PM
:super::yay