![]() |
|
![]() |
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
![]()
I’ve been attempting to format the following questions into a table (image shown), using a macro, in Microsoft Word. But the number of questions that the macro needs to be used on is always different. So it needs to be dynamic in that it reads the number of questions, then formats a table for them accordingly. I plead to the VBA guru's I know read this forum, I’ve been trying this a while now but can’t seem to find much on this subject area online, and so I don't know where to start.
Thank you in advance for any responce. |
#2
|
||||
|
||||
![]()
You don't need a macro for that. Instead, create a custom Quick Part for the rows you need for a sample question. Then, whenever you need to insert another question, simply insert the Quick Part at that point.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
![]()
Cross-posted at: https://answers.microsoft.com/en-us/...6-7936e0d4781b
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
![]() Quote:
I'm sorry for the late reply. The problem is that the user(not necessarily me) receives the list of questions from a third party, rather than writing it ourselfs.I intend to add several features such a multiple choice answers in some of the blank cells in the table (I don't know if this helps, but gives perspective). Thank you for taking the time to answer. |
#5
|
|||
|
|||
![]() Quote:
|
#6
|
|||
|
|||
![]()
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 |
#7
|
||||
|
||||
![]() Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
![]() Quote:
I'll try this out, thank you for tkaing the time ![]() |
#9
|
|||
|
|||
![]() Quote:
![]() |
#10
|
|||
|
|||
![]()
If you created a style "Questions" and applied it to each paragraph that was a question then something like this bigger mess might do:
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 If oRng.Paragraphs(lngIndex).Style = "Questions" Then oCol.Add Left(oRng.Paragraphs(lngIndex).Range.Text, Len(oRng.Paragraphs(lngIndex).Range.Text) - 1) End If 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 For lngIndex = oRngDup.Paragraphs.Count To 1 Step -1 If oRngDup.Paragraphs(lngIndex).Style = "Questions" Then oRngDup.Paragraphs(lngIndex).Range.Delete End If Next lngIndex lbl_Exit: Exit Sub End Sub |
#11
|
|||
|
|||
![]() Quote:
With kind regards, Mathias[/font] |
#12
|
||||
|
||||
![]()
Mathias, I'm not sure of what your issue is exactly. Are you trying to run this macro multiple times in a single document or are you running it once? It looks like it was intended for a single run in a document.
It could be adjusted to be run on subsequent passes but would be done as a separate macro for ease of implementation.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
![]() |
Tags |
vba in microsoft word |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
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 |
![]() |
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 |
![]() |
MrRikkie | Word VBA | 1 | 10-12-2012 09:15 AM |