View Single Post
 
Old 08-19-2021, 06:51 PM
Peterson Peterson is offline Windows 10 Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 141
Peterson is on a distinguished road
Default

Thanks for taking the time to provide some code. Unfortunately, I actually need to leave the cap'd text as-is (and, in fact, it's not always going to be all cap'd).

What I need to do is take the text and do stuff with it elsewhere: populate document properties and create a list in another document. It needs to be camel case per our style guide and bookmarking requirements (as the doc properties will import into PDFs doc properties).

Here's the relevant code. My struggle is figuring out how to get an all-cap'd string to another macro or function and converted to camel case.
Code:
Sub LoopAllWordFilesInFolder()

    Dim docDocument As Document
    Dim strPath As String
    Dim strFile As String
    Dim strExtension As String
    Dim FolderPicker As FileDialog

    Application.ScreenUpdating = False

    ' Get target folder path from user:
    Set FolderPicker = Application.FileDialog(msoFileDialogFolderPicker)
        With FolderPicker
            .Title = "Choose the folder containing your files"
            .AllowMultiSelect = False
                If .Show <> -1 Then GoTo NextCode
                strPath = .SelectedItems(1) & "\"
        End With

' In case the user cancels:
NextCode:
    strPath = strPath
    If strPath = "" Then GoTo ResetSettings
  
    ' To run the macro on .DOCX files only, change "*.doc*" to "*.docx"
    ' To run the macro on .DOC files only, change "*.doc*" to "*.doc"
    strExtension = "*.doc*"

    ' Target path with file extension:
    strFile = Dir(strPath & strExtension)
    
    ' Loop through each file in the folder:
    Do While strFile <> ""
        ' Set variable equal to opened document:
        Set docDocument = Documents.Open(FileName:=strPath & strFile)

        ' Use DoEvents to ensure document has opened before moving on to next line of code.
        DoEvents

' CALL OTHER MACROS HERE:
                                  
            Call Specs_GetNameAndNumber

        ' Save and close document:
        If docDocument.Saved = False Then docDocument.Save
        docDocument.Close
        ' Ensure document has closed before moving on to next line of code:
        DoEvents
        ' Get next file name:
        strFile = Dir
    Loop

    MsgBox "The macro has looped through the files in the folder you chose and completed the tasks."

' Reset macro optimization settings:
ResetSettings:
    Application.ScreenUpdating = True
End Sub

Sub Specs_GetNameAndNumber() ' 08/19/2021

' This macro speeds up PDFing spec packages by setting the Author and Title
' properties in Word so that they don't have to be manually set in Acrobat,
' and it creates a rough ToC (using the spec template), if needed.

' The macro does the following:
' 1. Gets spec section number, presumed to be the first or second line of a
'    document; however, the macro will loop through the initial paragraphs and
'    skip blank ones (doesn't matter if name/numbers are in content controls)
' 2. Gets spec section name, presumed to be after section number; again,
'    the macro skips blank lines
' 3. Removes hard and soft returns and tabs that end up in the the name/number
'    variables and trims spaces from the beginning/end of each
' 4. Concatenates the two lines into a single string, i.e., the title
' 5. Changes the string to camel case, if necessary, per in-house style and
'    bookmarking reqs.
' 6. Populates file document properties:
'     - Title
'     - Author (set to "Company Name")
' 7. If needed, creates a rough ToC based on spec template and applies formatting

    Dim strSectionNumber As String
    Dim strSectionName As String
    Dim strTitle As String
    
    Dim i As Long ' Counter to loop through  first paragraphs in the section
    Dim k As Long ' To remove hard and soft returns and tabs from strings
    
    ' Loop through the first paragraphs until the section name -- the second
    ' variable -- is assigned:
    i = 1
    
    Do Until strSectionName <> ""
        ' If the paragraph is empty (i.e., has a char. length of 1 [which is for
        ' the pilchrow]), then skip the paragraph:
        If ActiveDocument.Paragraphs(i).Range.Characters.Count = 1 Then
            i = i + 1
        ' Otherwise, if section number or name variable hasn't been assigned, do it:
        Else
            If strSectionNumber = "" Then
                strSectionNumber = ActiveDocument.Paragraphs(i).Range.Text
                ' Increment counter to next paragraph:
                i = i + 1
            ' If the section number variable has been assigned, then assign the
            ' section name variable:
            ElseIf strSectionName = "" Then
                strSectionName = ActiveDocument.Paragraphs(i).Range.Text
                ' Debug.Print strSectionName
            End If
        End If
    Loop
    
    ' Clean up and concatenate the strings to create the title:
    ' a. Remove tabs, paragraph marks, and soft returns, which are
    '    Chr(9), Chr(11), and Chr(13), respectively:
        For k = 9 To 13 Step 2
            strSectionNumber = Replace(strSectionNumber, Chr(k), "")
            strSectionName = Replace(strSectionName, Chr(k), "")
        Next k

    ' b. Trim spaces from both ends of the strings:
        strSectionNumber = Trim(strSectionNumber)
        strSectionName = Trim(strSectionName)
        
    ' c. Concatenate the number and name and assign to a variable:
        strTitle = strSectionNumber & " - " & strSectionName
        Debug.Print strTitle
                
    ' Change to camel case:
    
        ' ?????????????????
                
    ' Populate the file's document properties:
    Call DocProperties_SetInFile(strTitle)

    ' Create rough ToC (pending):
    ''Call ExtractListOfSectionsToNewDoc
 End Sub

Thank you
Reply With Quote