2013-05-14 83 views
0

我期望让我的MS Word VBA宏与Word 2007,2010和2013兼容,而无需下载MS Office对象所有机器上的库12,14和15。任何类型,我可以做到这一点?我的代码是下面的,似乎需要对象库12 & 14或15。它不只有12,14或15如何重写下面的MS Word VBA宏以与Word 2007/2010/2013兼容

data1 = InputBox("What was the Moving Water damage value (enter as 0.0 - 1.0).") 
' Validiate data 

Do 
If data1 > 1 Or data1 < 0 Then 
data1 = InputBox("Invalid input. What was the Moving Water damage value (enter as 0.0 - 1.0).") 
End If 
Loop Until (data1 <= 1 And data1 >= 0) 

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

Do 
If data2 > 1 Or data2 < 0 Then 
data2 = InputBox("Invalid input. What was the Settlement damage value (enter as 0.0 - 1.0).") 
End If 
Loop Until (data2 <= 1 And data2 >= 0) 

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

Do 
If data3 > 1 Or data3 < 0 Then 
data3 = InputBox("Invalid input. What was the Pre-Exisiting damage value (enter as 0.0 - 1.0).") 
End If 
Loop Until (data3 <= 1 And data3 >= 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 

ActiveDocument.Bookmarks("\Endofdoc").Select 
Selection.TypeParagraph 
+0

你要为从其他应用程序到MS Word中的内容?哪一个应用程序?并请显示你设置的引用的代码...你有什么样的问题,你期望你需要下载'Office Object Libraries 12,14和15' –

回答

0

工作,我已经得到了在Office 2010中运行代码,只有它引用版本14库。看起来代码已经存在了一段时间,并且旧的引用可以被删除。

要让它在不同的版本上工作,您必须将引用从早期绑定更改为后期绑定。这样,您无需为每个新版本添加来自VB编辑器的引用。

我可以使用此代码示例作为学生的Word宏的示例吗?

此外,如果你需要帮助后期绑定(这是一个老问题)让我知道。

参考早期绑定:http://word.mvps.org/faqs/interdev/earlyvslatebinding.htm