I am trying to create separate documents for each country I will send a questionnaire to, and I am struggling. Basically, I have a master document with this questionnaire:
ASK CANADA, GERMANY, ITALY
How old are you?
ASK CANADA
Canada
ASK GERMANY
Germany
ASK ITALY
Italy
Only difference is that instead of bold I am using orange (RGB: 237, 125, 49) for the logic of the questionnaire.
What I would like to obtain is a document named "questionnaire_CANADA" containing the following, with no format changes (i.e., keeping the orange, all caps etc.):
ASK CANADA, GERMANY, ITALY
How old are you?
ASK CANADA
Canada
Same for the other countries (there are more than these 3 in the original questionnaire).
Is something like this even possible with a macro? Below my (sloppy) attempt - with this I only get the three documents correctly named but empty.
Sub CreateCountryQuestionnaires()
Dim sourceDoc As Document
Dim countryDoc As Document
Dim para As Paragraph
Dim countryList As Object
Dim country As Variant
Dim currentLine As String
Dim i As Integer
Dim orangeColor As Long
Const savePath As String = "PATH"
' Set the orange color (RGB: 237, 125, 49)
orangeColor = RGB(237, 125, 49)
' Initialize
Set sourceDoc = ActiveDocument
Set countryList = CreateObject("Scripting.Dictionary")
Application.ScreenUpdating = False
' First pass: Find all countries
For Each para In sourceDoc.Paragraphs
If para.Range.Font.Color = orangeColor Then
currentLine = Trim(para.Range.Text)
If UCase(Left(currentLine, 4)) = "ASK " Then
' Extract countries from the ASK line
Dim countries() As String
countries = Split(Mid(currentLine, 5), ",")
For i = 0 To UBound(countries)
Dim trimmedCountry As String
trimmedCountry = Trim(countries(i))
If Len(trimmedCountry) > 0 Then
countryList(trimmedCountry) = 1 ' Track country
End If
Next i
End If
End If
Next para
' Second pass: Create documents for each country
For Each country In countryList.Keys()
' Create new document
Set countryDoc = Documents.Add
' Process paragraphs in order
Set para = sourceDoc.Paragraphs.First
Do While Not para Is Nothing
If para.Range.Font.Color = orangeColor Then
currentLine = Trim(para.Range.Text)
If UCase(Left(currentLine, 4)) = "ASK " Then
' Check if this ASK line includes our country
Dim askCountries() As String
askCountries = Split(Mid(currentLine, 5), ",")
Dim includeThis As Boolean
includeThis = False
For i = 0 To UBound(askCountries)
If Trim(askCountries(i)) = country Then
includeThis = True
Exit For
End If
Next i
If includeThis Then
' Copy ASK line with formatting
para.Range.Copy
countryDoc.Content.Paste
' Copy the next paragraph if it's a question
Dim nextPara As Paragraph
Set nextPara = para.Next
If Not nextPara Is Nothing Then
If nextPara.Range.Font.Color <> orangeColor Then
countryDoc.Content.InsertAfter vbCrLf
nextPara.Range.Copy
countryDoc.Content.Paste
countryDoc.Content.InsertAfter vbCrLf & vbCrLf
End If
End If
End If
End If
End If
Set para = para.Next
Loop
' Remove empty paragraphs at end
Do While countryDoc.Paragraphs.Count > 0 And _
Len(Trim(countryDoc.Paragraphs.Last.Range.Text)) = 0
countryDoc.Paragraphs.Last.Range.Delete
Loop
' Save the document
Dim safeCountry As String
safeCountry = Replace(Replace(Replace(country, "\", ""), "/", ""), ":", "")
countryDoc.SaveAs2 FileName:=savePath & "questionnaire_" & safeCountry & ".docx", _
FileFormat:=wdFormatXMLDocument
countryDoc.Close
Next country
Application.ScreenUpdating = True
MsgBox "Successfully created questionnaires for " & countryList.Count & " countries.", vbInformation
End Sub
Thanks in advance for any help!