PDA

View Full Version : Saving word doc as two names/textes which are located within the word document



buhay
09-12-2010, 11:57 AM
Does anyone know a macro to automatically save the word doc so that the save file name contains two names within the word document (see example attachment).

For example the word document contains the text XYZ corporation within the textbox and ABC Fond located in the middle of the document.

Now I would like to save the doc as new file name "XYZ corporation-ABC Fond.doc".

Up until now I have only found a way to save one name of the word document using the following procedure:

Sub FindNameSaveAs()

With Selection.Find
.ClearFormatting
.Text = "XYZ corporation"
.Execute Forward:=True
End With

If Selection.Find.Found = True Then

ActiveDocument.SaveAs FileName:=PagesDoc.SaveAs FileName:="C:\My Documents\ABC Fond & ".doc "

End Sub
Does anyone knows a procedure two save the filename as XYZ corporation-ABC Fond.doc?

Any help is highly appreciated

Tinbendr
09-12-2010, 06:29 PM
If Selection.Find.Found = True Then
ActiveDocument.SaveAs FileName:="C:\My Documents\" & Selection.Text & "-ABC Fond.doc"
End If This assumes you have selected XYZ Corporation before running the macro.

If you want to save it in the same folder as the document loaded, then

If Selection.Find.Found = True Then
ActiveDocument.SaveAs FileName:=ThisDocument.Path & "\" & Selection.Text & "-ABC Fond.doc"
End If

fumei
09-13-2010, 12:32 PM
Darn those horrible textboxes. If the text was NOT in a textbox, this would be very very very easy.

I hate textboxes.

It can be done, but (as it stands) by hard-coding "ABC Funds". This seems poor to me. If the the value of a formfield, or a bookmark was used, this would make more sense. After all, if you are hard-coding "ABC Funds" why not hard-code "XYZ Corporation"?
Sub GetTextboxCrap()
Dim oShape As Shape
Set oShape = ActiveDocument.Shapes(1)
ActiveDocument.SaveAs _
FileName:=oShape.TextFrame.TextRange.Paragraphs(1) & _
"-ABC Funds.doc"
End SubThe filename is a string appending the first paragraph of the textbox ("XYZ Corporation") and "ABC Funds" and ".doc"

I am attaching a different document with the Fundsname value as a bookmark (named...Fundsname). The code below does not actually save the file (but it could easily). Click "Show Name" on the top toolbar. A messagebox displays with the possible filename. Here is the code:
Sub ShowName()
Dim oShape As Shape
Dim msg As String
Set oShape = ActiveDocument.Shapes(1)
msg = Replace(oShape.TextFrame.TextRange.Paragraphs(1) _
.Range.Text, Chr(13), "") & "-" & _
ActiveDocument.Bookmarks("Fundsname").Range.Text & _
".doc"

MsgBox "File can be saved as:" & vbCrLf & vbCrLf & msg
End Sub
It builds a string of the first paragraph in the textbox (stripping off the paragraph mark with a Replace), the text of the bookmark Fundsname, and the ".doc" extenstion.

buhay
09-13-2010, 01:33 PM
Ingore the textbox. ABC Funds is located next to the found text "Fondsname" and I am trying to save the file as "CS-ABCFunds-date" using the following procedure:

Sub SaveAsDocText()

Selection.MoveUp Unit:=wdScreen, Count:=10

With Selection.Find
.ClearFormatting
.Text = "Fondsname"
.Execute Forward:=True
End With
If Selection.Find.Found = True Then
'Selects ABC Funds
Selection.MoveRight Unit:=wdCharacter, Count:=4
Selection.EndKey Unit:=wdLine, Extend:=wdExtend

ChangeFileOpenDirectory "C:\Test"

ActiveDocument.SaveAs FileName:="CS" & "-" & Selection.Text & Format(Now, "dd-mmm-yyyy-hh-mm-ss") & ".doc"

End Sub

But the "Selection.Text" gives me the following Runtime error 5487. Word cannot complete the save due to a file permission error
(C:\\...\CS- SSGA LIQUIDITY MGMT FND CHF C2...)

What does it mean and how can I include the Selection.Text in my filename?

buhay
09-13-2010, 01:55 PM
Darn those horrible textboxes. If the text was NOT in a textbox, this would be very very very easy.

I hate textboxes.

It can be done, but (as it stands) by hard-coding "ABC Funds". This seems poor to me. If the the value of a formfield, or a bookmark was used, this would make more sense. After all, if you are hard-coding "ABC Funds" why not hard-code "XYZ Corporation"?
Sub GetTextboxCrap()
Dim oShape As Shape
Set oShape = ActiveDocument.Shapes(1)
ActiveDocument.SaveAs _
FileName:=oShape.TextFrame.TextRange.Paragraphs(1) & _
"-ABC Funds.doc"
End SubThe filename is a string appending the first paragraph of the textbox ("XYZ Corporation") and "ABC Funds" and ".doc"

I am attaching a different document with the Fundsname value as a bookmark (named...Fundsname). The code below does not actually save the file (but it could easily). Click "Show Name" on the top toolbar. A messagebox displays with the possible filename. Here is the code:
Sub ShowName()
Dim oShape As Shape
Dim msg As String
Set oShape = ActiveDocument.Shapes(1)
msg = Replace(oShape.TextFrame.TextRange.Paragraphs(1) _
.Range.Text, Chr(13), "") & "-" & _
ActiveDocument.Bookmarks("Fundsname").Range.Text & _
".doc"

MsgBox "File can be saved as:" & vbCrLf & vbCrLf & msg
End Sub
It builds a string of the first paragraph in the textbox (stripping off the paragraph mark with a Replace), the text of the bookmark Fundsname, and the ".doc" extenstion.
fumei and Tinbendr, thank you so much for all your help but for some reason I am not able to save the file with "Selection.Text" within the filename. Any idea how to solve this problem?

fumei
09-13-2010, 02:20 PM
In your case Selection.Text has a paragraph mark in it. This is not an acceptable character for a filename.

As you seem to want to persist in what you are doing, put Selection.Text into a string, and get rid of the paragraph mark. Then use that string is your filename.

buhay
09-13-2010, 02:35 PM
In your case Selection.Text has a paragraph mark in it. This is not an acceptable character for a filename.

As you seem to want to persist in what you are doing, put Selection.Text into a string, and get rid of the paragraph mark. Then use that string is your filename.

How do I do that? I mean I want to include the name "CS", the actual date and the selection text as part of the file name?

Thanks again for all your help

fumei
09-13-2010, 02:45 PM
Dim strSelection As String
strSelection = Selection.Text
strSelection = Replace(strSelection, Chr(13), "")
This puts Selection.text into a string variable, and then replaces the paragraph mark - Chr(13) - with nothing.

ABC Funds^p ^p is the symbol for a paragraph mark

becomes

ABC Funds

Now you can append your other stuff, the "CS" etc.

buhay
09-13-2010, 02:55 PM
fumei you are the bomb! It works, tons of thanks:)

buhay
10-13-2010, 10:45 AM
This solution work fine for one text located inside the document but how come I am not able to save the file name containing two different texts. I am using this code below trying to store the tow texts in two different variables:

Sub SaveAsDocText()

Dim strSelection As String
strSelection = Selection.Text
strSelection = Replace(strSelection, Chr(13), "")

With Selection.Find
.ClearFormatting
.Text = "Fondsname"
.Execute Forward:=True
End With

If Selection.Find.Found = True Then

'Selects ABC Funds

Selection.MoveRight Unit:=wdCharacter, Count:=4
Selection.EndKey Unit:=wdLine, Extend:=wdExtend

Fondsname = Selection.Text

End If

With Selection.Find
.ClearFormatting
.Text = "Depotnummer"
.Execute Forward:=True
End With

If Selection.Find.Found = True Then

'Selects ABC Funds

Selection.MoveRight Unit:=wdCharacter, Count:=4
Selection.EndKey Unit:=wdLine, Extend:=wdExtend

Depotnummer = Selection.Text

End If




ChangeFileOpenDirectory "C:\Test"

ActiveDocument.SaveAs FileName:="Fondsname" & "-" & Depotnummer & Format(Now, "dd-mmm-yyyy-hh-mm-ss") & ".doc"

End Sub

For some reason it always saves the file as

"Depotnummer-Depotnummer Date.doc" instead of "Fondsname-Depotnummer Date.doc"


Any help is highly appreciated

fumei
10-14-2010, 11:13 AM
If you are executing:
ActiveDocument.SaveAs FileName:="Fondsname" & "-" & Depotnummer & _
Format(Now, "dd-mmm-yyyy-hh-mm-ss") & ".doc"


"For some reason it always saves the file as

"Depotnummer-Depotnummer Date.doc" instead of "Fondsname-Depotnummer Date.doc""

I do not see how this is possible. If you really do have the quotation marks around "Fondsname", then this is a string literal.

What is: Fondsname = Selection.Text

about?

buhay
10-14-2010, 01:40 PM
You are right the code is supposed to be like this:

ActiveDocument.SaveAs FileName:=Fondsname & "-" & Depotnummer & _ Format(Now, "dd-mmm-yyyy-hh-mm-ss") & ".doc" I use "Fondsname = Selection.Text" to store the selected text in a variable in order to save it later as part of the file name. This trick works fine with the email body text (.body =) but I am not able to use it with the save syntax. Any idea why?

fumei
10-14-2010, 02:12 PM
Post your current code. I still do not like all the use of Selection, but there you go. Post your code.

buhay
10-14-2010, 02:33 PM
I used the code above but as I said it saves the file name as Depotnummer-Depotnummer Date.doc instead of Fondsname-Depotnummer Date.doc

I used something similiar to display textes inside the word document as part of the mailbody in an e-mail such as:

.body = "Dear ladies and gentlmen, thank you for sending mit the " & Fondsname & " and " & Depotnummer & "."

and it works fine for the mailbody.