Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #6  
Old 01-03-2019, 08:53 AM
gmaxey gmaxey is offline How to I create a macro that dynamically creates a table, to enter questions, in the below format? Windows 10 How to I create a macro that dynamically creates a table, to enter questions, in the below format? Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

If each question consists of a single paragraph and there are no other paragraphs in the document (including empty paragraphs) then something like this mash of unorganized mess might do. I don't have time to refine:

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 1/3/2019
Dim oCol As New Collection
Dim lngIndex As Long
Dim oTbl As Table
Dim oRng As Range, oRngDup As Range
  Set oRng = ActiveDocument.Range
  For lngIndex = 1 To oRng.Paragraphs.Count
    oCol.Add Left(oRng.Paragraphs(lngIndex).Range.Text, Len(oRng.Paragraphs(lngIndex).Range.Text) - 1)
  Next
  Set oRngDup = oRng.Duplicate
  oRng.Collapse wdCollapseEnd
  oRng.InsertBefore vbCr
  oRng.Collapse wdCollapseEnd
  oRngDup.End = oRng.Start
  Set oTbl = oRng.Tables.Add(oRng, 4, 2)
  With oTbl
    .Style = "Table Grid"
        For lngIndex = 1 To 4
      .Cell(lngIndex, 1).Borders(wdBorderBottom).LineStyle = wdLineStyleNone
    Next lngIndex
    .Columns(1).Width = 18
    .AutoFitBehavior wdAutoFitWindow
    .Cell(1, 2).Range.Text = oCol.Item(1)
    For lngIndex = 2 To oCol.Count
      .Rows.Add
      .Rows.Last.Cells(2).Range.Text = oCol.Item(lngIndex)
      .Rows.Add
      .Rows.Add
      .Rows.Add
    Next
    For lngIndex = 4 To .Rows.Count Step 4
      .Cell(lngIndex, 1).Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
    Next lngIndex
  End With
  oRngDup.Delete
  ActiveDocument.Paragraphs(1).Range.Delete
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
 

Tags
vba in microsoft word

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to I create a macro that dynamically creates a table, to enter questions, in the below format? Legal Caption needs a way to dynamically create right-paren separator gilly1 Mail Merge 2 03-08-2017 08:39 AM
Create a ComboBox in a Form that creates a new document and autofills a textBox BlackGirlMagic Word VBA 5 02-13-2017 02:39 PM
How to I create a macro that dynamically creates a table, to enter questions, in the below format? Automatically Organize data in a sheet differently (in order to create a dynamically updated graph) carlos_cs Excel 3 05-04-2016 08:44 AM
Looking for code to create a macro that prompts user to enter text, then does a find/replace. sfvegas PowerPoint 0 01-08-2016 02:22 AM
How to I create a macro that dynamically creates a table, to enter questions, in the below format? Dynamically create documents MrRikkie Word VBA 1 10-12-2012 09:15 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:29 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft