Aggiungi Word in un campo OLE










Option Explicit

Dim oDocument As Object

Private Sub Command1_Click()
On Error GoTo Err_Handler

' Create a new Word Document...

OLE1.CreateEmbed vbNullString, "Word.Document"

' Add some text to the document. The OLE.Object

' property returns the document object...

Set oDocument = OLE1.object


oDocument.Content.Select
With oDocument.Application.Selection

' Add a heading at the top of the document...

.Style = oDocument.Styles("Heading 1")
.Font.Color = &HFF0000
.TypeText "Blue Sky Airlines"
.ParagraphFormat.Alignment = 1 '[wdAlignParagraphCenter]
.TypeParagraph
.TypeParagraph

' Now add some text...

.TypeText "Dear Mr. Smith,"
.TypeParagraph
.TypeParagraph
.TypeText "Thank you for your interest in our current fares " & _
"from Oakland to Sacramento. We guarantee to be " & _
"the lowest price for local flights, or we'll " & _
"offer to make your next flight FREE!"
.TypeParagraph
.TypeParagraph
.TypeText "The current fare for a flight leaving Oakland " & _
"on October 4, 1999 and arriving in Sacramento " & _
"the same day is $54.00."
.TypeParagraph
.TypeParagraph
.TypeText "We hope you will choose to fly Blue Sky Airlines."
.TypeParagraph
.TypeParagraph
.TypeText "Sincerely,"
.TypeParagraph
.TypeParagraph
.TypeParagraph
.TypeText "John Taylor"
.TypeParagraph
.Font.Italic = True
.TypeText "Regional Sales Manager"
.TypeParagraph

End With

' Zoom to see entire document...

OLE1.SizeMode = 3
OLE1.DoVerb -1

Command1.Enabled = False
Command2.Enabled = False
Command3.Enabled = True
Exit Sub


Err_Handler:
MsgBox "An error occurred: " & Err.Description, vbCritical
End Sub

Private Sub Command2_Click()
On Error GoTo Err_Handler

' Create an embedded object using the data

' stored in Test.doc.<?xm-insertion_mark_start author="v-thomr" time="20070327T040719-0600"?> If this code is run in Microsoft Office

' Word 2007, change the file name to Test.docx.<?xm-insertion_mark_end?><?xm-deletion_mark author="v-thomr" time="20070327T040717-0600" data=".."?>

OLE1.CreateEmbed "C:\Test.doc"

Command1.Enabled = False
Command2.Enabled = False
Command3.Enabled = True
Exit Sub

Err_Handler:
MsgBox "The file 'C:\Test.doc' does not exist" & _
" or cannot be opened.", vbCritical
End Sub

Private Sub Command3_Click()
On Error Resume Next

' Delete the existinf test file (if any)...

Kill "C:\Test.doc"

' Save the file as a native Word DOC file...

oDocument.SaveAs "C:\Test.doc"
Set oDocument = Nothing

' Close the OLE object and remove it...

OLE1.Close
OLE1.Delete

Command1.Enabled = True
Command2.Enabled = True
Command3.Enabled = False
End Sub


Private Sub Form_Load()
Command1.Caption = "Create"
Command2.Caption = "Open"
Command3.Caption = "Save"
Command3.Enabled = False
End Sub










( aggiungiwordinuncampoole.html )- by Paolo Puglisi - Modifica del 17/12/2023