So I have a set of code that would select a specific range of text to be hidden and I need to have a macro that would select those hidden text and unhide them. However, I don't know how to select those hidden text without first displaying them. Is there a way to select hidden text while they're not displayed. I'm trying to create a dynamic template where if certain conditions are selected only certain text will appear. So far this is my code to unhide hidden text.
Sub Macro2()
'
' Macro2 Macro
'
'
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.MoveUp Unit:=wdParagraph, Count:=4, Extend:=wdExtend
With Selection.Font
.NameFarEast = "+Body Asian"
.NameAscii = "+Body"
.NameOther = "+Body"
.Name = "+Body"
.Size = 11
.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
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 0
.Animation = wdAnimationNone
.DisableCharacterSpaceGrid = False
.EmphasisMark = wdEmphasisMarkNone
.Ligatures = wdLigaturesNone
.NumberSpacing = wdNumberSpacingDefault
.NumberForm = wdNumberFormDefault
.StylisticSet = wdStylisticSetDefault
.ContextualAlternates = 0
End With
End Sub
The hidden text has to be visible on the screen to be selected. So you need this in your code. You don't need to un-hide them;
ActiveDocument.ActiveWindow.View.ShowHiddenText = True
If you want the user to not see the texts while running this may do the job. However, it is not tested;
Application.ScreenUpdating = False
Remember to turn it back on at the end of your code;
Application.ScreenUpdating = True
Related
I'm writing a macro in Word to find an open parenthesis, select from that character to the start of the paragraph, and then reformat the selected text into small caps. Fair warning, I know nothing about VBA and I hack these macros together from examples I find online.
The last part of the macro moves the cursor to the start of the next paragraph so that I can run it again. I'd like to loop it, but I haven't figured out how to set a stopping point for the loop yet.
The following macro works, but there has to be a better way to do it. This method results in a lot of screen flicker and takes forever to run.
Sub References()
'
' References Macro
'this part selects text until it finds an open parenthesis
Dim flag As Boolean
flag = True
While flag = True
Selection.MoveRight Unit:=wdCharacter, Count:=1, _
Extend:=wdExtend
'checks the last character to see if its a open parenthesis
If Strings.Right(Selection.Range.Text, 1) = "(" Then
'if it was an open parenthesis the loop flag will end
flag = False
End If
Wend
'this part reformats the text
With Selection.Font
.Name = "Times New Roman"
.Size = 12
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.SmallCaps = True
.AllCaps = False
.Color = wdColorAutomatic
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 1
End With
'this part moves the cursor to the beginning of the next paragraph
Selection.HomeKey Unit:=wdLine
Selection.MoveDown Unit:=wdParagraph, Count:=1
End Sub
For example, using Find (which is way faster than testing every character):
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "("
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = False
End With
Do While .Find.Execute
i = i + 1
.Start = .Paragraphs.First.Range.Start
.Font.SmallCaps = True
.Start = .Paragraphs.First.Range.End
Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances processed."
End Sub
Sub MakeTextRed()
'
' MakeTextRed Macro
'
'
With Selection.Font
.Name = "+Body"
.Size = 16
.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 = wdColorRed
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = 0
.Scaling = 100
.Position = 0
.Kerning = 0
.Animation = wdAnimationNone
.SizeBi = 16
.NameBi = "+Body CS"
.BoldBi = False
.ItalicBi = False
.Ligatures = wdLigaturesNone
.NumberSpacing = wdNumberSpacingDefault
.NumberForm = wdNumberFormDefault
.StylisticSet = wdStylisticSetDefault
.ContextualAlternates = 0
End With
End Sub
Shortcut key is Shift+Alt+R
I want to make it first make text red and then if I press same shortcut again make the text black.
I do not have knowledge of VBA. Please provide logic or algorithm.
With Selection.Font
If .Color = wdColorRed then
.Color = wdColorBlack
else
.Color = wdColorRed
end if
End With
Sub toggleColor()
With Selection.Font
.Color = -wdColorRed * (.Color <> wdColorRed)
End With
End Sub
If color is not red then (.Color <> wdColorRed) = -1 (True as integer) and result = wdColorRed. Otherwise (.Color <> wdColorRed) = 0 (False as integer) and result = 0 (wdColorBlack)
In word, I have been creating a macro that formats an existing table. The dimension of the table can be anything. The formatting looks like this:
My VBA code in word looks like this:
Sub Makro7()
'
' Makro7 Makro
'
'
Selection.HomeKey Unit:=wdRow
Selection.HomeKey Unit:=wdColumn
Selection.Range.Relocate wdRelocateDown
Selection.Range.Relocate wdRelocateUp
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = 192
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.EndKey Unit:=wdColumn, Extend:=True
With Selection.ParagraphFormat
.LeftIndent = CentimetersToPoints(0)
.RightIndent = CentimetersToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.WidowControl = True
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = CentimetersToPoints(0)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
.MirrorIndents = False
.TextboxTightWrap = wdTightNone
.CollapsedByDefault = False
End With
End Sub
Now, I need this function in Powerpoint too. Unfortunately I can't just copy this code to Powerpoint. I'm not that familiar with VBA in Powerpoint, and don't know how to proceed. I get an error if I use this code within Powerpoint.
I have the following code to create a button.
ActiveSheet.Buttons.Add(264, 230.25, 127.5, 11.25).Select
Selection.OnAction = "choose_worksheet_1"
Range("C16").Select
ActiveSheet.Buttons.Add(264, 230.25, 127.5, 11.25).Select
Selection.Characters.Text = "Next step (2)"
With Selection.Characters(Start:=1, Length:=13).Font
.Name = "Tahoma"
.FontStyle = "Standaard"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
'.ThemeColor = 2
'.TintAndShade = 0
'.ThemeFont = xlThemeFontNone
End With
This all works but the thing is that I would like to add a macro to it straight away. Could anybody tell me how I can a macro to the button, for example "Macro2"
Is it possible to access this option in VSTO:
Right click on paragraph -> Font -> Advanced Tab -> Spacing Option
and change it?
I believe so. These are the different parameters with which you can interact.
Sub Sample()
With Selection.Font
.Name = "+Body"
.Size = 11
.Bold = False
.Italic = False
.Underline = 0 ' wdUnderlineNone
.UnderlineColor = -16777216 'wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = -16777216
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = -1.1
.Scaling = 150
.Position = 4
.Kerning = 0
.Animation = 0 'wdAnimationNone
End With
End Sub