通过VBA

时间:2016-06-01 10:24:51

标签: excel vba excel-vba ms-word word-vba

我有一个工作簿,可以根据工作簿中的Word模板和表创建Word报告。

根据设备类型,它会从电子表格中复制范围并将其粘贴到word文档中的两个书签位置(bmInternal和bmExternal)。我尝试使用PasteAppendTable,但这仅适用一次。如果我尝试使用它两次,对于每个书签,它两次都不会复制。因此我使用Paste for one和PasteAppendTable作为第二个(PasteAppendTable更整洁,因为格式更好)。

这很好用,但我对代码进行了更改,与此无关,现在Paste(转到bmInternal)无效。当我没有改变任何有关该部分的内容时,我不明白为什么:

Sub Data2Word()

Application.GoTo Reference:=ActiveSheet.Range("A2")

GoAgain:
On Error Resume Next
Dim vItem As String
'Dim vImagePath As String

Dim vCurrentRow As Integer

Dim vDesc As String
Dim vN2 As String
Dim vGuide As String
Dim vUnit As String
Dim vBlock As String

Dim wrdPic As Word.InlineShape
Dim rng As Excel.Range                    'our source range
Dim rngText As Variant
Dim rngText2 As Variant

Dim wdApp As New Word.Application   'a new instance of Word
Dim wdDoc As Word.Document          'our new Word template
Dim myWordFile As String            'path to Word template
Dim wsExcel As Worksheet
Dim tmpAut

'Find Item and type
vItem = ActiveCell.Value
vDesc = ActiveCell.Offset(0, 2)
vN2 = ActiveCell.Offset(0, 1)
vGuide = ActiveCell.Offset(0, 3)
vBlock = ActiveCell.Offset(0, 4)
vUnit = Left(vItem, 3)

If ActiveSheet.Range("rngREPORTED") = "Yes" Then
    MsgBox vItem & " already has a report."
    Exit Sub
End If
'initialize the Word template path
'here, it's set to be in the same directory as our source workbook
myWordFile = "W:\Entity\Inspect\WORD\INSPECTION TEMPLATES\Inspection Template - 20160511.dotx"

'open a new word document from the template
Set wdDoc = wdApp.Documents.Add(myWordFile)

If vGuide = "IGE01" Then

    rngText = "rngEXCH"
    rngText2 = "rngEXCHE"

ElseIf ActiveCell.Offset(, 4) = "Mono" Then

    'Do Mono
    rngText = "rngMONO"

Else

        ActiveWorkbook.Names.Add Name:="rngItemSub", RefersTo:=Worksheets("SubEquipment").Range("B" & ActiveCell.Offset(0, 6) & ":C" & ActiveCell.Offset(0, 7) + ActiveCell.Offset(0, 6))

CarryOn:
        rngText = "rngItemSub"

End If

'Insert Tables
'get the range of the data

Set rng = Range(rngText)
rng.Copy                            'copy the range

wdDoc.Bookmarks("bmInternal").Range.Paste 'AppendTable

If vGuide = "IGE01" Then
    Set rng = Range(rngText2)
    rng.Copy
End If

wdDoc.Bookmarks("bmExternal").Range.PasteAppendTable

wdDoc.Bookmarks("bmItem").Range.InsertAfter vItem
wdDoc.Bookmarks("bmDesc").Range.InsertAfter vDesc
wdDoc.Bookmarks("bmN2").Range.InsertAfter vN2
wdDoc.Bookmarks("bmGuide").Range.InsertAfter vGuide
wdDoc.Bookmarks("bmBlock").Range.InsertAfter vBlock

wdDoc.Variables("wvItem").Value = vItem
ActiveDocument.Fields.Update

With wdDoc
        Set wrdPic = .Bookmarks("bmImage").Range.InlineShapes.AddOLEObject(ClassType:="AcroExch.Document.7", Filename:="W:\Entity\Inspect\T&I\2016\Various Items\Photos\Sorted\" & vItem & ".pdf", LinkToFile:=False, DisplayAsIcon:=False)
        wrdPic.ScaleHeight = 55
        wrdPic.ScaleWidth = 55
End With

wdApp.Visible = True

wdApp.Activate

wdDoc.SaveAs "W:\Entity\Inspect\WSDATA\REPORTS\2016\" & vUnit & "\" & vItem & " " & vN2 & " THO.docx" 'Mid(ActiveDocument.Name, 1, Len(ActiveDocument.Name) - 4)

MoveHere:

ActiveWorkbook.Sheets("AllItems").Range("G" & ActiveCell.Offset(0, 8)).Value = "Yes"
ActiveWorkbook.Save

End Sub

1 个答案:

答案 0 :(得分:0)

我认为DocVariables更容易使用书签。在Word DocVariables上快速搜索Google。在Word中设置正确的设置,然后运行下面的脚本。

UIView.animateWithDuration(Double(0.5), animations: {
    self.yourConstraint.constant = //Whatever
    self.view.layoutIfNeeded()
})