OpenXML how to make text bold - vb.net

I use openXML to write a word document containing my text. Now I want to make a part of the text bold. I however fail to find a solution to make my text bold, since I can't figure it out. My code is:
Dim FilePath As String = "C:\\MyFolder\\MyFile.docx"
'Create a document to work in
Using wordDocument As WordprocessingDocument = WordprocessingDocument.Create(FilePath, WordprocessingDocumentType.Document)
' Add a main document part.
Dim mainPart As MainDocumentPart = wordDocument.AddMainDocumentPart()
' Create the document structure and add some text.
mainPart.Document = New Document()
Dim body As Body = mainPart.Document.AppendChild(New Body())
Dim para As Paragraph = body.AppendChild(New Paragraph())
Dim runNormal As Run = para.AppendChild(New Run())
Dim runBold As Run = para.AppendChild(New Run())
runBold.RunProperties.Bold()
I want to have two instances: one for normal text runNormal and one for the bold text runBold. Sadly the runBold does not work, while the runNormal works.

Refer to the following code.
WordprocessingDocument doc = WordprocessingDocument.Create(path, WordprocessingDocumentType.Document)
Dim mainDocumentPart As MainDocumentPart = doc.AddMainDocumentPart()
mainDocumentPart.Document = New Document()
Dim body As Body = mainDocumentPart.Document.AppendChild(New Body())
Dim para As Paragraph = body.AppendChild(New Paragraph())
Dim run As Run = para.AppendChild(New Run())
Dim runProperties As RunProperties = run.AppendChild(New RunProperties())
Dim bold As Bold = New Bold()
bold.Val = OnOffValue.FromBoolean(True)
runProperties.AppendChild(bold)
run.AppendChild(New Text("Welcome!!!!!"))
doc.MainDocumentPart.Document.Save()

Ok I figured it out how it works, so here the solution for everyone:
If you are writing the first time in bold in your text, you need to use this:
Dim runProperties As RunProperties = run.AppendChild(New RunProperties(New Bold()))
run.AppendChild(New Text("I am written in bold!"))
Now if you want to write again in bold, add this code:
run.AppendChild(New RunProperties(New Bold()))
run.AppendChild(New Text("I am also written in bold!"))
That's it. To summarize, here the complete code:
Using wordDocument As WordprocessingDocument = WordprocessingDocument.Create(DOCXPath, WordprocessingDocumentType.Document)
' Add a main document part.
Dim mainPart As MainDocumentPart = wordDocument.AddMainDocumentPart()
' Create the document structure
mainPart.Document = New Document()
Dim body As Body = mainPart.Document.AppendChild(New Body())
Dim para As Paragraph = body.AppendChild(New Paragraph())
Dim run As Run = para.AppendChild(New Run())
' Write in bold (only works in the first line which you write)
Dim runProperties As RunProperties = run.AppendChild(New RunProperties(New Bold()))
run.AppendChild(New Text("I am written in bold!"))
' If you want to write a second time in bold, just add the next line and afterwards your text to be written in bold.
run.AppendChild(New RunProperties(New Bold()))
run.AppendChild(New Text("I am also written in bold!"))

Related

Kofax power PDF VBA dll reference

Just wanted to add information on the below post (but seems I cant).
https://safeweb.hsbcpb.com/https://stackoverflow.com/questions/39269911/nuance-power-pdf-automation-using-excel-vba-macro
The reference is now:
"C:\Program Files (x86)\Kofax\Power PDF 31\bin\PDFCore8_x64.dll"
even if called powerPDF and the documentation said so to create the with "powerdpdf.app", the following code work fine for me (just in case someone was looking for a solution like I did).
Function copy_pdf_values_wiht_nuance(path As String) As String
Dim PDFApp As Object
Dim dvOpenDoc As Object
Dim ddDoc As Object
Set PDFApp = CreateObject("nuancePDF.App")
Set dvOpenDoc = CreateObject("nuancePDF.DVDoc")
If dvOpenDoc.Open(path) = False Then
PDFApp.Exit
Exit Function
End If
Set ddDoc = dvOpenDoc.GetDDDoc()
PDFApp.MenuItemExecute ("SelectAll")
PDFApp.MenuItemExecute ("Copy")
PDFApp.Exit
'Set ddPage = ddDoc.AcquirePage(0)
'Set hl = CreateObject("NuancePDF.HiliteList")
'hr = hl.Add(12, 1)
'hr = hl.Add(52, 2)
'Set ts = ddPage.CreatePageHilite(hl)
'hr = dvOpenDoc.SetTextSelection(ts)
'dvOpenDoc.ShowTextSelect
Dim clipboard As MSForms.DataObject
Dim strContents As String
Set clipboard = New MSForms.DataObject
clipboard.GetFromClipboard
strContents = clipboard.GetText
copy_pdf_values_wiht_nuance = strContents
End Function
Copy and paste content of a PDF, having had the wrong reference.

vbCFBitmap Variable Not defined

I am using MS-Access 2007 VBA.
I am attempting to convert a pdf to an image. I found this chunk of code online, but they failed to provide all the references. My compile is failing on vbCFBitmap. Does anyone know where this reference comes from?
Dim MyAcro As New AcroApp
Dim MyPDF As New AcroPDDoc
Dim MyPage As AcroPDPage
Dim MyPt As acrobat.AcroPoint
Dim MyRect As AcroRect
Dim MyData As DataObject
Dim strPathString As String
Dim MyPath As String
Dim SaveToPath As String
Dim mysavepath As String
MyPath = "\\spfs1\stone\Long Term Share\gentex_ppaps\gentex_ppaps_raw\Supplier Request Number 3034910, Gentex Part Number 345-2120-000 Revision (003).pdf"
mysavepath = "C:\out"
' open the PDF
MyPDF.Open (MyPath)
Set MyPage = MyPDF.AcquirePage(0)
' Convert Point to Twips for document
Set MyPt = New AcroPoint
'Define the rectangle that contains the PDF form
Set MyRect = New acrobat.AcroRect
MyRect.Top = 0
MyRect.Left = 0
MyRect.Right = MyPt.x
MyRect.bottom = MyPt.y
' Copy the PDF image to the clip board
Call MyPage.CopyToClipboard(MyRect, MyRect.Left, MyRect.Top, 100)
' Capture image from clip board to data object
Set MyData = Clipboard.GetData(vbCFBitmap)
'Save the data object
SavePicture MyData, mysavepath
' Clean up
Set MyAcro = Nothing
Set MyPDF = Nothing
Set MyPage = Nothing
Set MyPt = Nothing
Set MyRect = Nothing
Set MyData = Nothing
That's likely VB6 code, not VBA.
vbCFBitmap is a system global, and thus not imported using any references.
However, that's just a copy of the Windows Standard Clipboard Formats, thus vbCFBitmap is equal to 2. You can use 2 instead.

Populate PDF Combo Box Via VBA

I have the below code to populate a combobox in a PDF document. As far as I can tell, the code is correct, according to the Acrobat Forms API Reference
However the code fails on the PopulateListOrComboBox call with this error:
Method 'PopulateListOrComboBox' of object 'IField' failed
Dim acroApp As Acrobat.acroApp
Set acroApp = New Acrobat.acroApp
Dim myForm As Acrobat.AcroAVDoc
Set myForm = New Acrobat.AcroAVDoc
Dim bOK As Boolean
bOK = myForm.Open("C:\Users\sholtzman\downloads\wordFormTest.pdf", "temp")
Dim theRealForm As AFORMAUTLib.AFormApp
Set theRealForm = New AFORMAUTLib.AFormApp
Dim pdField As AFORMAUTLib.Field
Set pdField = theRealForm.Fields.Add("triaCoverage", "combobox", 0, 10, 20, 100, 200)
Dim items(2) As String
items(0) = " "
items(1) = "Accept"
items(2) = "Reject"
pdField.PopulateListOrComboBox items
Furthermore, I tested this piece of code as well to try to fill it, but it also failed with the error:
Automation error Unspecified error
Dim myPDForm As Acrobat.AcroPDDoc
Set myPDForm = myForm.GetPDDoc
Dim jso As Object
Set jso = myPDForm.GetJSObject
jso.getField("triaCoverage").setItems(1) = " "
Lastly, when I save and close the document after adding the combobox, I can go in
and set list values manually. Any ideas how I can get this to work through code?
I did some more research and have found one answer just now by replacing the line:
jso.getField("triaCoverage").setItems(1) = " "
with
jso.getField("triaCoverage").setItems(items)
However, I prefer to use the PopulateListOrComboBox method if at all possible, so i will leave this unanswered for some time.

Closing word when using interop library - VB.net

I have successfully been able to convert a word doc to pdf - but I can't get the winword process to end.
Dim wordApplication As Object = New Microsoft.Office.Interop.Word.Application
Dim wordDocument As Object
wordApplication.displayalerts = Word.WdAlertLevel.wdAlertsNone
wordDocument = New Microsoft.Office.Interop.Word.Document
'wordDocument = Nothing
Dim outputFilename As String
Dim filename As String
filename = "c:\TestInvoice.doc"
Try
wordDocument = wordApplication.Documents.Open(filename, ReadOnly:=False)
outputFilename = System.IO.Path.ChangeExtension(filename, "pdf")
If Not wordDocument Is Nothing Then
wordDocument.ExportAsFixedFormat(outputFilename, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF, False, Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen, Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument, 0, 0, Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent, True, True, Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks, True, True, False)
End If
'Threading.Thread.Sleep(3000)
If File.Exists(System.IO.Path.ChangeExtension(filename, "pdf")) Then
MessageBox.Show(System.IO.Path.ChangeExtension(filename, "pdf"))
End If
'wordDocument = Nothing
'wordApplication.Documents(filename).Close(Word.WdSaveOptions.wdDoNotSaveChanges)
'wordDocument.dispose()
'wordApplication.quit(False)
wordDocument.close()
wordApplication.application.quit(False)
wordApplication = Nothing
I've tried pretty much everything and have been scratching my head for the past few days - can you point me in the right direction?
Have you tried a Using statement?
Try wrapping everything in this.
Using wordApplication As Object = New Microsoft.Office.Interop.Word.Application
End Using

CatiaV5 macro: how to insert background view?

I'm making a macro to automatically open a new drawing on the correct sheet format with filled in title block but I can't seem to figure out how to insert a pre-made .CATDrawing in the same way the following option in the page setup dialog box would:
see here: https://i.imgur.com/goClGIh.png
my current progress looks like this:
Sub CATMain()
Dim partDoc As PartDocument
Set partDoc = CATIA.ActiveDocument
Dim myParam As Parameter
Set myParam = partDoc.Part.parameters.Item("Description")
Dim documents1 As Documents
Set documents1 = CATIA.Documents
Dim MyDrawingDoc As DrawingDocument
Set MyDrawingDoc = documents1.Add("Drawing")
MyDrawingDoc.Standard = catISO
Dim MyDrawingSheets As DrawingSheets
Set MyDrawingSheets = MyDrawingDoc.Sheets
Dim MyDrawingSheet As DrawingSheet
Set MyDrawingSheet = MyDrawingSheets.Item("Sheet.1")
MyDrawingSheet.PaperSize = catPaperA3
MyDrawingSheet.[Scale] = 1#
MyDrawingSheet.Orientation = catPaperLandscape
**CATIA.StartCommand "Page Setup"**
Dim dView As DrawingViews
Set dView = MyDrawingSheet.Views
dView.Item("Background View").Activate
AddTextWithLinkedParameter dView, 20, 20, myParam
End Sub
Sub AddTextWithLinkedParameter(dViewToContainTheText As DrawingViews, xPos, yPos, Optional param As Parameter)
Dim dtext As DrawingText
Set dtext = dViewToContainTheText.ActiveView.Texts.Add("", xPos, yPos)
If Not param Is Nothing Then
dtext.InsertVariable 0, 0, param
End If
End Sub
This line here
CATIA.StartCommand "Page Setup"
should be replaced by a sequence of codes that does the same thing as clicking the options would as shown in the image above.
In my experience, I think you are better off writing a script to draw a title block rather than using a template. This way it's more flexible with regards to changing sheet size and orientation. You can also update the titleblock if sheet size and orientation changes. This is also how catia does titleblocks with the catscript. I would avoid StartCommand as it's not inline with the script execution.
That being said. If you want to use a "template", then the best way to do that is to setup your template catDrawing and then your script will open the template as read-only, do what you need, and then the user will save-as. Avoid StartCommand if you can.
Directly opening the .CATdrawing template has the same result.
One can do this by using the follwing code:
Dim documents1 As Documents
Set documents1 = CATIA.Documents
Dim mydrawingdoc As DrawingDocument
Set mydrawingdoc = documents1.Open("Path\Template.CATDrawing")