Que tal algumas funções para utilizar o VBA no MS Word?
Funções CHR (Character):Function FilePathExists (strPickedPath) As BooleanSet objFSO = CreateObject("scripting.FileSystemObject")If objFSO.FolderExists(strPickedPath) ThenLet FilePathExists = TrueElseLet FilePathExists = FalseEnd IfEnd SubSub ShowProgress (ByVal snglFileCounter, ByVal snglCount, ByVal strFolderPath As String)Dim snglDecimal As SingleDim snglWidth As SingleDim strLabelText As StringIf BoolCancel Then Exit SubLet snglDecimal = snglFileCounter / snglCountLet snglWidth = snglDecimal * 280Let strLabelText = TruncPathForLabel(strFolderPath)Let frmProgress.lblPercent.Caption = "Folder scan is " & FormatPercent(snglDecimal) & " complete."Let frmProgress.lblStatus.Caption = snglCount & " files in " & strLabelTextLet frmProgress.lblProgress.Width = snglWidthfrmProgress.RepaintEnd SubSub MakeTable()Dim MyTableRangeDim ActiveIf BoolCancel Then Exit SubSet Active = ActiveDocument
If ActiveDocument.Tables.Count = 0 ThenSet MyTableRange = Active.Tables.Add(Range:=Active.Range(Start:=0,End:=0), NumRows:=1, NumColumns:=3)AddMacroButton 1, "MACROBUTTON TableSortAToZ Description "AddMacroButton 2, "MACROBUTTON TableSortAToZ File Path/Name "AddMacroButton 3, "MACROBUTTON TableSortAToZ File Type "With ActiveDocument.Tables(1).Columns(1).SetWidth ColumnWidth:=InchesToPoints(2.3),RulerStyle:=wdAdjustProportional.Columns(2).SetWidth ColumnWidth:=InchesToPoints(4.5),RulerStyle:=wdAdjustProportional.Columns(3).SetWidth ColumnWidth:=InchesToPoints(0.7),RulerStyle:=wdAdjustProportionalEnd WithLet lngTableRows = 1End IfLet BoolTableMade = TrueEnd SubSub AddMacroButton (ByVal lngCellNumber As Long, ByVal strMacroButton As String)Dim CellRange As RangeIf BoolCancel Then Exit SubSet CellRange = ActiveDocument.Tables(1).Rows(1).Cells(lngCellNumber).RangeCellRange.SelectCellRange.DeleteSelection.Fields.Add Range:=CellRange, Type:=wdFieldEmpty,text:=strMacroButton, preserveformatting:=FalseEnd Sub
Sub pMacroClickOptions()If BoolCancel Then Exit SubIf frmOptions.chkSort.Value = True ThenWith OptionsLet .ButtonFieldClicks = 1End WithElseWith OptionsLet .ButtonFieldClicks = 2End WithEnd IfEnd SubFunction TruncPathForLabel (strText)Dim intLen As IntegerDim intMarkLeft As IntegerDim intMarkRight As IntegerDim strLeft As StringDim strConj As StringDim strRight As StringDim strLabelText As StringIf BoolCancel Then Exit FunctionLet intLen = Len(strText)If intLen > 60 ThenLet intMarkLeft = InStr(15, strText, "\")Let intMarkRight = InStrRev(strText, "\", -1)Let strLeft = Left(strText, intMarkLeft)Let strConj = "..."Let strRight = "\" & Right(strText, intLen - intMarkRight)Let strLabelText = strLeft & strConj & strRightLet TruncPathForLabel = strLabelTextElseLet TruncPathForLabel = strTextEnd IfEnd Sub
Sub sOpenBrowser (FileName)Dim Dummy As StringDim RetVal As LongDim hwndLet RetVal = ShellExecute(hwnd, "open", FileName, "", Dummy,SW_SHOWNORMAL)End SubSub BuildCustomMenu()Dim vCtrlCount As LongDim ctlControlOn Error Resume NextApplication.CommandBars("Menu Bar").Controls("docsonline").DeleteOn Error GoTo 0Let vCtrlCount = CommandBars("MenuBar").Controls.CountLet vCtrlCount = vCtrlCount + 1With CommandBars("MenuBar").Controls.Add(Type:=msoControlPopup, Before:=vCtrlCount).Caption ="&docsonline"End With
'Make the new menu start the groupWith CommandBars("MenuBar").Controls("docsonline")Let .BeginGroup = TrueEnd WithSet ctlControl = CommandBars("MenuBar").Controls("docsonline").Controls.Add(msoControlButton)Let ctlControl.Caption = "&New Catalog"Let ctlControl.Style = msoButtonCaption ''' Display only the caption text."Let ctlControl.OnAction = "NewCatalog"Set ctlControl = CommandBars("MenuBar").Controls("docsonline").Controls.Add(msoControlButton)Let ctlControl.Caption = "&About docsonline"Let ctlControl.Style = msoButtonCaption ''' Display only the caption text."Let ctlControl.OnAction = "Aboutdocsonline"Let ActiveDocument.Saved = TrueEnd SubSub NewCatalog()Application.Documents.Add 'Template:=Templates(1).FullName, Visible:=TruefrmOptions.ShowEnd SubSub SetTable()ActiveDocument.Tables(1).SelectLet Selection.Font.Size = 9ActiveDocument.Tables(1).AutoFitBehavior wdAutoFitWindowSelection.MoveUp Unit:=wdParagraph, Count:=1, Extend:=FalseLet ActiveDocument.SpellingChecked = TrueEnd Sub
Sub CheckTableContents()If ActiveDocument.Tables(1).Rows.Count = 1 ThenActiveDocument.Tables(1).DeleteSelection.InsertAfter "There were no files or subfolders in the selected path."End IfEnd SubSub DoTheseThings(strFile)Debug.Print strFileEnd SubSub DeletaTachado()Dim oTexto As Word.RangeSet oTexto = ActiveDocument.RangeWith oTexto.Find.ClearFormatting.Replacement.ClearFormattingLet .Text = "*"Let .MatchWildcards = TrueLet .Font.StrikeThrough = TrueLet .Replacement.Text = "".Execute Replace:=wdReplaceAllEnd WithEnd Sub
Aqui estão alguns dos mais comuns código ASCII utilizados:
Chr(9) = tabChr(11) = manual line break (shift-enter)
Chr(12) = manual page breakChr(13) = vbCrLf (return)Chr(14) = column break
Chr(30) = non-breaking hyphenChr(31) = optional hyphenChr(32) = spaceChr(34) = quotation mark
Chr(160) = nonbreaking space
Sub Table_To_Text()
'October 5, 2008 by Derrick Duehren'Converts the current table to regular text.'Handy when forwarding emails with annoying nested tables of content.
On Error GoTo TheEndSelection.Rows.ConvertToText Separator:=wdSeparateByParagraphs, _NestedTables:=TrueSelection.Find.ClearFormattingSelection.Find.Replacement.ClearFormattingWith Selection.FindLet .Text = "^l"Let .Replacement.Text = "^p"Let .Forward = TrueLet .Wrap = wdFindAskLet .Format = FalseEnd WithSelection.Find.Execute Replace:=wdReplaceAllSelection.HomeKey Unit:=wdLineTheEnd:
End Sub
Esta função formata o texto selecionado com a fonte Veranda, com o tamanho 10, e como azul escuro. Pode utilizar dentro do Outlook, para pré formatar algum texto que for colado nele.
Sub FormatVeranda()
' Macro created August 18, 2008 by Derrick Duehren' Formats the selected text as Veranda font, size 10, dark blue.On Error GoTo EHSelection.Font.Name = "Verdana"Selection.Font.Size = 10Selection.Font.Color = wdColorDarkBlueExit SubEH:If Err.Number = 4605 Thenmsgbox "You need to switch to HTML format for this email."End If
End Sub
Sub NoIndent_Bullet_List()
'Turn the selected text into a bullet list with no indent.With ListGalleries(wdBulletGallery).ListTemplates(1).ListLevels(1)Let .NumberFormat = ChrW(61623)Let .TrailingCharacter = wdTrailingTabLet .NumberStyle = wdListNumberStyleBulletLet .NumberPosition = InchesToPoints(0.25)Let .Alignment = wdListLevelAlignLeftLet .TextPosition = InchesToPoints(0.5)Let .TabPosition = InchesToPoints(0.5)Let .ResetOnHigher = 0Let .StartAt = 1
With .FontLet .Bold = wdUndefinedLet .Italic = wdUndefinedLet .StrikeThrough = wdUndefinedLet .Subscript = wdUndefinedLet .Superscript = wdUndefinedLet .Shadow = wdUndefinedLet .Outline = wdUndefinedLet .Emboss = wdUndefinedLet .Engrave = wdUndefinedLet .AllCaps = wdUndefinedLet .Hidden = wdUndefinedLet .Underline = wdUndefinedLet .Color = wdUndefinedLet .Size = wdUndefinedLet .Animation = wdUndefinedLet .DoubleStrikeThrough = wdUndefinedLet .Name = "Symbol"End WithLet .LinkedStyle = ""End WithLet ListGalleries(wdBulletGallery).ListTemplates(1).Name = ""Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries( _wdBulletGallery).ListTemplates(1), ContinuePreviousList:=False, ApplyTo:= _wdListApplyToWholeList, DefaultListBehavior:=wdWord10ListBehaviorWith Selection.ParagraphFormatLet .LeftIndent = InchesToPoints(0.25)Let .RightIndent = InchesToPoints(0)Let .SpaceBefore = 0Let .SpaceBeforeAuto = FalseLet .SpaceAfter = 0Let .SpaceAfterAuto = FalseLet .LineSpacingRule = wdLineSpaceSingleLet .Alignment = wdAlignParagraphLeftLet .WidowControl = TrueLet .KeepWithNext = FalseLet .KeepTogether = FalseLet .PageBreakBefore = FalseLet .NoLineNumber = FalseLet .Hyphenation = TrueLet .FirstLineIndent = InchesToPoints(-0.25)Let .OutlineLevel = wdOutlineLevelBodyTextLet .CharacterUnitLeftIndent = 0Let .CharacterUnitRightIndent = 0Let .CharacterUnitFirstLineIndent = 0Let .LineUnitBefore = 0Let .LineUnitAfter = 0End WithSelection.ParagraphFormat.TabStops.ClearAllLet ActiveDocument.DefaultTabStop = InchesToPoints(0.5)Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(0.25), _Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpaces
Numbered List with No IndentEnd Sub
This Word function turns the selected text into a numbered list with no indent.
Sub NoIndent_Numbers()
' Macro recorded May 6, 2009 by Derrick Duehren'Turn the selected text into a bullet list with no indent.With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1).NumberFormat = "%1.".TrailingCharacter = wdTrailingTab.NumberStyle = wdListNumberStyleArabic.NumberPosition = InchesToPoints(0.25).Alignment = wdListLevelAlignLeft.TextPosition = InchesToPoints(0.5).TabPosition = InchesToPoints(0.5).ResetOnHigher = 0.StartAt = 1With .Font.Bold = wdUndefined.Italic = wdUndefined.StrikeThrough = wdUndefined.Subscript = wdUndefined.Superscript = wdUndefined.Shadow = wdUndefined.Outline = wdUndefined.Emboss = wdUndefined.Engrave = wdUndefined.AllCaps = wdUndefined.Hidden = wdUndefined.Underline = wdUndefined.Color = wdUndefined.Size = wdUndefined.Animation = wdUndefined.DoubleStrikeThrough = wdUndefined.Name = ""End With.LinkedStyle = ""End WithListGalleries(wdNumberGallery).ListTemplates(1).Name = ""Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries( _wdNumberGallery).ListTemplates(1), ContinuePreviousList:=True, ApplyTo:= _wdListApplyToWholeList, DefaultListBehavior:=wdWord10ListBehaviorWith Selection.ParagraphFormat.LeftIndent = InchesToPoints(0.25).RightIndent = InchesToPoints(0).SpaceBefore = 0.SpaceBeforeAuto = False.SpaceAfter = 0.SpaceAfterAuto = False.LineSpacingRule = wdLineSpaceSingle.Alignment = wdAlignParagraphLeft.WidowControl = True.KeepWithNext = False.KeepTogether = False.PageBreakBefore = False.NoLineNumber = False.Hyphenation = True.FirstLineIndent = InchesToPoints(-0.25).OutlineLevel = wdOutlineLevelBodyText.CharacterUnitLeftIndent = 0.CharacterUnitRightIndent = 0.CharacterUnitFirstLineIndent = 0.LineUnitBefore = 0.LineUnitAfter = 0End WithSelection.ParagraphFormat.TabStops.ClearAllActiveDocument.DefaultTabStop = InchesToPoints(0.5)Selection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(0.25), _Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpacesSelection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(0.5), _Alignment:=wdAlignTabList
End Sub
Reference: Derrick Duehren
Tags: Bernardes, Windows, MS, Office, Word, FilePathExists, ShowProgress, MakeTable, AddMacroButton, TruncPathForLabel, sOpenBrowser, BuildCustomMenu, NewCatalog, SetTable, CheckTableContents, DoTheseThings, DeletaTachado, Table_To_Text, FormatVeranda, NoIndent_Bullet_List, NoIndent_Numbers
Nenhum comentário:
Postar um comentário