So I'm trying to write a macro for word that will insert a table into a document at a specific location.
The document is in a set format where I need to insert a line feed, the table and then another line feed following each paragraph with a style type of heading 4.
I have the following which inserts the table I like:
I also found the following on the MS support page as an example of finding a heading and inserting something. Here it's a string bein inserted.Code:ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _ 4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _ wdAutoFitFixed With Selection.Tables(1) If .Style <> "Table Grid" Then .Style = "Table Grid" End If .ApplyStyleHeadingRows = True .ApplyStyleLastRow = False .ApplyStyleFirstColumn = True .ApplyStyleLastColumn = False .ApplyStyleRowBands = True .ApplyStyleColumnBands = False End With Selection.TypeText Text:="A" Selection.MoveRight Unit:=wdCell Selection.TypeText Text:="B" Selection.MoveRight Unit:=wdCell Selection.TypeText Text:="C" Selection.MoveRight Unit:=wdCell Selection.TypeText Text:="D" Selection.MoveDown Unit:=wdLine, Count:=1 Selection.TypeParagraph Selection.MoveDown Unit:=wdLine, Count:=1 Selection.TypeParagraph
Ive been using these two bits in an effort to combine them so that rather than inserting the string i can insert a table. But I honestly haven't had a ton of experience with VB or word macros so it's not working out well.Code:Sub EditFindLoopExample() 'This example inserts "Tip: " at the beginning of ' every paragraph formatted with the Heading 3 style. With ActiveDocument.Content.Find .ClearFormatting .Style = wdStyleHeading3 'The Do...Loop statement repeats a series of ' actions each time this style is found. Do While .Execute(Forward:=True, Format:=True) = True With .Parent 'If the found text is the last ' paragraph in the document... If .End = ActiveDocument.Content.End Then .StartOf Unit:=wdParagraph, Extend:=wdMove .InsertAfter "Tip: " Exit Do 'If the found text is *not* the last ' paragraph in the document... Else .StartOf Unit:=wdParagraph, Extend:=wdMove .InsertAfter "Tip: " .Move Unit:=wdParagraph, Count:=1 End If End With 'Goes back to the beginning of the Do...Loop statement. Loop End With End Sub
I am hoping for some advice on either an approach or some examples... or maybe even a point towards a decent tutorials/examples web site?
Thanks in advance!~



Reply With Quote
Bookmarks