Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filename and Saving 2

Status
Not open for further replies.

moontho

Technical User
Jun 4, 2003
85
GB
I'm wanting to save a document using VBA which uses whatever text is styled as "Heading 1". Does anyone know if this is possible?

Thanks

 
This might not be very elegant, but it does work :-

'--------------------------------------------------
Sub TEST()
Dim MyName As String
With ActiveDocument.Content.Find
.Forward = True
.ClearFormatting
.Style = wdStyleHeading1
.Execute
With .Parent
.Select
.StartOf Extend:=wdExtend
End With
MyName = Selection.Text
MyName = Left(MyName, Len(MyName) - 1) & ".doc"
ActiveDocument.SaveAs FileName:=MyName
End With
End Sub
'-------------------------------------------


Regards
BrianB
Use CupOfCoffee to speed up all windows applications
================================
 
Hi moontho,

Something like this ..

Code:
[blue]Selection.HomeKey Unit:=wdStory

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 1")

With Selection.Find
    .Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
End With

Selection.Find.Execute

ActiveDocument.SaveAs Left(Selection.Text, Len(Selection.Text) - 1)[/blue]

.. should find the first paragraph with style "Heading 1" and use it (without the trailing paragraph mark) as the name to SaveAs.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Thanks guys, but both of the above got stuck on the save as. It came up with a "Compile Error" "Can't find project or Library". I hope i'm not missing something obvious!
 
Hi moontho,

Are you running this from Word? SaveAs is part of Word. As Find is also part of Word, I don't quite see what could be wrong, but check out your References (Tools > References in the VBE Menu) and see if anything is marked as MISSING.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
I am curious about what you are trying to do. Tony's code will save the file (using SaveAs) with ther first Heading1 styled text. Your problem is likely, as Tony mentioned, a missing reference.

But what if you have more than one Heading1 piece of text? Or is this never going to happen? Will you only ever want the file saved as the first Heading1 text?

Gerry
 
Finally got it going guys. There was a temp file that was missing in the references. Big thanks to both of you. A star each.

We have 70-80 documents all based from one template that are created monthly, and rather than have a bit of code in each individual document, I just want one bit of code that needn't ever change sat in the Normal.dot.

many thanks

Thom
 
One last thing!!! There are two types of heading that can appear at the top of the document "G Heading 1" or "B Heading 1" Though not being too hot at the moment with VBA (learning), I know that I probably need to use If..Then..Else, but i'm not sure how to code it in this instance.

Any advice would be greatly appreciated
 
Hi moontho,

Don't know what you want to do with it, but after the Find.Execute you can do ..

Code:
[blue]If Left(Selection.Text,1) = "G" Then
    [green]' Do stuff with "G" Heading[/green]
Else
    [green]' Do stuff with "B" Heading[/green]
End If[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top