Inconsistency Setting Word Margins/Column Spacing via Excel VBA - excel

Good Morning!
I am creating a tool to format many word documents with the same settings, and am working on setting all document body data to the column count specified, and the margins as specified. When run, this code works, but does not set the left/ right margins appropriately. The code should set each of them to the same value.
When run it seems to be variable. For instance, if I choose 0.3; the left margin will end of as 0.2 and the right will be 0.4. To make it a little weirder, if I manually go into the custom margin settings in MS Word, it states it is at the appropriate numbers (0.3) even when the margin bar on the page is not set there.
Has anyone worked with margins via vb to know if this is a setting issue, or if there is a more accurate way? I think it may have something to do with the column spacing .SpaceAfter = InchesToPoints(frmWordEdit.txtColumnSpacing), but I am unsure.
Any help is appreciated!
Sub AddRemoveWatermark(strReplaceText As String)
'Word Variables
Dim wrdApplication As Word.Application
Dim wrdDocument As Word.Document
Dim wrdSection As Word.section
Dim wrdSelection As Word.Selection
Dim wrdHeader As Word.HeaderFooter
Dim rngHeader As Word.Range
Dim rngFooter As Word.HeaderFooter
Dim spShape As Word.Shape
Dim strDocumentName As String
Dim strPath As String
Dim strBBPath As String
Dim lngCount As Long
' Open the file dialog
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = True
.Show
Set wrdApplication = New Word.Application
' Display paths of each file selected
For lngCount = 1 To .SelectedItems.Count
strPath = .SelectedItems(lngCount)
Set wrdDocument = wrdApplication.Documents.Open(strPath)
strDocumentName = wrdDocument.FullName 'Record the document name
wrdApplication.Templates.LoadBuildingBlocks
wrdApplication.Visible = True
'Document Layout
If frmWordEdit.chkDocumentLayout.Value = True Then
'Change Columns
If frmWordEdit.chkColumns = True Then
With wrdDocument.PageSetup.TextColumns
.SetCount NumColumns:=frmWordEdit.txtColumns
'.Add EvenlySpaced:=True
'.Width = InchesToPoints(3)
'.SpaceAfter = InchesToPoints(0.3)
End With
Dim i As Integer
If frmWordEdit.txtColumns > 1 Then
For i = 1 To frmWordEdit.txtColumns - 1
With wrdDocument.PageSetup.TextColumns(i)
'.Width = InchesToPoints(4)
.SpaceAfter = InchesToPoints(frmWordEdit.txtColumnSpacing)
End With
Next
End If
End If
'Change Margins
If frmWordEdit.chkMargins = True Then
With wrdDocument.PageSetup
.LeftMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMLeftRight)
.RightMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMLeftRight)
.TopMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMTop)
.BottomMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMBottom)
End With
End If
End If
'Document Design
If frmWordEdit.chkDocumentDesign.Value = True Then
If frmWordEdit.chkMHeader = True Then
With wrdDocument.PageSetup
.HeaderDistance = wrdApplication.InchesToPoints(frmWordEdit.txtMHeader)
End With
End If
If frmWordEdit.chkMFooter = True Then
With wrdDocument.PageSetup
.FooterDistance = wrdApplication.InchesToPoints(frmWordEdit.txtMFooter)
End With
End If
End If
End Sub

This is the part of your code which sets the margins.
With wrdDocument.PageSetup
.LeftMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMLeftRight)
.RightMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMLeftRight)
.TopMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMTop)
.BottomMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMBottom)
End With
I find no fault with the syntax (except that you don't need to specify wrdApplication unless you run the code from another application, perhaps Excel). Since the code appears without fault errors must have their origin in the reference to frmWordEdit. I suggest that you run this code with plain numbers, expressed in points, and see if you still get the same result.
frmWordEdit.txtMLeftRight would appear to be a Textbox. Since you don't specify which property you are referring to it must be the default which is the Value property. The Value property of a Textbox holds a string which you feed into the InchesToPoints function. That function takes a single, if I'm not mistaken - anyway, a numeric value. Therefore I suspect that the string isn't correctly translated. Try something like InchesToPoints(Val(frmWordEdit.txtMLeftRight)).

Related

Remove part of the border of a table excel VBA

There is a set of forms that we sometimes have to fill out at work, and I'm trying to automate the task by having excel VBA recreate the form as a word document and populate with the appropriate data and print as a pdf. I'm getting stuck on removing the border line style. I want there to be no border line on the left side. I have tried different approaches, and the one that seems the most likely that it should work based on my understanding is below:
(note: ".Border(xlEdgeleft).LineStyle = xlLineStyleNone" is the line that is giving me trouble)
Sub main()
Dim objWord As Object
Dim objDoc As Object
Dim objHdrRange As Object
Dim myTable As Object
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add
Set objHdrRange = objDoc.Sections(1).headers(1).Range
Set myTable = objWord.activedocument.tables.Add(objHdrRange, 5, 5)
With myTable
.Borders.enable = True
.Border(xlEdgeleft).LineStyle = xlLineStyleNone
‘more code goes here later
End With
Set objDoc = Nothing
Set objHdrRange = Nothing
objWord.Quit
End Sub
xlEdgeLeft and xlLineStyleNone are from the Excel Object Model, not from the Word Object Model, and you need the latter.
Since you are late-binding, you could add the following lines:
Const wdBorderLeft As Long = -2
Const wdLineStyleNone As Long = 0
and replace xlEdgeLeft and xlLineStyleNone with these, respectively.
See the WdBorderType and WdLineStyle enum docs for more detail.

Word remains in the background despite the "Active" function VBA

I need to make barcode label sheets for items. For this I use Excel with VBA and a "user form" to help the user in entering the number and information of bar codes. Once I have all my information for my barcodes, I transfer its information to a Word in which I format it to make printable labels.
My system works fine, although a bit long when there are a large number of labels to transfer, but once word and excel have been closed once when I want to restart the transfers, Word no longer comes to the fore , which makes me completely miss the transfer. I am using the tab key which is the main source of the problem.
I have tried performing the same actions as the tab key with other commands like "next" so that it is no longer a problem. However this does not work entirely because the tab key allows at the end of a page to continue the layouts on a new page which the "next" function does not do.
So my questions are: How can I force Word to come to the fore? Can we replace the tab key with another parameter that allows me to do the same thing?
I provide you below the code of my loop performing the transfer.
Dim appwd As Word.Application
Dim oDoc As Object
Dim Code As String, SKU As String, Name As String, Size As String
Dim DerLign As Byte
With Sheets("Reference")
DerLign = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row
End With
On Error Resume Next
Set appwd = GetObject(, "Word.Application")
If Err Then
Set appwd = New Word.Application
End If
On Error GoTo 0
With appwd
If .Documents.Count = 0 Then
.Documents.Add
End If
Set oDoc = .MailingLabel.CreateNewDocument("3474")
.Visible = True
.Activate
' Colle les données dans Word
For i = 8 To DerLign
Code = ThisWorkbook.Worksheets("Reference").Range("B" & i)
SKU = ThisWorkbook.Worksheets("Reference").Range("C" & i)
Name = ThisWorkbook.Worksheets("Reference").Range("D" & i)
Size = ThisWorkbook.Worksheets("Reference").Range("E" & i)
appwd.Selection.ParagraphFormat.Alignment = 1
appwd.Selection.TypeParagraph
appwd.Selection.TypeText Text:=SKU
appwd.Selection.TypeParagraph
appwd.Selection.Font.Name = "Code EAN13"
appwd.Selection.Font.Size = 40
appwd.Selection.TypeText Text:=Code
appwd.Selection.Font.Name = "Calibri"
appwd.Selection.Font.Size = 11
appwd.Selection.TypeParagraph
appwd.Selection.TypeText Text:=Name + " " + Size
SendKeys "{TAB}", False
Next i
End With
End Sub
Regards

Automatically selecting from content control dropdown

I'm trying to select an option from a content control dropdown in a pre built MS Word template based on values in excel. I've done this by matching the named ranges to a respective tag in the template. I was able to get all of the rich text, regular text, and checkboxes to successfully input their respective values, but when it gets to the dropdown list, it skips right past it. I've tried using the same code as the text content controls in order to proceed with the dropdown selection with no such luck. The .Tag matches the named range for all the dropdowns... I'm stumped. Here is the code. Again, all of the code works, except for the first ElseIf.
Sub Transfer()
Dim cs As Workbook
Dim wrd As Word.Application
Dim pc As Word.Document
Dim CC As ContentControl
Dim CCTag As String
Dim CStxt As String
Set cs = ThisWorkbook
Set wrd = CreateObject("Word.Application")
Set pc = wrd.Documents.Open("Template Source")
For Each CC In pc.ContentControls
On Error Resume Next
CCTag = CC.Tag
If CCTag <> "" Then
CStxt = Range(CCTag)
If CC.Type = wdContentControlRichText Or CC.Type = wdContentControlText Then
CC.Range.Text = CStxt
ElseIf CC.Type = wdContentControlComboBox Or CC.Type = wdContentControlDropdownList Then
CC.SetPlaceholderText , , CStxt
ElseIf CC.Type = wdContentControlCheckBox Then
CC.Checked = False
If CStxt = "True" Then
CC.Checked = True
ElseIf CStxt = "False" Then
CC.Checked = False
Else
CC.Checked = False
End If
End If
End If
Next CC
End Sub
Your code works for me - assuming each Excel range refers to a single cell. Your code could, however, be both improved and simplified:
Sub Transfer()
Dim wdApp As Word.Application, wdDoc As Word.Document, wdCC As Word.ContentControl
Dim CStxt As String
Set wdApp = CreateObject("Word.Application")
Set wdDoc = wdApp.Documents.Open("Template Source")
For Each wdCC In wdDoc.ContentControls
With wdCC
If .Tag <> "" Then
CStxt = ThisWorkbook.Names(.Tag).RefersToRange.Text
Select Case .Type
Case wdContentControlRichText, wdContentControlText
.Range.Text = CStxt
Case wdContentControlDropdownList, wdContentControlComboBox
.Type = wdContentControlText
.Range.Text = CStxt
.Type = wdContentControlDropdownList
'.SetPlaceholderText Text:=CStxt
Case wdContentControlCheckBox
If CStxt = "True" Then
.Checked = True
Else
.Checked = False
End If
End Select
End If
End With
Next
End Sub
I do have to wonder, though, why you'd be modifying the placeholder text rather than selecting a combobox/dropdown item corresponding to CStxt. See code modification for a simple way to update the combobox/dropdown. For comboboxes, even that isn't needed - you could simply move ', wdContentControlComboBox' to follow 'wdContentControlText'.
Firstly, I recommend that you refer to your range directly by referencing the workbook/worksheet you have declared in your variable cs This will ensure your subroutine executes on the correct workbook and correct worksheet.
If I have understood your question correctly - you would like to transfer a set of values in an Excel range over to the matching MS Word content control box - the issue would appear to be your use of
CStxt = Range(CCTag)
You cannot assign a variable of type string (CStxt) to an entire range of cells. This would generate a "Runtime error 13: Type mismatch" when executing your code.
In order to get this line to work, I had to change it to a single value/cell reference. See example below;
CStxt = cs.Sheets(1).Range("A1")
In this example, cell A1 has the value you want to transfer over to the relevant MS Word content control. This has the effect of giving the content control a display value equal to the Excel value, but is not added to the list to allow you to select it.
You can change the reference to A1 so that your named range is a single cell.

Excel VBA Code Race Condition Not Fixed by Wait, Sleep, DoEvents, etc

SOLVED! See the code below for the solution!
I have an Excel file containing multiple shape objects next to a series of text. I wrote a script to identify the location of each shape, identify how many cells to the right and down the text extends to, sets that as a range and then imports that into a chart object so I can save it as a .jpg.
The trouble is that between the creation of the chart and the pasting of the string there exists a Race Condition. If I step through the script it works fine, but as soon as I run it I get nothing but blank images.
I've tried Application.ScreenUpdating = True; Application.PrintCommunication = True; and DoEvents
I've also tried Application.Wait, but even having it wait ten seconds doesn't do the trick, when stepping through the code the chart is loaded in less than 2 seconds.
Recently I tried the kernel32 sleep method as well, and that doesn't seem to work either. Again, the amount of time I let the system sleep far exceeded my stepping. I also added all of the above methods between each line within the With statement (obviously not as a solution, but as a test) and that didn't work either..
At this point I'm completely at a loss.
If I place a stop at .Chart.Paste and then run the script (F5), and just keep hitting Run then the script works wonderfully. I just don't want users to have to sit there and hit run 600 times.
There are obvious redundancies written in between the creation of the chart and pasting of the text. This is all in the attempt at getting the code to work properly when run, and once a solution is found most of that code will be removed.
Option Explicit
Public Function ChartCheck() As String
ReCheckChart:
DoEvents
If ActiveWorkbook.ActiveSheet.ChartObjects.Count > 0 Then
GoTo ContinuePaste:
Else
GoTo ReCheckChart:
ContinuePaste:
End If
End Function
Public Function GetFolder() As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder to Save the Images In"
.AllowMultiSelect = False
If .Show -1 Then GoTo NextCode:
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
Private Sub DNImageExtraction_Click()
Dim fileName As String
Dim targetWorkbook As Excel.Workbook
Dim targetWorksheet As Excel.Worksheet
Dim saveLocation As Variant
Dim saveName As String
Dim targetShape As Shape
Dim workingRange As Excel.Range
Dim bottomRow As Long
Dim workingRangeWidth As Double
Dim workingRangeHeight As Double
Dim tempChart As ChartObject
Application.ScreenUpdating = False
Application.DisplayAlerts = False
DNImageExtraction.AutoSize = False 'This is necessary to prevent the system I use from altering the font on the button
DNImageExtraction.AutoSize = True
DNImageExtraction.Height = 38.4
DNImageExtraction.Left = 19.2
DNImageExtraction.Width = 133.8
fileName = Application.GetOpenFilename("Excel Files (*.xls*),*.xls*", , "Please select Excel file...")
Set targetWorkbook = Workbooks.Open(fileName)
Set targetWorksheet = targetWorkbook.ActiveSheet
saveLocation = GetFolder
For Each targetShape In targetWorksheet.Shapes
Set workingRange = targetWorksheet.Cells(targetShape.TopLeftCell.Row, targetShape.TopLeftCell.Column).Offset(1, 0)
saveName = workingRange.Text
If workingRange.Offset(0, 1).Value "" Then
If workingRange.Offset(1, 1).Value = "" Then
Set workingRange = Nothing
Set workingRange = targetWorksheet.Cells(targetShape.TopLeftCell.Row, targetShape.TopLeftCell.Column).Resize(, 2)
Else
bottomRow = workingRange.Offset(0, 1).End(xlDown).Row
Set workingRange = targetWorksheet.Cells(targetShape.TopLeftCell.Row, targetShape.TopLeftCell.Column).Resize((bottomRow + 2 - workingRange.Row), 2)
End If
workingRangeWidth = workingRange.Width
workingRangeHeight = workingRange.Height
End If
workingRange.CopyPicture Appearance:=xlPrinter, Format:=xlPicture
Set tempChart = targetWorksheet.ChartObjects.Add(0, 0, workingRangeWidth, workingRangeHeight)
Application.ScreenUpdating = True
Application.PrintCommunication = True
DoEvents
Call ChartCheck
tempChart.Chart.Paste
Application.ScreenUpdating = False
tempChart.Chart.Export fileName:=saveLocation & "\DN " & saveName & ".jpg", Filtername:="JPG"
tempChart.Delete
Set tempChart = Nothing
Next
Application.Workbooks(targetWorkbook.Name).Close savechanges:=False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Any assistance in either a solution to the Race Condition, or re organizing the script to avoid the Race Condition altogether would be greatly appreciated.
(Code above was updated per suggestions made by Macro Man, and then re-modified once again to add in all previous suggestions on how to fix Race Condition issues after the changes were not effective.)
Consider using Application.OnTime which is good feature. It allows the scheduling of some code to be run at a certain time, most often one adds a few seconds to the current time.
Excel VBA is single-threaded and so there is no real synchronization but there is a message pump to keep order. The great thing about Application.OnTime is that it will not run despite being scheduled until the current graph of code has completed.
Because Application.OnTime uses the message pump as that is a FIFO structure it is possible to interleave the execution of code.
I think this might help here.
You can schedule a "hasItFinished" procedure which checks the existence of the shape/chart objects and if not re-schedules itself.
P.S. Can be a little tricky to debug, refactor as much code as possible outside of the procedure that you will schedule and unit test them separately. Please don't expect the lovely Edit,Debug and Continue flow that you normally get with VBA if you go down this path.
Try getting rid of the error handlers and labels, and working with objects directly instead of searching through workbook/worksheet collections. Also using meaningful variable names and proper indentation will help follow the code easily should you have any issues.
If your code works when stepping through, that usually suggests there is some issue with the use of ActiveWorkbook when workbooks are being opened/closed. Working with workbooks as objects allows us to overcome that problem because no matter if the workbook is active or not, we are always using the same instance of that workbook.
Private Sub DNImageExtraction_Click()
Dim fileName As String
Dim targetWorkbook As Excel.Workbook
Dim targetWorksheet As Excel.Worksheet
Dim saveLocation As Variant
Dim saveName As String
Dim targetShape As Shape
Dim workingRange As Excel.Range
Dim bottomRow As Long
Dim workingRangeWidth As Double
Dim workingRangeHeight As Double
Dim tempChart As ChartObject
Application.ScreenUpdating = False
Application.DisplayAlerts = False
fileName = Application.GetOpenFilename("Excel Files (*.xls*),*.xls*", , "Please select Excel file...")
Set targetWorkbook = Workbooks.Open(fileName)
Set targetWorksheet = targetWorkbook.ActiveSheet
saveLocation = GetFolder
For Each targetShape In targetWorksheet.Shapes
Set workingRange = targetWorksheet.Cells(targetShape.TopLeftCell.Row, targetShape.TopLeftCell.Column).Offset(1, 0)
saveName = workingRange.Text
If workingRange.Offset(0, 1).value <> "" Then
If workingRange.Offset(1, 1).value = "" Then
Set workingRange = Nothing
Set workingRange = targetWorksheet.Cells(targetShape.TopLeftCell.Row, targetShape.TopLeftCell.Column).Resize(, 2)
Else
bottomRow = workingRange.Offset(0, 1).End(xlDown).Row
Set workingRange = targetWorksheet.Cells(targetShape.TopLeftCell.Row, targetShape.TopLeftCell.Column).Resize((bottomRow + 2 - workingRange.Row), 2)
End If
workingRangeWidth = workingRange.Width
workingRangeHeight = workingRange.Height
End If
workingRange.CopyPicture Appearance:=xlPrinter, Format:=xlPicture
Set tempChart = targetWorksheet.ChartObjects.Add(0, 0, workingRangeWidth, workingRangeHeight)
With tempChart
.Chart.Paste
.Chart.Export FileName:=saveLocation & "\DN " & saveName & ".jpg", Filtername:="JPG"
.Delete
End With
Set tmpChart = Nothing
DoEvents
Next
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub

Finding heading of chapters in word file and copying individual paragraphs to new word file with VBA

Since nobody was able to help me with the problem I posted here before (link is below), I am now trying to solve the task through VBA.
Finding a heading in word file and copying entire paragraph thereafter to new word file with python
To briefly recap, I have a large amount of word files, which I would like to reduce to a more readable size each. In each file, there is one heading that appears several times, always formated as a 'Heading 2'. I look for this specific heading which occurs several times in a document and I want to copy all the text parts in just these chapters with the respective heading to a new word document.
I decided to create an excel file in which I list the files and the respective heading of the chapters that I want to copy (see picture below).
To do this now I have written the following code:
Sub SelectData()
Application.ScreenUpdating = False
Dim WdApp As Word.Application
Set WdApp = CreateObject("Word.Application")
Dim Doc As Word.Document
Dim NewDoc As Word.Document
Dim HeadingToFind As String
Dim ChapterToFind As String
Dim StartRange As Long
Dim EndRange As Long
Dim WkSht As Worksheet
Dim LRow As Long
Dim i As Long
Set WkSht = ThisWorkbook.Sheets("Sheet1")
LRow = WkSht.UsedRange.Cells.SpecialCells(xlCellTypeLastCell).Row
With WkSht
For i = 1 To LRow
If Dir(.Cells(i, 1).Text, vbNormal) = "" Then
.Cells(i, 3).Value = "Please check File Location"
Else
Set Doc = WdApp.Documents.Open(Filename:=.Cells(i, 1).Text, _
AddToRecentFiles:=False, Visible:=False, ReadOnly:=False)
Set NewDoc = Documents.Add
ChapterToFind = LCase(.Cells(i, 2).Text)
With Doc
Selection.HomeKey Unit:=wdStory
With Selection
With .Find
.ClearFormatting
.Text = ChapterToFind
.MatchWildcards = False
.MatchCase = True
.Execute
End With
If .Find.Found Then
.Collapse wdCollapseStart
With .Find
.Text = ""
.Style = "Heading 2"
.Forward = False
.Execute
End With
.MoveDown Count:=1
.HomeKey Unit:=wdLine
StartRange = .Start
.Find.Forward = True
.Find.Execute
.Collapse wdCollapseStart
.MoveUp Count:=1
.EndKey Unit:=wdLine
EndRange = .End
Doc.Range(StartRange, EndRange).Copy
NewDoc.Content.Paste
NewDoc.SaveAs2 Doc.Path & "Clean" & ".docx", wdFormatFlatXML
Else
WkSht.Cells(i, 4).Value = "Error Chapter Not Found"
End If
End With
End With
WdApp.Quit
Set Doc = Nothing: Set NewDoc = Nothing: Set WdApp = Nothing: Set WkSht = Nothing
Application.ScreenUpdating = True
End If
Next
End With
End Sub
However I am really struggling. It seems to not work as I constantly get an error with the command (RunTimeError 438):
Selection.HomeKey Unit:=wdStory
I am aware that I have to activate the Microsoft Word 15.0 Object Library in the references to be able to get word commands. Nevertheless it is not working.
I would greatly appreciate any help, I am also open to other suggestions of course.
The word files look something like in the picture below, however the chapter that I want to extract can occur several times within one word document. As a result my code would probably need a loop or something, I was not able to get this done.
Also I have considered the following links to the topic:
Use VBA with Powerpoint to Search titles in a Word Doc and Copy Text into another Word Document
VBA: open word from excel
word vba: select text between headings
Did I understand this correctly? The following code is the core of what I think you're trying to do. It finds the first Heading 2, then finds all the paragraphs after it until it finds another header of any type or the end of the document. startCopyRange and endCopyRange is the range of those paragraphs. You'll have to piece this into your Excel routine.
A few notes. Always save the active document to a variable and work from that; the user is then free to change active documents while this routine is running. Never use Selection, always use ranges. Never use relative movements like Move, always use API calls.
Sub SelectData()
Dim Doc As Word.Document
Set Doc = ActiveDocument
Dim findRange As Range
Set findRange = Doc.Range
ChapterToFind = "My Chapter"
findRange.Find.Text = ChapterToFind
findRange.Find.Style = "Heading 2"
findRange.Find.MatchCase = True
Dim startCopyRange As Long
Dim endCopyRange As Long
Do While findRange.Find.Execute() = True
startCopyRange = findRange.End + 1
endCopyRange = -1
'findRange.Select
Dim myParagraph As Paragraph
Set myParagraph = findRange.Paragraphs(1).Next
Do While Not myParagraph Is Nothing
myParagraph.Range.Select 'Debug only
If InStr(myParagraph.Style, "Heading") > 0 Then
endCopyRange = myParagraph.Range.Start - 0
End If
If myParagraph.Next Is Nothing Then
endCopyRange = myParagraph.Range.End - 0
End If
If endCopyRange <> -1 Then
Doc.Range(startCopyRange, endCopyRange).Select 'Debug only
DoEvents
Exit Do
End If
Set myParagraph = myParagraph.Next
DoEvents
Loop
Loop
End Sub

Resources