2014-10-30 63 views
-1

我從word文檔中的表格中提取每個單元格的值,然後基於這些值創建圖表。圖表很好。VBA插入圖表在每個頁面中的相同位置

但是,它在第​​一頁保持插入。有誰知道我怎樣才能將我的圖表插入每個頁面的相同位置?

郵件合併生成的Word文檔。這會導致問題嗎?

此外,劑量任何人都知道如何將圖表插入表格單元格?

Dim pge As Page 
Dim i As Integer 
i = 3 
Dim j As Integer 
j = 1 

For peg = 1 To Selection.Information(wdNumberOfPagesInDocument) 
     Dim tTable As Table 

     Set tTable = ActiveDocument.Tables(i) 
     Set cTable = ActiveDocument.Tables(j) 


     Dim wChart As Chart 
     Dim chartWorkSheet As Excel.Worksheet 
     Dim ThisYrSumCon As Integer 
     Dim ThisYrWinCon As Integer 
     Dim PreYrSumCon As Integer 
     Dim PreYrWinCon As Integer 
     Dim BefPreYrSumCon As Integer 
     Dim BefPreYrWinCon As Integer 

     '• 
     ThisYrSumCon = CInt(Left(tTable.Cell(2, 2).Range.Text, Len(tTable.Cell(3, 2).Range.Text))) 
     ThisYrWinCon = CInt(Left(tTable.Cell(3, 2).Range.Text, Len(tTable.Cell(3, 2).Range.Text) - 1)) 
     PreYrSumCon = CInt(Left(tTable.Cell(2, 3).Range.Text, Len(tTable.Cell(3, 2).Range.Text))) 
     PreYrWinCon = CInt(Left(tTable.Cell(3, 3).Range.Text, Len(tTable.Cell(3, 2).Range.Text))) 
     BePreYrSumCon = CInt(Left(tTable.Cell(2, 4).Range.Text, Len(tTable.Cell(3, 2).Range.Text))) 
     BePreYrWinCon = CInt(Left(tTable.Cell(3, 4).Range.Text, Len(tTable.Cell(3, 2).Range.Text))) 

     'MsgBox (ThisYrSumCon) 

     'cTable.Cell(3, 4).Range.Text = "test" 
     'cTable.Cell(12, 3).Range.Text = "test" 


     Set wChart = ActiveDocument.Shapes.AddChart.Chart 
     With wChart.Parent 
      .Top = 105 
      .Left = 205 
      .Width = 300 
      .Height = 150 
     End With 



     Set chartWorkSheet = wChart.ChartData.Workbook.WorkSheets(1) 
     chartWorkSheet.ListObjects("Table1").Resize chartWorkSheet.Range("A1:G2") 
     chartWorkSheet.Range("Table1[[#Headers],[Series 1]]").FormulaR1C1 = "Water Consumption Records" 
     wChart.ChartType = xlColumnClustered 


     chartWorkSheet.Range("A1").FormulaR1C1 = "" 
     chartWorkSheet.Range("B1").FormulaR1C1 = "2012 Summer" 
     chartWorkSheet.Range("C1").FormulaR1C1 = "2012 Winter" 
     chartWorkSheet.Range("D1").FormulaR1C1 = "2013 Summer" 
     chartWorkSheet.Range("E1").FormulaR1C1 = "2013 Winter" 
     chartWorkSheet.Range("F1").FormulaR1C1 = "2014 Summer" 
     chartWorkSheet.Range("G1").FormulaR1C1 = "2014 Winter" 

     chartWorkSheet.Range("A2").FormulaR1C1 = "" 
     chartWorkSheet.Range("B2").FormulaR1C1 = BePreYrSumCon 
     chartWorkSheet.Range("C2").FormulaR1C1 = BePreYrWinCon 
     chartWorkSheet.Range("D2").FormulaR1C1 = PreYrSumCon 
     chartWorkSheet.Range("E2").FormulaR1C1 = PreYrWinCon 
     chartWorkSheet.Range("F2").FormulaR1C1 = ThisYrSumCon 
     chartWorkSheet.Range("G2").FormulaR1C1 = ThisYrWinCon 

     wChart.ChartData.Workbook.Application.Quit 


     i = i + 5 
     j = j + 5 
     Selection.GoTo What:=wdGoToPage, Which:=lNextPage 


Next 

回答

0

大聲笑,我很高興我能回答我的問題... :)

下面是在每個頁面創建圖表的基礎上相同的格式字表的答案,並把圖表在同一地點每頁。

我整數是爲我找到每頁中的同一張表。

Dim Rng As Range, pg As Long 
Dim i As Integer 
i = 3 

With ActiveDocument 
    Set Rng = .Range(0, 0) 
    For pg = 1 To .ComputeStatistics(wdStatisticPages) 
     Set Rng = Rng.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=pg) 

     Rng.Collapse wdCollapseStart 

     Dim tTable As Table 
     Set tTable = ActiveDocument.Tables(i) 

     Dim wChart As Chart 
     Dim chartWorkSheet As Excel.Worksheet 
     Dim ThisYrSumCon As Integer 
     Dim ThisYrWinCon As Integer 
     Dim PreYrSumCon As Integer 
     Dim PreYrWinCon As Integer 
     Dim BefPreYrSumCon As Integer 
     Dim BefPreYrWinCon As Integer 

     ThisYrSumCon = CInt(Left(tTable.Cell(2, 2).Range.Text, Len(tTable.Cell(2, 2).Range.Text) - 1)) 
     ThisYrWinCon = CInt(Left(tTable.Cell(3, 2).Range.Text, Len(tTable.Cell(3, 2).Range.Text) - 1)) 
     PreYrSumCon = CInt(Left(tTable.Cell(2, 3).Range.Text, Len(tTable.Cell(2, 3).Range.Text) - 1)) 
     PreYrWinCon = CInt(Left(tTable.Cell(3, 3).Range.Text, Len(tTable.Cell(3, 3).Range.Text) - 1)) 
     BePreYrSumCon = CInt(Left(tTable.Cell(2, 4).Range.Text, Len(tTable.Cell(2, 4).Range.Text) - 1)) 
     BePreYrWinCon = CInt(Left(tTable.Cell(3, 4).Range.Text, Len(tTable.Cell(3, 4).Range.Text) - 1)) 

     Set wChart = .Shapes.AddChart(xlColumnClustered, 270, 105, 230, 150, Rng).Chart 
     Set chartWorkSheet = wChart.ChartData.Workbook.WorkSheets(1) 
     chartWorkSheet.ListObjects("Table1").Resize chartWorkSheet.Range("A1:G2") 
     chartWorkSheet.Range("Table1[[#Headers],[Series 1]]").FormulaR1C1 = "Water Consumption Records" 


     chartWorkSheet.Range("A1").FormulaR1C1 = "" 
     chartWorkSheet.Range("B1").FormulaR1C1 = "2012 Summer" 
     chartWorkSheet.Range("C1").FormulaR1C1 = "2012 Winter" 
     chartWorkSheet.Range("D1").FormulaR1C1 = "2013 Summer" 
     chartWorkSheet.Range("E1").FormulaR1C1 = "2013 Winter" 
     chartWorkSheet.Range("F1").FormulaR1C1 = "2014 Summer" 
     chartWorkSheet.Range("G1").FormulaR1C1 = "2014 Winter" 

     chartWorkSheet.Range("A2").FormulaR1C1 = "" 
     chartWorkSheet.Range("B2").FormulaR1C1 = BePreYrSumCon 
     chartWorkSheet.Range("C2").FormulaR1C1 = BePreYrWinCon 
     chartWorkSheet.Range("D2").FormulaR1C1 = PreYrSumCon 
     chartWorkSheet.Range("E2").FormulaR1C1 = PreYrWinCon 
     chartWorkSheet.Range("F2").FormulaR1C1 = ThisYrSumCon 
     chartWorkSheet.Range("G2").FormulaR1C1 = ThisYrWinCon 

     wChart.ChartData.Workbook.Application.Quit 

     i = i + 5 
     j = j + 5 

    Next 
End With 
相關問題