View Single Post
 
Old 11-20-2012, 10:53 AM
gmaxey gmaxey is offline Windows 7 32bit Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Couple of other things you might consider.

1) Use calls to functions to do standard things like validate a unique title:

HTML Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim strTitle As String
strTitle = "Name"
'Is this a unique titled?  Check with function
If fcn_IsTitleUnique(strTitle) Then
  Debug.Print "Yep it is."
Else
  Debug.Print "Nope its not."
End If
End Sub
Function fcn_IsTitleUnique(ByRef strTitlePassed As String) As Boolean
Dim oCol As New Collection
Dim oCC As ContentControl
fcn_IsTitleUnique = True
For Each oCC In ActiveDocument.ContentControls
  oCol.Add oCC.Title, oCC.Title
Next oCC
  On Error Resume Next
  oCol.Add strTitlePassed, strTitlePassed
  If Err.Number = 0 Then fcn_IsTitleUnique = True
  On Error GoTo 0
lbl_Exit:
  Exit Function
End Function
2) You can simplify InputBox code using StrPtr. This lets you determine between user pressing "Cancel" or not making an entry:

HTML Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim strDemo As String
Dim bVillageIdiotNeedsHelp As Boolean
bVillageIdiotNeedsHelp = False
Do
  If Not bVillageIdiotNeedsHelp Then
    strDemo = InputBox("Enter your name")
    bVillageIdiotNeedsHelp = True
  Else
    strDemo = InputBox("You have an name even if it is Village Idiot." & vbCr + vbCr _
                      & "Please enter your name!")
  End If
  'User cancels.
  If StrPtr(strDemo) = 0 Then GoTo lbl_Exit
Loop Until strDemo <> ""
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote