PDA

View Full Version : Use VBA to save a document including a word from within the file in the filename



Phil Smith
06-06-2018, 03:16 AM
Hi everyone. I have a document which I have split into single page documents. Hoever, when each single page document is saved, I would like to save the document as "Diagram xxx". The format of the document means that the xxx is always the 6th word and is as follows: Transport Company Anytown LTP MB xxx The space between LTP and MB is one Tab as is the space between MB and xxx. Thanks for in advance.

gmayor
06-06-2018, 04:26 AM
Without knowing how the pages are separated it is difficult to produce a loop that will loop through the pages. However given your description the folllowing will find the wanted xxx strings in the document, which should help you on your way.

If this is the result of a mail merge you might find http://www.gmayor.com/MergeAndSplit.htm (http://www.gmayor.com/MergeAndSplit.htm) useful


Dim oRng As Range
Dim sName As String
Set oRng = ActiveDocument.Range
With oRng.Find
Do While .Execute(FindText:="Transport Company Anytown LTP" & Chr(9) & "MB" & Chr(9))
sName = "Diagram "
oRng.Collapse 0
oRng.MoveEnd wdWord
sName = sName & oRng.Text
MsgBox sName
oRng.Collapse 0
Loop
End With