2013-05-01 100 views
1

我试图在表格中的特定单元格中嵌入图表。我能够实现它的唯一方法是使用selection.cut并粘贴。这在第二次和第三次运行后不起作用。这是我到目前为止:如何在MS Word中使用VBA在表格中的特定单元格中嵌入图表

Dim data1 As Variant 
data1 = InputBox("What was the Moving Water damage value (enter as 0.0 - 1.0).") 

Dim data2 As Variant 
data2 = InputBox("What was the Settlement damage value (enter as 0.0 - 1.0).") 

Dim data3 As Variant 
data3 = InputBox("What was the Pre-Exisiting damage value (enter as 0.0 - 1.0).") 


Dim i As Integer 

i = ActiveDocument.Tables.Count 
i = i + 1 

' Create table 
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=5, NumColumns:=2 
ActiveDocument.Tables(i).Cell(1, 2).Split NumColumns:=3 
ActiveDocument.Tables(i).Cell(1, 1).Range.Text = "Location:" 
ActiveDocument.Tables(i).Cell(1, 3).Range.Text = "Quantity (Measurable Area):" 
ActiveDocument.Tables(i).Cell(2, 1).Range.Text = "Description:" 
ActiveDocument.Tables(i).Cell(3, 1).Range.Text = "Analysis:" 
ActiveDocument.Tables(i).Cell(4, 1).Range.Text = "Cause(s) of Damage:" 
ActiveDocument.Tables(i).Cell(5, 1).Range.Text = "Recommended Repairs:" 

With ActiveDocument.Tables(i) 
    .Borders(wdBorderTop).LineStyle = wdLineStyleSingle 
    .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle 
    .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle 
    .Borders(wdBorderRight).LineStyle = wdLineStyleSingle 
    .Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle 
    .Borders(wdBorderVertical).LineStyle = wdLineStyleSingle 
End With 

Dim small As Boolean 
small = False 
Dim twoSeries As Boolean 
twoSeries = False 
Dim pieChart As Boolean 
pieChart = True 

Dim salesChart As Chart 
Dim chartWorkSheet As Excel.Worksheet 


With ActiveDocument.Tables(i) 
    .Borders(wdBorderTop).LineStyle = wdLineStyleSingle 
    .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle 
    .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle 
    .Borders(wdBorderRight).LineStyle = wdLineStyleSingle 
    .Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle 
    .Borders(wdBorderVertical).LineStyle = wdLineStyleSingle 
End With 

With ActiveDocument.Tables(i).Cell(4, 2).Range 
    ActiveDocument.Range(.Start, .Start).Select 
End With 

' Add in a new chart 
Set salesChart = ActiveDocument.InlineShapes.AddChart.Chart 
Set chartWorkSheet = salesChart.ChartData.Workbook.WorkSheets(1) 

' Resize the chart area 
chartWorkSheet.ListObjects("Table1").Resize chartWorkSheet.Range("A1:B4") 

' Rename Series 1 as Sales 
chartWorkSheet.Range("Table1[[#Headers],[Series 1]]").FormulaR1C1 = "Damage" 

' Add data to the chart 
chartWorkSheet.Range("A2").FormulaR1C1 = "Moving Water" 
chartWorkSheet.Range("A3").FormulaR1C1 = "Settlement" 
chartWorkSheet.Range("A4").FormulaR1C1 = "Pre-Exisiting" 
chartWorkSheet.Range("B2").FormulaR1C1 = data1 
chartWorkSheet.Range("B3").FormulaR1C1 = data2 
chartWorkSheet.Range("B4").FormulaR1C1 = data3 


' Quit Excel, since we no longer need it 
salesChart.ChartData.Workbook.Application.Quit 

' Put a box around the legend 
salesChart.Legend.Format.Line.Visible = msoCTrue 

' Fill the background with theme color accent 1 
With salesChart.ChartArea.Format.Fill 
    .Visible = msoTrue 
    .Solid 
    .ForeColor.ObjectThemeColor = wdThemeColorAccent1 
End With 

' Add a title and format it 
salesChart.HasTitle = True 
With salesChart.ChartTitle 
    .Characters.Font.Italic = True 
    .Characters.Font.Size = 18 
    .Characters.Font.color = RGB(0, 0, 100) 
    .Text = "Damage" 
End With 


If small Then 
' Size and move the chart 
With salesChart.Parent 
    .Left = 100 
    .Width = 300 
    .Height = 150 
End With 
End If 

If pieChart Then 
' Set chart type 
    salesChart.ChartType = xl3DPie 
End If 
+0

它的工作对我来说相当不错(宏地方你的图表中的每个新表的单元格(1,1))...其中并你有错误的类型... – 2013-05-01 05:49:47

+0

我如何获得它把它放在单元格(4,2) – 2013-05-01 14:35:44

回答

0

要将您的图表放置在单元格(4,2)内,您需要选择此单元格中的范围。因此,你的代码中添加以下代码添加图表文档之前:

'....your code here 

'<-- new code -- 
    'select destination cell 
    With ActiveDocument.Tables(i).Cell(4, 2).Range 
     ActiveDocument.Range(.Start, .Start).Select 
    End With 
'-- new code --> 

'your code... 
' Add in a new chart 
Set salesChart = ActiveDocument.InlineShapes.AddChart.Chart 
Set chartWorkSheet = salesChart.ChartData.Workbook.WorkSheets(1) 
+0

我使用word 2007,它仍然把它放在单元格(1,1)。当我第二次运行宏时。图表出现在第一个表格中,而不是第二个表格。 http://i44.tinypic.com/343tf7s.jpg – 2013-05-01 16:11:04

+0

在同一个文件中,你一个接一个地运行它,没有任何额外的文件变化?? – 2013-05-01 16:13:03

+0

我所做的唯一更改是按Enter键将光标放在新行上。 – 2013-05-01 16:17:53

相关问题