簡體   English   中英

我如何重寫以下MS Word VBA宏以與Word 2007/2010/2013兼容

[英]How can I rewrite the following MS Word VBA macro to be compatible with Word 2007/2010/2013

我希望使我的MS Word VBA宏與Word 2007、2010和2013兼容,而不必在所有計算機上下載MS Office對象庫12、14和15。 任何類型的我怎么能做到這一點? 我的代碼如下,並且似乎需要對象庫12和14或15。它僅適用於12,14或15。

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  

我已經在Office 2010中運行了該代碼,它僅引用了版本14庫。 似乎代碼已經存在了一段時間,可以刪除較舊的引用。

為了使其能夠在不同版本上工作,您必須將引用從早期綁定更改為后期綁定。 這樣,您無需為每個新版本從VB編輯器中添加引用。

我可以使用此代碼示例作為學生的Word宏示例嗎?

另外,如果您需要后期裝訂方面的幫助(這是一個老問題),請告訴我。

早期綁定參考: http : //word.mvps.org/faqs/interdev/earlyvslatebinding.htm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM