austin_love_32
New Member
- Joined
- Feb 13, 2015
- Messages
- 3
Hello all, I'm pretty much brand new to the world of VBA and so far, it's been an uphill battle. What I have right now is a macro in excel that will take named ranges and copy them into their own sheets (if those ranges exist), and from there prompt the user to create a word document from a template and save it under a name input via messagebox. All that works. The problem I can't seem to figure out is moving one of those ranges into a table in Word. I've been jury rigging some code to make it work with copy and paste, but I can't seem to make it merge with an existing table using Selecton. PasteAppendTable as it gives me an error saying the action is not available. I'm doing this by running a sub in excel that calls a function in excel that calls a series of subs in word, and that's where it stops. I can verify that the cells are in clipboard, and it works if I stop the macro and paste it by hand, or manually select anywhere in the word doc and run the word macro. There is a small 2 x 1 table in the template that I'm using to merge with, and then expand. I'll attach the code below.
The excel code:
The word code:
The excel code:
Code:
Public Function DailyChecklist()
Dim objWord
Dim objDoc
Dim objSelection
NewSaveName = InputBox("What is the name of this machine?")
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add(Template:="Z:\Checklists\Checklist.dotm")
objWord.Visible = True
Set objSelection = objWord.Selection
Range("DailyChecklist").Copy
objWord.Run ("DailyTable")
objDoc.SaveAs ("Z:\Checklists\" + NewSaveName)
objWord.Run ("MachNam")
objDoc.Save
MsgBox ("Your daily checklist has been completed for this machine. Please adjust the table as necessary to ensure the minimum number of pages possible.")
End Function
The word code:
Code:
Dim NewSaveName As String
Public Declare PtrSafe Sub Sleep Lib "kernel32" ( _
ByVal dwMilliseconds As LongLong)
Public Sub MachNam() 'Edits a textbox in the header
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes.Range(Array("Text Box 4")).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.InsertBefore Text:=Left(ActiveDocument.Name, _
Len(ActiveDocument.Name) - 5) + " "
End Sub
Sub Dailytable() 'edits and formats the table
ActiveDocument.Tables(1).Cell(1,1).Select
Call GrowingOlderButNotUp ' to delay the paste just in case
Selection.PasteAppendTable 'error line
Call TableSize 'more table formatting
Selection.Tables(1).Columns(2).Select
Selection.InsertColumnsRight
Selection.Tables(1).Cell(1, 3).Select
Selection.Text = "Comments"
Selection.Tables(1).Rows(1).Select
Selection.Font.Bold = wdToggle
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = -603937025
Selection.Tables(1).AutoFitBehavior (wdAutoFitWindow)
Dim RowNum As Variant
RowNum = Selection.Tables(1).Rows.Count
Selection.Tables(1).Cell(RowNum, 1).Select
Selection.InsertRowsBelow 1
Selection.Cells.Merge
Selection.Shading.Texture = wdTextureNone
Selection.Shading.ForegroundPatternColor = wdColorAutomatic
Selection.Shading.BackgroundPatternColor = -603937025
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Text = _
"Note to perform a visual inspection only. Report any oil or lubricant refills to your supervisor."
Selection.Font.Bold = wdToggle
Selection.Tables(1).Rows.Select
Selection.Range.ParagraphFormat.KeepTogether = True
Selection.Range.ParagraphFormat.KeepWithNext = True
End Sub
Sub GrowingOlderButNotUp() 'delays macro for one second
Sleep 1 * 1000
End Sub
Sub TableSize() 'Formates table
ActiveDocument.Tables(1).Select
Selection.Tables(1).Rows.Alignment = wdAlignRowCenter
Selection.Rows.HeightRule = wdRowHeightAuto
Selection.Rows.Height = InchesToPoints(0)
Selection.Rows.AllowBreakAcrossPages = False
Selection.Rows.HeadingFormat = True
Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter
Selection.Font.Size = 9
With Selection.ParagraphFormat
.LeftIndent = InchesToPoints(0)
.RightIndent = InchesToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 10
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.WidowControl = True
.KeepWithNext = True
.KeepTogether = True
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = InchesToPoints(0)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
.MirrorIndents = False
.TextboxTightWrap = wdTightNone
End With
End Sub
Last edited: