How do I center a paragraph in Word using vb.net? - vb.net

I am using Visual Studio 2015 and coding in vb.net and importing Microsoft.Office.Interop.Word . I am using the following code to create a one page Word document with only two lines. How can I center, both vertically and horizontally these two lines? Also, is there a way to put both lines, with a line break in between, in one paragraph rather than using two? I am very new to this type of programming so please be specific. Thanks.
Private Sub CreateTitlePage2()
Dim wdApp As Microsoft.Office.Interop.Word.Application = New Microsoft.Office.Interop.Word.Application
Dim wdDoc As Microsoft.Office.Interop.Word.Document = New Microsoft.Office.Interop.Word.Document
Dim wdPara1 As Microsoft.Office.Interop.Word.Paragraph
Dim wdPara2 As Microsoft.Office.Interop.Word.Paragraph
wdDoc.Application.Visible = False
wdPara1 = wdDoc.Content.Paragraphs.Add
wdPara1.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter
wdPara1.Range.Font.Bold = True
wdPara1.Range.Text = "BINDER DOCUMENT"
wdPara1.Range.InsertParagraphAfter()
wdPara2 = wdDoc.Content.Paragraphs.Add
wdPara2.Format.SpaceBefore = WdVerticalAlignment.wdAlignVerticalCenter
wdPara2.Range.Font.Bold = True
wdPara2.Range.Text = "Created: " + formattedDate2
wdPara2.Range.InsertParagraphAfter()
wdDoc.SaveAs(binderNameDoc)
wdDoc.Close()
wdApp.Quit()
End Sub

#Ross: It would help if you'd describe HOW it's "not working". However...
WdVerticalAlignment is not valid applied to a paragraph object, I'm surprised you're not getting a compiler error. See https://msdn.microsoft.com/en-us/library/aa224305(v=office.11).aspx.
If you want to center something vertically on the page then it must be done via the PageSetup object and then it will apply to the entire SECTION. See https://msdn.microsoft.com/en-us/library/office/ff838676.aspx?f=255&MSPPError=-2147217396
If you document is really only the one page, as you say, then you don't need to worry about the SECTION part as the document will have only the one.
RE Line break: Insert ANSI 11 character (vbVerticalTab) for a line break (what you get when pressing Shift+Enter in the Word application).

Related

Creating new Section in onenote using VBA

Hi I have searched through the internet looking for examples of creating a new onenote section and I can't find the right example for me to understand. The closest I can get to is using .OpenHierarchy function but I'm still very new to it and I couldn't get the parameters right.
I'm currently working on an OCR marco for multiple PDF file. Everything is working fine till I realise that I'm creating huge waste files on my computer.
Here's the code I used to delete all the pages created in the section
Dim oneNote As OneNote14.Application
Dim secDoc As MSXML2.DOMDocument60
Set secDoc = New MSXML2.DOMDocument60
Dim secNodes As MSXML2.IXMLDOMNodeList
Set secNodes = secDoc.DocumentElement.getElementsByTagName("one:Section")
' Get the first section.
Dim secNode As MSXML2.IXMLDOMNode
Set secNode = secNodes(0)
Dim sectionName As String
sectionName = secNode.Attributes.getNamedItem("name").Text
Dim sectionID As String
sectionID = secNode.Attributes.getNamedItem("ID").Text
oneNote.DeleteHierarchy (sectionID)
oneNote.OpenHierarchy
End Sub
Deletehierarchy function deletes the entire section away leaving no section behind but my OCR macro requires at least one section to work.
Thanks for reading and thank you in advance!
oneNote.OpenHierarchy in vba does not allow bracklet and thats the issue that is causing the error.
solution:
oneNote.OpenHierarchy fileName, "", "New Section 1", 3

word vba: documents.add: put this new document on top

I am using VBA for MS Word. I created a macrofile (docm) to create a new word-document using documents.add....
I want to switch from my macro-document to my new created document on the screen:
Sub test()
Dim MacroDocument As Document
Set MacroDocument = ActiveDocument
Dim newDocument As Document
Set newDocument = Documents.Add
'try to show my macroDocument on the windows screen,
MacroDocument.Select
stop
' now to the new document
newDocument.Select
End Sub
Why doesn't it work?
Any ideas?
document.Select just selects the document but doesn't displays it.
Use MacroDocument.Activate and newDocumente.Activate instead.
When you add a document, the new document automatically becomes the active one and will replace the current one on your screen. Therefore, in most cases the task isn't to make it show but to keep the previous one on top. In such a case making the new document invisible is one of the options you might want to consider.
Set NewDocument = Documents.Add(Visible:=False)

VBA replace logo in Excel file

I have a little strange question. I used to have few reports worked upon daily.
All these are in Excel and had a logo of the company in all the sheets of each file.
However, now the company name is changed and hence a new logo needs to be replaced in place of the existing. Wanted to check if this replacement can be done with VBA.
I tried with the application.shapes method. But, was confused to proceed further.
Try this....
Sub ChangePicture(strNewPath As String)
Dim oOld As Picture
Dim oNew As Picture
Set oOld = ActiveSheet.Pictures(1)
Set oNew = ActiveSheet.Pictures.Insert(strNewPath)
oNew.Left = oOld.Left
oNew.Top = oOld.Top
oNew.Width = oOld.Width
oNew.Height = oOld.Height
oOld.Delete
End Sub

Word VSTO: Save a string as an AutoText value

I am porting a Word vba project that has some older WordBasic elements into a VSTO project using vb.net. One of the tasks I need to do is programmatically save a string, or the contents of a text box on a form, to an AutoText value in the Word template.
In the old project, this was easy. The command looked something like this:
WordBasic.SetAutoText "AT Name", strSomeValue, 0
Attempting this in vb.net:
'declarations
Public appWord As Word.Application
Public tplMyTpl As Word.Template
Public doc As Word.Document
'Get the template
appWord = Me.Application
doc = appWord.ActiveDocument
tplMyTpl = doc.AttachedTemplate
'try saving autotext
tplMyTpl.AutoTextEntries.Add("AT Name", strSomeValue)
does not work because the AutoTextEntries.Add method only accepts the value as a Word.Range. A type cast error is thrown at runtime with the code above. It would have to look something like:
dim sel as Word.Selection = appWord.Selection
tplMyTpl.AutoTextEntries.Add("AT Name", sel.Range)
Problem is I do not want to insert the string into my document, select it as a Range, save the AutoText entry then delete the text. That seems like extremely sloppy coding.
You can still use WordBasic in VSTO. Just call it through your Word.Application object reference, e.g.:
appWord.WordBasic.SetAutoText("AT Name", strSomeValue, 0)
Tested and working in Word 2013.

Word VSTO - Change the absolute positions' type?

I use the following VB.NET (VSTO) code to add a shape in MS-Word,
Dim app As Word.Application = Globals.ThisAddIn.Application
Dim doc As Word.Document = app.ActiveDocument
Dim left As Single = CSng(Convert.ToDouble(app.Selection.Information(Word.WdInformation.wdHorizontalPositionRelativeToPage)))
Dim top As Single = CSng(Convert.ToDouble(app.Selection.Information(Word.WdInformation.wdVerticalPositionRelativeToPage)))
Dim shape As Word.Shape = doc.Shapes.AddShape(1, left, top, 225.1F, 224.5F)
shape.Fill.BackColor.RGB = ColorTranslator.ToOle(Color.Transparent)
shape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
shape.Fill.Transparency = 0.0F
shape.Line.Transparency = 0.0F
shape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
What this code does is, it adds a rectangle shape at cursor point and makes it transparent (both background and line).
Now I like to change the absolute positions' type. To explain further, when you select the rectangle shape, then if you select the Ribbon tab Format > Position > More Layout Options... as shown in the image below,
It will open the following dialog,
In the above dialog I like to change Column and Paragraph marked by the red rectangles into the type Margin. How to do this by code?
Word provides a Macro recorder. You may use it to get the code generated for you in the background. Thus, you will find what properties and methods exactly should be used to get the job done. See Record or run a macro for more information.
The solution to this was solved in the link below,
https://social.msdn.microsoft.com/Forums/vstudio/en-US/e69584d7-24fe-4396-9a82-26b7dae02584/word-vsto-change-the-absolute-positions-type?forum=vsto