I'm working with Visual Basic (2022) and Word (2013). I have a "Building Blocks.dotx" file in which I've add some QuickParts like 'sua' (BuildingBlock Name) for 'Signos de uncoartrosis'. All of them were added to 'General' category. I would like to implement a solution without word application. For this I would like to save all of my QuickParts on a SQL Database.
So, the first step would be to read the QuickParts on my template and showed them into a RTB control. That's because some text are in bold fonts.
Rigth now I can read 255 characters of the BuildingBlock value, without RTF format and save it to my table:
Dim wordApp As Word.Application = Nothing
Dim document As Word.Document = Nothing
Dim objBB As Word.BuildingBlock = Nothing
Dim objTemplate As Word.Template
Dim Cn As New SqlConnection("Data Source=xxx;Initial Catalog=xxxxx;User Id=sa;Password=xxxxx;")
Cn.Open()
Try
'create new instance
wordApp = New Word.Application() With {.Visible = False}
objTemplate = wordApp.Templates.Item(1)
Dim cnt As Integer
Dim cant As Integer = 0
For cnt = 1 To objTemplate.BuildingBlockEntries.Count
objBB = objTemplate.BuildingBlockEntries.Item(cnt)
'objBB.
If objBB.Category.Name = "General" Then
cant += 1
txtCodigo.Text = objBB.Name
rtbValor.Text = objBB.Value
Using cmd As SqlCommand = New SqlCommand("INSERT INTO AutoTexts (Nombre, Texto) VALUES (@Nombre, @rtfData)", Cn)
cmd.Parameters.Add("@Nombre", SqlDbType.VarChar).Value = objBB.Name
cmd.Parameters.Add("@rtfData", SqlDbType.NVarChar).Value = objBB.Value
cmd.ExecuteNonQuery()
End Using
End If
Next
Debug.Print(cant)
Cn.Close()
Catch ex As Exception
'ToDo: add desired code
Debug.WriteLine($"Error (ModifyWordDoc) - {ex.Message}")
Finally
If document IsNot Nothing Then
document.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
End If
If wordApp IsNot Nothing Then
wordApp.Quit()
End If
End Try
End Sub
After that I was trying another approach. I try to insert de BB in a Word document, the copy and paste formatted text into a RTB. That works ok:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim wordApp As Word.Application = Nothing
Dim document As Word.Document = Nothing
Dim objBB As Word.BuildingBlock = Nothing
Dim objBBT As Word.BuildingBlockType = Nothing
Dim objTemplate As Word.Template = Nothing
wordApp = New Word.Application() With {.Visible = True}
document = wordApp.Documents.Add(Template:="D:\SistemaClaudio\LeeTemplate-Autotextos\BB.dotx")
Dim paramBBType As Word.WdBuildingBlockTypes = Word.WdBuildingBlockTypes.wdTypeQuickParts
Dim paramBBCategory As String = "General"
Dim paramBBName As String = "codopat"
wordApp.Templates.LoadBuildingBlocks()
wordApp.Templates.LoadBuildingBlocks()
objTemplate = wordApp.Templates(2)
Dim TemplateName As String = objTemplate.Name
objBBT = objTemplate.BuildingBlockTypes.Item(paramBBType)
objBB = objTemplate.BuildingBlockTypes.Item(paramBBType).Categories.Item(paramBBCategory).BuildingBlocks.Item(paramBBName)
rtbValor.Text = objBB.Value
objBB.Insert(document.Sections(1).Range)
End Sub
Private Sub rtbValor_KeyDown(sender As Object, e As KeyEventArgs) Handles rtbValor.KeyDown
If (e.Control = True And e.KeyCode = Keys.V) Then
Dim s As String = Clipboard.GetDataObject().GetData(DataFormats.Rtf)
rtbValor.Rtf = s
e.Handled = True
End If
End Sub
I press the command button and the full BB text is written on a word document, then I copied that and Paste into RTB and from there I could read it and save into my database table. I could automate the CTRL-C and CTRL-V steps programmatically, but I'm wondering how to get RTF format from a QuickPart building block.