PDA

View Full Version : ClearFormatting command error with footnotes



seamoon
09-05-2013, 11:16 AM
The script works fine without a doc has no footnotes but when there's a footnote then debug highlights all the lines of "Selection.ClearFormatting" How to resolve it?






...
ActiveWindow.ActivePane.DisplayRulers = Not ActiveWindow.ActivePane. _
DisplayRulers
Selection.WholeStory
Selection.Font.Bold = wdToggle
Selection.Font.Bold = wdToggle
Options.DefaultHighlightColorIndex = wdNoHighlight
Selection.Range.HighlightColorIndex = wdNoHighlight
Selection.Font.Color = wdColorAutomatic
Selection.ClearFormatting
Selection.ClearFormatting
Selection.Font.Size = 12
Selection.Font.Name = "Tahoma"
ActiveWindow.View.Type = wdWebView

End With

If ActiveDocument.Footnotes.Count > 0 Then

ActiveWindow.View.SplitSpecial = wdPaneFootnotes
End If
Selection.WholeStory
If Selection.StoryType = wdFootnotesStory Then
Selection.Footnotes.Convert
Else
Selection.Endnotes.Convert
End If
Selection.WholeStory
Selection.Font.Name = "Tahoma"
Selection.Font.Size = 12
Selection.Font.Bold = wdToggle
Selection.Font.Bold = wdToggle
Selection.Font.Italic = wdToggle
Selection.Font.Italic = wdToggle
With Selection.ParagraphFormat
.RightIndent = InchesToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceAtLeast
.LineSpacing = 2
.Alignment = wdAlignParagraphLeft
.WidowControl = False
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitRightIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0

End With
Selection.WholeStory
Selection.Font.Bold = wdToggle
Selection.Font.Bold = wdToggle
Options.DefaultHighlightColorIndex = wdNoHighlight
Selection.Range.HighlightColorIndex = wdNoHighlight
Selection.Font.Color = wdColorAutomatic
Selection.ClearFormatting
Selection.ClearFormatting
Selection.Font.Size = 12
Selection.Font.Name = "Tahoma"
ActiveWindow.View.Type = wdWebView
Selection.HomeKey Unit:=wdStory

End Sub

fumei
09-05-2013, 09:40 PM
Why are you doing

Selection.ClearFormatting

so many times.

And again, you should not use Selection at all.

seamoon
09-06-2013, 04:13 AM
The recorded macro gives double Selection.ClearFormatting but even one gives error. One Selection.ClearFormatting for the body and other Selection.ClearFormatting for the endnotes. If I dont add Selection. macro gives error as well.

SamT
09-06-2013, 08:10 AM
Fumai means, why aren't you using VBA code constructs like

With WholeStory.Font
.Name = "Tahoma"
.Size = 12
.Bold = wdToggle
.Italic = wdToggle
End With

seamoon
09-06-2013, 08:39 AM
Because I don't know anything about VBA, mainly I stick the code which recorded macro. But endnotes cause Selection.ClearFormatting error. I need that command because when I copy text from Word to Frontpage without it, all text color is black on black background though text formatting set to auto.

I added your code just top of my script but macro run also gives the error for it.

SamT
09-06-2013, 02:05 PM
Hard to help you when we don't know what you are trying to accomplish. The part of the sub you shared only tells us what you physically did while the macro recorder was running.

I suspect that the problem lies in
If Selection.StoryType = wdFootnotesStory Then
Selection.Footnotes.Convert
Else
Selection.Endnotes.Convert
End If but it's hard to really tell without seeing the whole sub and knowing what you selected before recording the sub and knowing what you are trying to do.


I need that command because when I copy text from Word to Frontpage without it, all text color is black on black background though text formatting set to auto
Try running this sub on your document

Sub SamT()
With ActiveDocument.
.ForeColor.RGB = RGB(0, 0, 0)
.BackColor.RGB = RGB255, 255, 255)
End With
End Sub


I added your code just top of my script but macro run also gives the error for it.
I would hope so. It was not meant to be added to the top of your sub. It was an example in illustration of Fumei's statement.

fumei
09-06-2013, 07:11 PM
I think - not 100% sure - that this may be the issue

ActiveWindow.View.SplitSpecial

Using View is a bad way to go, as is using Selection. In fact, if you did not use Selection you will not get the ClearFormatting error. What happens if you take them ALL out

seamoon
09-07-2013, 03:50 AM
I recorded a macro (inspired by SamT's first example) that first edits the text style then apply that style as "Normal" to body text. For endnotes; "Normal" (for the font I wanted) & "Default Paragraph Font" (it unifies all styles in endnotes) style so no need to clear formatting anymore. Now I can see endnotes clearly to revise them and Frontpage paste color problem solved as well. Thanks for your help.


Selection.WholeStory
With ActiveDocument.Styles("Normal").Font
.Name = "Tahoma"
.Size = 12
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = wdColorAutomatic
.Engrave = False
.Superscript = False
.Subscript = False
.Scaling = 100
.Kerning = 0
.Animation = wdAnimationNone
End With
With ActiveDocument.Styles("Normal")
.AutomaticallyUpdate = True
.BaseStyle = ""
.NextParagraphStyle = "Normal"
End With
Selection.Style = ActiveDocument.Styles("Normal")
ActiveWindow.View.Type = wdWebView


End With




If ActiveDocument.Footnotes.Count > 0 Then

ActiveWindow.View.SplitSpecial = wdPaneFootnotes
End If
Selection.WholeStory
If Selection.StoryType = wdFootnotesStory Then
Selection.Footnotes.Convert
Else
Selection.Endnotes.Convert
End If


Selection.WholeStory
Selection.Font.Name = "Tahoma"
Selection.Font.Size = 12
Selection.Font.Bold = wdToggle
Selection.Font.Bold = wdToggle
Selection.Font.Italic = wdToggle
Selection.Font.Italic = wdToggle
Selection.Style = ActiveDocument.Styles("Normal")
Selection.Style = ActiveDocument.Styles("Default Paragraph Font")
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.RightIndent = InchesToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphLeft
.WidowControl = False
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = InchesToPoints(0)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0

End With
Selection.Font.Color = wdColorAutomatic
ActiveWindow.View.Type = wdWebView


Selection.HomeKey Unit:=wdStory

SamT
09-07-2013, 04:07 PM
Because I am not real familiar with the Word Object Model, I wrote these two subs for practice. Note that they must be placved in a Standard Module in the Document.

First I created a User Defined variable Type, (UDT,) CFCurrFont, then a Static Variable of that Type, CurrFont, to hold the settings of the Document before you get it ready for FrontPage. Note that I used the MS convention of using an all Upper Case prefix for the UDT and an all lower case prefix for it members.

Can you do me a favor and test them for me. First run the Convert4FrontPage sub and see if it works as you want, then the RestoreStyle Sub to see if the document reverts back. Except for the Paragraph style. I didn't code for that. If they work, you can make a CPCurrPara UDT and add the code your self using the model herein.

Thanks In Advance.

SamT

BTW, they are totally not tested, so...: pray2:

Option Explicit

Type CFCurrFont
cfActDocName As String
cfName As String
cfSize As Long
cfBold As Boolean
cfItalic As Boolean
cfUnderline As Long
cfUnderlineColor As Long
cfStrikeThrough As Boolean
cfDoubleStrikeThrough As Boolean
cfOutline As Boolean
cfEmboss As Boolean
cfShadow As Boolean
cfHidden As Boolean
cfSmallCaps As Boolean
cfAllCaps As Boolean
cfColor As Long
cfEngrave As Boolean
cfSuperscript As Boolean
cfSubscript As Boolean
cfScaling As Boolean
cfKerning As Boolean
cfAnimation As Long
cfBaseStyle As String
cfNextParagraphStyle As String
End Type

Static CurrFont As CFCurrFont
Static ActDocName As String
Static CurrViewState As Long


Sub Convert4FrontPage()

ActDocName = ActiveDocument.Name

With ActiveDocument.Styles("Normal")
With .Font
CurrFont.cfName = .Name
.Name = "Tahoma"
CurrFont.cfSize = .Size
.Size = 12
CurrFont.cfBold = .Bold
.Bold = False
CurrFont.cfItalic = .Italic
.Italic = False
CurrFont.cfUnderline = .Underline
.Underline = wdUnderlineNone
CurrFont.cfUnderlineColor = .UnderlineColor
.UnderlineColor = wdColorAutomatic
CurrFont.cfStrikeThrough = .StrikeThrough
.StrikeThrough = False
CurrFont.cfDoubleStrikeThrough = .DoubleStrikeThrough
.DoubleStrikeThrough = False
CurrFont.cfOutline = .Outline
.Outline = False
CurrFont.cfEmboss = .Emboss
.Emboss = False
CurrFont.cfShadow = .Shadow
.Shadow = False
CurrFont.cfHidden = .Hidden
.Hidden = False
CurrFont.cfSmallCaps = .SmallCaps
.SmallCaps = False
CurrFont.cfAllCaps = .AllCaps
.AllCaps = False
CurrFont.cfColor = .Color
.Color = wdColorAutomatic
CurrFont.cfEngrave = .Engrave
.Engrave = False
CurrFont.cfSuperSrcript = .Superscript
.Superscript = False
CurrFont.cfSubSript = .Subscript
.Subscript = False
CurrFont.scScaling = .Scaling
.Scaling = 100
CurrFont.cfKerning = .Kerning
.Kerning = 0
CurrFont.cfAnimation = .Animation
.Animation = wdAnimationNone
End With 'Font

With .ParagraphFormat
.LeftIndent = InchesToPoints(0)
.RightIndent = InchesToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphLeft
.WidowControl = False
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = InchesToPoints(0)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
End With 'ParagraphFormat

CurrFont.cfBaseStyle = .BaseStyle
.BaseStyle = ""
CurrFont.cfNextParagraphStyle = NextParagraphStyle
.NextParagraphStyle = "Normal"
.Style = .Styles("Normal")

.AutomaticallyUpdate = True

'Pick only one. Do you want FootNotes or EndNotes?
If .Footnotes.Count > 0 Then .Footnotes.Convert 'Using EndNotes
'If .Endnotes.Count > 0 Then .Endnotes.Convert 'To use FootNotes

CurrViewState = ActiveWindow.View.Type
ActiveWindow.View.Type = wdWebView
.HomeKey Unit:=wdStory

End With 'ActiveDocument
End Sub


Sub RestoreStyle()

On Error Resume Next
If Documents(ActDocName) Is Nothing Then
MsgBox ActDocName & "is not Open. This Function will remove all traces from memory and terminate!"
GoTo CleanUp
End If

With Documents(ActDocName)
With .Font
.Name = CurrFont.cfName
.Size = CurrFont.cfSize
.Bold = CurrFont.cfBold
.Italic = CurrFont.cfItalic
.Underline = CurrFont.cfUnderline
.UnderlineColor = CurrFont.cfUnderlineColor
.StrikeThrough = CurrFont.cfUnderlineColor
.DoubleStrikeThrough = CurrFont.cfDoubleStrikeThrough
.Outline = CurrFont.cfOutline
.Emboss = CurrFont.cfEmboss
.Shadow = CurrFont.cfShadow
.Hidden = CurrFont.cfHidden
.SmallCaps = CurrFont.cfSmallCaps
.AllCaps = CurrFont.cfAllCaps
.Color = wCurrFont.cfColor
.Engrave = CurrFont.cfEngrave
.Superscript = CurrFont.cfSuperSrcript
.Subscript = CurrFont.cfSubSript
.Scaling = CurrFont.scScaling
.Kerning = CurrFont.cfKerning
.Animation = CurrFont.cfAnimation
End With 'Font

.BaseStyle = CurrFont.cfBaseStyle
.NextParagraphStyle = CurrFont.cfNextParagraphStyle
.Style = .Styles("Normal")

.AutomaticallyUpdate = True
End With 'ActDocName
Exit Sub

CleanUp:
CurrFont = Nothing
ActDocName = Nothing
CurrViewState = Nothing
End Sub

fumei
09-07-2013, 04:07 PM
Whoa. It would be so much better to apply an explicit style you set up, rather than all those individual changes. But hey, glad it is working.

seamoon
09-08-2013, 01:20 PM
Sorry SamT, I have just seen your reply. For your first code, though I added Sub & End Sub, Macro gives invalid inside procedure.

For second code, I get this error: Method or data member not found.

For third code, Word gives this message: "is not Open. This Function will remove all traces from memory and terminate!" after I press okay nothing happened in my text.

I use Office 2003. And I tested your macros as separate macros (I didn't added inside my macro above)

BTW there's portable Office 2003 and the rest versions in Google if you like to test it yourself.

SamT
09-09-2013, 07:03 AM
Remove Sub and End sub from first code. It is all Module level declarations. Not a procedure (Sub or Function).

Second code can't find Module declarations because you put them inside procedure.

Third code can't find Module level variable "ActDocName" because ...inside procedure.

Why was your first thought that I forgot Sub...End Sub? :bug:

seamoon
09-09-2013, 12:01 PM
Sorry, I said I don't know anything about it. Where should I put Module level declarations?

SamT
09-09-2013, 03:12 PM
In the VB Editor (VBE) make sure that the Project Explorer window is showing. (Press Ctrl+R)

Right Click on the Project with the name of the Document, and select "Insert -> Module."

A blank Code page will appear.

Into that code page paste each of the code examples I gave in order. At the top, paste the first. Under that paste the second. and at the bottom, paste the third.

Since you have code that works, and I just did it for practice, I will understand if you don't want to have the hassle, especially because I don't know for sure that it will work the way you need.

seamoon
09-10-2013, 04:50 AM
I created it as you said, I run Convert4FrontPage macro but invalid outside procedure message for this string came: Static CurrFont As CFCurrFont

SamT
09-10-2013, 06:45 AM
Ok.

Well, I thank you for taking the time to test it for me. It was fun for me, and I hope for you too.

seamoon
09-10-2013, 07:30 AM
No problem, it's nice to see an expert using Office 2002 & Windows XP (like me) by the way.

fumei
09-10-2013, 09:06 AM
Me too (2002 on XP)