Word 2007 Script random font generator - ms-word

Would any one know or be able to help me get this to work with Word 2007.
This is a code snippet that is supposed to set styles randomly.
//Create instance of Word application
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
//Open an existing Word document, Make sure the file is exist in this path- otherwise the program fails.
Set objDoc = objWord.Documents.Open("C:\Scripts\Test.doc")
//Create instance of random object
Set objRandom = CreateObject("System.Random")
intLow = 1
intHigh = 5
//Get the text of word document
Set objRange = objDoc.Range()
//Get the charcters of the text
Set colCharacters = objRange.Characters
//Set different styles randomly
For Each strCharacter in colCharacters
intRandom = objRandom.Next_2(intLow,intHigh)
Select Case intRandom
Case 1 strCharacter.Font.Name = "Arial"
Case 2 strCharacter.Font.Name = "Times New Roman"
Case 3 strCharacter.Font.Name = "Courier New"
Case 4 strCharacter.Font.Name = "Forte"
End Select
Next

You should add to the program the following references:
Microsoft.Office.Interop.Word
Microsoft Office Object Library
You can do it by right ckick on project -> Add reference, from the GAC tab select the first reference I mentioned above, from the COM tab select the second one.

Related

Can I increase a number by a macro in LibreOffice Impress?

I want to make a counter with Libreoffice Impress.
Each Time I click on a rectangle, the number inside increases.
Dim Doc As Object
Dim Page As Object
Dim Rect As Object
Doc = ThisComponent
Page = Doc.drawPages.getByName("Test")
Rect = Page.getByIndex(0)
Rect.String = CInt(Rect.String) + 1
It works everywhere except in presentation mode.
Is it possible to make it work in presentation mode ?

How to amend the below code to replace the "text" searched, in this case "london" for text in a particular cell, "Registration Form" Sheet Cell C15

Below is the code I am using to find text within a data table and return all rows that contain that text on another sheet. The code works on a full data table with now empty cells, however I want to perform the same task but instead of giving the text to search within the code, I want this to be determined by the user in a given cell on the sheet.
Sub CopyOverBudgetRecords()
Dim StatusCol As Range
Dim Status As Range
Dim PasteCell As Range
Set StatusCol = Sheets("Data Sheet").Range("c8:l35")
For Each Status In StatusCol
If Sheets("Registration Form").Range("c22") = "" Then
Set PasteCell = Sheets("Registration Form").Range("c22")
Else
End If
If Status = "london" Then Range(Status.End(xlToLeft), Status.End(xlToRight)).Copy PasteCell
Next Status
End Sub
I tried
using - If Status = "london" Then Range(Status.End(xlToLeft), Status.End(xlToRight)).Copy PasteCell
Replacing "london" With Sheets ("Registration Form").Range("c15").value
So - If Status = Sheets ("Registration Form").Range("c15").value Then Range(Status.End(xlToLeft), Status.End(xlToRight)).Copy PasteCell. This didn't work.

Userform for Word with bookmarks and automated Crossreferences

so this is my first programming experience and I am pretty excited about it. However, I have been dealing with a problem:
Goal
My goal is to create a word document with an integrated Userform asking for Name, Title and Startdate at the beginning. The information shall then be reflected in the defined areas in the document.
Dim Name As Range
Set Name = ActiveDocument.Bookmarks("Name").Range
Name.Text = Me.TextBox1.Value
Dim Title As Range
Set Title = ActiveDocument.Bookmarks("Title").Range
Title.Text = Me.TextBox2.Value
Dim Startdate As Range
Set Startdate = ActiveDocument.Bookmarks("Startdate").Range
Startdate.Text = Me.TextBox3.Value
Me.Repaint
UserForm1.Hide
I could successfully integrate the Userform, assigning the information to the bookmarks. Since I can only use 1 bookmark I tried my luck with text properties but it does not work.
The next thing I tried was to apply 1 bookmark and several cross-references throughout the text, meaning that I have to add a command to the code that updates all cross-references automatically I tried it with sub updateAllFields()
Dim Name As Range
Set Name = ActiveDocument.Bookmarks("Name").Range
Name.Text = Me.TextBox1.Value
Dim Title As Range
Set Title = ActiveDocument.Bookmarks("Title").Range
Title.Text = Me.TextBox2.Value
Dim Startdate As Range
Set Startdate = ActiveDocument.Bookmarks("Startdate").Range
Startdate.Text = Me.TextBox3.Value
Me.Repaint
Sub UpdateAllFields()
UserForm1.Hide
But this one gives me an error message. Can somebody help, please?
Stick to the fields path as it may be less work to maintain.
I: Create three custom document properties for each field (Name, Title, StartDate)
Steps:
1) Click File | Properties | Advanced properties | Custom
2) Enter the following data:
- Name: customName
- Text: Enter name
3) Click the button "Add"
4) Repeat steps 2 and 3 for fields Title and StartDate (remember to add the "custom" word before so you can identify them later easily
This is how it should look like:
II: Add the fields of each custom property created to the word document
Steps:
1) Position the cursor where you want the field in the word document
2) Click Insert | Quick parts | Field | Categories: Document information | DocProperty | < name of the doc property >
3) Click Ok
4) Repeat the process for each field
Note: You can add the same field / custom property, in different parts of the document
III: Create the UserForm and add the controls
Steps:
1) Add the UserForm
2) Add three textboxes and set the name property to:
- txtName
- txtTitle
- txtStartDate
for each one.
3) Add a command button and set its name to cmdInsertData
This is how it should look like:
To set the name of each control look for (Name) at the properties window:
IV: Add code to show the userform at startup
Steps:
1) Launch the VBE by pressing Alt + F11 (or activating the developer tab in the ribbon and pressing the Visual basic button
2) Add the following code to the "ThisDocument" object:
Private Sub Document_Open()
UserForm1.Show
End Sub
V: Add the form code
Steps:
1) Right click the UserForm1 object and select "View Code"
2) Add the following code:
Private Sub cmdInsertData_Click()
' Update the properties values
ThisDocument.CustomDocumentProperties("customName").Value = Me.txtName.Value
ThisDocument.CustomDocumentProperties("customTitle").Value = Me.txtTitle.Value
ThisDocument.CustomDocumentProperties("customStartDate").Value = Me.txtStartDate.Value
' Show changes of document properties in document
ThisDocument.Fields.Update
' Hide the userform
UserForm1.Hide
End Sub
Remember to save your document as macro-enabled
Close it and reopen it
Give it a try and let me know
Disclaimer: As this article states, there is a right way to handle showing a userform properly, but I'll leave it the "easy" way for simplicity purposes.

Access VBA to Change an Image in a Continuous Form

I am trying to create a receipt form where people will confirm if they've received the full quantity of an order. As part of this, I want the following to happen:
If they received the full quantity, a green check mark appears
If they received a partial quantity, an orange triangle appears
If they received no items, a red x appears
To accomplish this, I'm using a continuous form with 3 image files for each situation. I'm using the code below to change the image when the quantity is changed. The problem is, when the quantity is change on 1 line, the symbol changes for all lines. I'll post pictures as well.
Any thoughts on how I can fix this?
I'm open to other methods of accomplishing this idea too.
Private Sub FinalQTY_AfterUpdate()
If IsNull(Me.FinalQty) Then
MsgBox "You must enter a quantity for this item"
Me.FinalQty.SetFocus
Exit Sub
Else
LValue = Me.[FinalQty]
If IsNumeric(LValue) = 0 Then
Me.FinalQty = ""
MsgBox "Qty must be a numeric value"
Me.QTY.SetFocus
Exit Sub
End If
End If
Me.FinalTotalPrice = Me.FinalPrice * Me.FinalQty
If Me.FinalQty = 0 Then
Me.Yes.Visible = False
Me.Change.Visible = False
Me.No.Visible = True
End If
If Me.FinalQty < Me.QTY Then
Me.Yes.Visible = False
Me.Change.Visible = True
Me.No.Visible = False
End If
If Me.FinalQty = Me.QTY Then
Me.Yes.Visible = True
Me.Change.Visible = False
Me.No.Visible = False
End If
End Sub
This is before I adjust the quantity:
This is after I adjust the qty of only the second line:
Since the formatting of each record displayed by a continuous form is inherited from the form design template, any changes to the template will be automatically applied to all records displayed by the form, aside from Conditional Formatting rules in effect or the handful of properties which may changed via the OnPaint event of the Detail section.
One possible alternative might be to add a new field to your table with a data type of OLE Object and populate the value on the AfterUpdate event using the AppendChunk method, sourcing image data from a separate table containing three records corresponding to your green tick, orange triangle, and red cross images.

iTextSharp - Force text below image

My issue with images and iTextSharp is that I want to put a header in and the have the text below that point.
I can display the image and wrap the text but not get it to display below, even when adding New paragraph.
I am assuming there is some command around the aligmnent but I am struggling to find it even when looking at tutorials such as http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images
'Import Image
Dim vImagePath = "C:\sites\images\" & "Logo.jpg"
Dim jpg As Image = Image.GetInstance(vImagePath)
jpg.ScaleToFit(139.0F, 106.0F)
jpg.Alignment = Image.ALIGN_RIGHT Or Image.TEXTWRAP
jpg.IndentationLeft = 7.0F
jpg.SpacingAfter = 9.0F
jpg.BorderWidthTop = 6.0F
jpg.BorderColorTop = Color.WHITE
document.Add(jpg)
'##Main Document Header
Dim tableHeader As New PdfPTable(1) ' Number in brackets is number of columns
tableHeader.DefaultCell.Border = Rectangle.NO_BORDER
Dim phFirst As Phrase = New Phrase("Title", HeaderFont)
tableHeader.AddCell(phFirst)
document.Add(tableHeader)