Yeni bir Paket Programlar dersi makalesi ile birlikteyiz.,
Bilindiği gibi, 31 Mart 2019 tarihinde yerel seçimler olacaktır. Özellikle programcılar seçim sonuçlarını grafiklerle göstermeyi çok severler. Seçimlerde televizyonların gösterdikleri grafikler bazen beğeni yarışmasına dönüşmektedir. Word programında da seçim sonuçlarını grafiklerle gösterebilir, görsellik katabilirsiniz.
Vakit kaybetmeden uygulamamıza geçelim. Yeni bir belge açalım. Belgemize aşağıdaki gibi bir tablo ekleyelim.

Burada boş olan satıra grafiğimiz gelecektir. Grafiğimiz tabloya ait olan Parti adları ve oy oranları ile oluşacaktır. Oy sayıları ise, kullanılan oyun partilerin aldığı oy onanına göre dağıtılması sonucu bulunacaktır. Kodlarımızı yazalım.
Private Sub CommandButton1_Click()
Dim nGrafik As Chart
Dim grafikExcel As Excel.Worksheet
ActiveDocument.Tables(1).Cell(5, 1).Select
Set nGrafik = ActiveDocument.InlineShapes.AddChart.Chart
Set grafikExcel = nGrafik.ChartData.Workbook.Worksheets(1)
grafikExcel.ListObjects(1).Resize grafikExcel.Range("A1:B6")
grafikExcel.Range("b1").FormulaR1C1 = "Partiler"
grafikExcel.Range("A2").FormulaR1C1 = "A Partisi"
grafikExcel.Range("A3").FormulaR1C1 = "B Partisi"
grafikExcel.Range("A4").FormulaR1C1 = "C Partisi"
grafikExcel.Range("A5").FormulaR1C1 = "D Partisi"
grafikExcel.Range("A6").FormulaR1C1 = "E Partisi"
grafikExcel.Range("B2").FormulaR1C1 = ActiveDocument.ContentControls(4).Range.Text
grafikExcel.Range("B3").FormulaR1C1 = ActiveDocument.ContentControls(6).Range.Text
grafikExcel.Range("B4").FormulaR1C1 = ActiveDocument.ContentControls(8).Range.Text
grafikExcel.Range("B5").FormulaR1C1 = ActiveDocument.ContentControls(10).Range.Text
grafikExcel.Range("B6").FormulaR1C1 = ActiveDocument.ContentControls(12).Range.Text
nGrafik.ChartData.Workbook.Application.Quit
nGrafik.Legend.Format.Line.Visible = msoCTrue
With nGrafik.ChartArea.Format.Fill
.Visible = msoTrue
.Solid
.ForeColor.ObjectThemeColor = wdThemeColorAccent1
End With
nGrafik.HasTitle = True
With nGrafik.ChartTitle
.Characters.Font.Italic = True
.Characters.Font.Size = 18
.Characters.Font.Color = RGB(0, 0, 100)
.Text = "A İline Ait Yerel Seçim Sonuçları"
End With
End Sub
Private Sub Document_ContentControlOnExit(ByVal CC As ContentControl, Cancel As Boolean)
Dim strTemp As String
Dim hata1 As Boolean, hata2 As Boolean, hata3 As Boolean
Dim hata4 As Boolean, hata5 As Boolean, Dogrula As Boolean
Dim i As Long
Dim oRng As Word.Range
strTemp = CC.Range.Text
Dogrula = False
If Application.Version < "14.0" Then Main.SetDeveloperTabActive
Select Case CC.Title
Case "Secmen"
If CC.ShowingPlaceholderText = True Or strTemp = "" Then
MsgBox "Bu alan boş geçilemez"
Cancel = True
Else
secmen = Val(ActiveDocument.ContentControls(1).Range.Text)
oy = Val(ActiveDocument.ContentControls(2).Range.Text)
If oy <> "" Then
koran = (oy / secmen) * 100
ActiveDocument.ContentControls(3).Range.Text = Format(koran, "% ###")
End If
End If
Case "Oy"
If CC.ShowingPlaceholderText = True Or Not IsNumeric(strTemp) Then
MsgBox "Bu alan boş geçilemez"
Cancel = True
Else
secmen = Val(ActiveDocument.ContentControls(1).Range.Text)
oy = Val(ActiveDocument.ContentControls(2).Range.Text)
If secmen <> "" Then
koran = (oy / secmen) * 100
ActiveDocument.ContentControls(3).Range.Text = Format(koran, "% ###")
End If
End If
Case "Oy1"
If CC.ShowingPlaceholderText = True Or Not IsNumeric(strTemp) Then
MsgBox "Bu alan boş geçilemez"
Cancel = True
Else
oy1 = Val(ActiveDocument.ContentControls(5).Range.Text)
oy = Val(ActiveDocument.ContentControls(2).Range.Text)
If oy <> "" Then
oran1 = (oy1 / oy) * 100
ActiveDocument.ContentControls(4).Range.Text = Format(oran1, "% ###")
End If
End If
Case "Oy2"
If CC.ShowingPlaceholderText = True Or Not IsNumeric(strTemp) Then
MsgBox "Bu alan boş geçilemez"
Cancel = True
Else
oy2 = Val(ActiveDocument.ContentControls(7).Range.Text)
oy = Val(ActiveDocument.ContentControls(2).Range.Text)
If oy <> "" Then
oran2 = (oy2 / oy) * 100
ActiveDocument.ContentControls(6).Range.Text = Format(oran2, "% ###")
End If
End If
Case "Oy3"
If CC.ShowingPlaceholderText = True Or Not IsNumeric(strTemp) Then
MsgBox "Bu alan boş geçilemez"
Cancel = True
Else
oy3 = Val(ActiveDocument.ContentControls(9).Range.Text)
oy = Val(ActiveDocument.ContentControls(2).Range.Text)
If oy <> "" Then
oran3 = (oy3 / oy) * 100
ActiveDocument.ContentControls(8).Range.Text = Format(oran3, "% ###")
End If
End If
Case "Oy4"
If CC.ShowingPlaceholderText = True Or Not IsNumeric(strTemp) Then
MsgBox "Bu alan boş geçilemez"
Cancel = True
Else
oy4 = Val(ActiveDocument.ContentControls(11).Range.Text)
oy = Val(ActiveDocument.ContentControls(2).Range.Text)
If oy4 <> "" Then
oran4 = (oy4 / oy) * 100
ActiveDocument.ContentControls(10).Range.Text = Format(oran4, "% ###")
End If
End If
Case "Oy5"
If CC.ShowingPlaceholderText = True Or Not IsNumeric(strTemp) Then
MsgBox "Bu alan boş geçilemez"
Cancel = True
Else
oy5 = Val(ActiveDocument.ContentControls(13).Range.Text)
oy = Val(ActiveDocument.ContentControls(2).Range.Text)
If oy5 <> "" Then
oran5 = (oy5 / oy) * 100
ActiveDocument.ContentControls(12).Range.Text = Format(oran5, "% ###")
End If
End If
End Select
lbl_Exit:
Exit Sub
End Sub
Formda yer alan bilgileri girelim. Tablomuzun sayısal sonuçları aşağıdaki gibi olacaktır.

Şimdi de Oluştur butonuna basarak A iline ait yerel seçim sonuçlarını grafikle gösterelim.

Görüldüğü gibi, Word programında hem girişleri hem bu girişlere ait matematiksel işlemleri ve en önemlisi de grafiği dinamik bir şekilde belgemizde oluşturmuş olduk.
Uygulamayı buradan indirebilirsiniz.