IT業界のすみっこ暮らし

ふと気がついたときの記録

VBでWord作成

f:id:papamau:20161222085025p:plain

'Add Reference > Interop.Microsoft.Office.Interop.Word.dll
'Imports Microsoft.Office.Interop.Word

Dim mf As New Microsoft.Office.Interop.Word.Application
mf.Documents.Add()
Dim range As Object = mf.ActiveDocument.Range(Start:=0, End:=0)
range.InsertParagraph()

range.InsertParagraph()   'inserting paragraph
range.Text = "Test" ' Inserting text
range.InsertParagraph()

range.InlineShapes.AddPicture("d:\test.png")   'add picture
' Following try catch is long just to make sure interop objects are disposed 
Try
    mf.ActiveDocument.SaveAs("d:\test1.docx")
    mf.ActiveDocument.Close()
    mf.Quit()

   'メモリ開放
    System.Runtime.InteropServices.Marshal.ReleaseComObject(mf)
    System.Runtime.InteropServices.Marshal.ReleaseComObject(range)
    GC.Collect()
    GC.WaitForPendingFinalizers()
Catch
    range = Nothing
    mf = Nothing
End Try
広告を非表示にする