ホーム

moiblog

読書メモ:2012第1週

  • 投稿者: ntoh(夫)
  • 2012年1月7日 7:04 PM
  • 未分類

年末年始の長期休暇明けのせいか、活字を見ると疲れるので初週はこんなペースで。

漫画は、あいかわらずハイペースで読み進めております!ビシッ!


読書メモ:20114Q

  • 投稿者: ntoh(夫)
  • 2011年12月27日 9:00 PM
  • 未分類

年末に読んだ本

VisualBasic.net から PowerPointを弄くる方法

  • 投稿者: ntoh(夫)
  • 2011年10月13日 7:37 PM
  • Computer | Tech

vb.netからpowerpointを制御するための有用な情報リンク+自分で書いたコードのメモ

C#のページもあるけど、基本的にPIA部分のコードは変わらないので後で使えそうなのはメモしておく。

Visual Studio .NET を使用して Microsoft Office ソリューションを開発する
http://support.microsoft.com/kb/311452/ja

オートメーションを使用して PowerPoinプレゼンテーションの作成および表示を行う方法
http://support.microsoft.com/kb/303717/JA

オートメーションを使用して、Visual Basic .NET で Office ドキュメントのプロパティを取得および設定する方法
http://support.microsoft.com/kb/303294/ja

Visual Basic .NET で PowerPoint のイベントを処理する方法
http://support.microsoft.com/kb/308330/ja

WebBrowserコントロール経由でアプリ内にPPTを表示する方法
http://support.microsoft.com/kb/304643/ja

http://www.dotnetspider.com/resources/29664-Power-point-presentation-VB-net.aspx

テキストを走査するクラス他(C#sample)
http://d.hatena.ne.jp/torasenriwohashiru/?of=10
http://d.hatena.ne.jp/torasenriwohashiru/searchdiary?word=*[PowerPoint]

ロゴスウェア システム開発部のblog(C#sample)
http://blog3.logosware.com/archives/tag/%e3%83%91%e3%83%af%e3%83%bc%e3%83%9d%e3%82%a4%e3%83%b3%e3%83%88

 

上記に追加で個人的メモ

Powerpointをひらく

Dim ppt_pres As PowerPoint.Presentation
Dim Ppt_app As New PowerPoint.Application

Ppt_app = New PowerPoint.Application
Ppt_app.Visible = MsoTriState.msoTrue
ppt_pres = Ppt_app.Presentations.Open("c:\sample.ppt")

現在ひらいているスライドを選択(slideshowモードだと例外が発生するので注意)

Dim slide
slide = Ppt_app.ActiveWindow.View.Slide

楕円の追加(塗りつぶしなし)

Dim oShape As PowerPoint.Shape
oShape = slide.Shapes.addshape(MsoAutoShapeType.msoShapeOval, 520, 36.0#, 160, 150)
oShape.Fill.Visible = MsoTriState.msoFalse
oShape.Line.ForeColor.RGB = &HFF
oShape.Line.Weight = 5.0#
oShape.Line.DashStyle = MsoLineDashStyle.msoLineSolid

その他のシェイプは、

1)PowerPointでマクロを記録開始する
2)任意のShapeをスライドに追加、また書式の変更を実施
3)マクロの記録停止
4)マクロのソースを見ると必要なコードが表示されている

ゴミが入ることと、若干コードの修正は必要ですがリファレンス等で調べるよりは楽です。

テキストボックスの追加

oShape = slide.Shapes.AddTextbox(1, 500, 20, 200, 100)
oShape.TextFrame.TextRange.Font.Bold = MsoTriState.msoFalse
oShape.TextFrame.TextRange.Font.Name = "HGP明朝B"
oShape.TextFrame.TextRange.Text = "済"
oShape.TextFrame.TextRange.Font.Size = 128
oShape.TextFrame.TextRange.Font.Color.RGB = &HFF

テキストボックスの追加(背景色、透過有り)

oShape = slide.Shapes.AddTextbox(1, 500, 190, 200, 60)
oShape.TextFrame.TextRange.Text = "sampleword"
oShape.TextFrame.TextRange.Font.Size = 12
oShape.TextFrame.TextRange.Font.Color.RGB = &HFF
oShape.TextFrame.TextRange.Font.Name = "Arial Black"
oShape.Fill.Visible = MsoTriState.msoTrue
oShape.Fill.Solid()
oShape.Fill.ForeColor.RGB = &HFFFFFF
oShape.Fill.Transparency = 0.25#

特定の文字列があるシェイプをさがす(slideには事前に走査対象のslideに設定しておく)には、hastextframeをみてtrueの場合に、textrange.textを取得すればよい。

Dim shape
Dim shape_text as string
 For Each shape In slide.Shapes

    If shape.HasTextFrame Then
      shape_text = shape.textframe.textrange.text
      If shape_text.IndexOf("さがしたい文字列") >=0 Then (みつかった場合の処理)
    End If
 Next

shapeがTableの場合は、各セル内のtextframeの内容を順に見る必要がある。
shape.hastableがtrueの場合は、さらに下記のコードを追加する。

If shape.HasTable Then
   Dim colx, lowy, cflag
   colx = 0
   For colx = 0 To shape.table.columns.count
      For lowy = 0 To shape.table.rows.count
Try
   cflag = shape.table.cell(colx, lowy).shape.HasTextFrame
Catch ex As Exception
   cflag = False
End Try
If cflag Then
   Try
      shape_text = shape.table.cell(colx, lowy).shape.textframe.textrange.text
      If shape_text.IndexOf("さがしたい文字列") >= 0 Then (みつかった場合の処理)
   Catch ex As Exception
      shape_text = ""
   End Try
End If
      Next
   Next
End If

セルの結合等にて存在しない行・列のセル等を参照した場合には例外が発生するため、例外の発生の有無をみて処理を分岐させているが、もっとよい方法があるかもしれないので、あまりここは真似しないように。

1 2 3 4 5 6 7 15

ホーム

RSS ntoh-Twitter
RSS aquaqu-Twitter
アクセスの多いページ

ページの上部に戻る