Import/Export Text Boxes

image_pdfimage_print

There are two complementary MSWord macros here: One exports the text of all text boxes to an external file, where you can edit it as you please, and the other re-imports the updated text into the original text box locations.

Note1: The indicated file (in this case “C:\temp\MyFile.txt”) must exist before you run either macro, it won’t get created automatically.

Note2: The order of text boxes is not determined from top-down (i.e. 1,2,3), it is determined by the order in which the text boxes were inserted into the document. So, do NOT change the order of text in the output text file! If you do so, the text will not be re-imported into the correct text boxes!!

  1. Sub ExportTextBoxes()
  2.  
  3.     Selection.HomeKey unit:=wdStory
  4.     txtBoxID = 0
  5.     Open "C:\temp\MyFile.txt" For Output As #1
  6.     For Each oStory In ActiveDocument.Shapes
  7.  
  8.         'Check if text box contains text
  9.         With oStory.TextFrame
  10.             If .HasText Then
  11.                 txtBoxID = txtBoxID + 1
  12.                 'if it does, move cursor to start of text box
  13.                 Set myRange = .TextRange
  14.                 myRange.Select
  15.                 Write #1, Selection.Text
  16.                 Selection.TypeText Text:="{{{Textbox" & txtBoxID & "}}}"
  17.             End If
  18.         End With
  19.     Next oStory
  20.     Close #1
  21. End Sub
  1. Sub ImportTextBoxes()
  2.     Dim MyString, s
  3.     Selection.HomeKey unit:=wdStory
  4.     Open "C:\temp\MyFile.txt" For Input As #2
  5.     Do While Not EOF(2)                'Loop until end of file
  6.  
  7.         For Each s In ActiveDocument.Shapes
  8.             With s.TextFrame
  9.                 If .HasText Then
  10.                     Input #2, MyString    'Read 1 line at a time
  11.                     Set myRange = .TextRange
  12.                     myRange.Select
  13.                     Selection.TypeText Text:=MyString
  14.                 End If
  15.             End With
  16.         Next
  17.  
  18.     Loop
  19.     Close #2
  20. End Sub

Author: admin