I'm creating a series of tags in Illustrator, using VBA in excel (the excel worksheet has the information that populates the tags), and I cannot find a way to specify that the font which appears in Illustrator is italicized and a particular font.
Using:
.TextRange.CharacterAttributes.TextFont = appIll.TextFonts.Item("Arial")
lends the same result as using:
.TextRange.CharacterAttributes.TextFont = appIll.TextFonts.Item("Monotype Corsiva")
And needless to say, I also can't get italics. I'm very new to this, but would appreciate anyone letting me know how to specify the font and font-style. Thanks!
.TextRange.ParagraphAttributes.Justification = aiCenter
.TextRange.CharacterAttributes.size = dblTopLine1FontSize
.TextRange.CharacterAttributes.StrokeWeight = 0.35
.TextRange.CharacterAttributes.StrokeColor = clrStrokeColor
.TextRange.CharacterAttributes.FillColor = clrFontFillColor
SetItalics tfrmTopLine1
.CreateOutline
End With
have a look at the following to see if it helps:
Firstly, at the risk of stating the obvious, I first identified that the font I 'needed' to use was indeed accessible to my copy of Illustrator - as it happens, to use Monotype Corsiva in code it has to be "MonotypeCorsiva"! The lessons here are that the 'real' font name may be different from the Illustrator displayed font name and the 'real' font name also indicates the 'style'. I used the following code which simply listed the font and its 'style' to Excel's Immediate Window. Illustrator needs to be open for this example.
Sub IllFnts()
Dim IApp As New Illustrator.Application
Set IApp = GetObject(, "Illustrator.Application")
Dim fnt As Illustrator.TextFont
For Each fnt In IApp.TextFonts
Debug.Print fnt.Name & " - " & fnt.Style
Next
End Sub
I then added a Point Text Frame, added some text and changed the TextFont with the code below:
EDIT - UPDATE TO INCLUDE A MEANS OF APPLYING ITALIC
Sub TestChngeFnt()
Dim IApp As New Illustrator.Application
If IApp Is Nothing Then
Set IApp = CreateObject("Illustrator.Application")
Else
Set IApp = GetObject(, "Illustrator.Application")
End If
Dim fnt As Illustrator.TextFont
'A distinctive font for reference?
Set fnt = IApp.TextFonts("Algerian")
'Add a Document
Set docRef = IApp.Documents.Add()
'Add some Point Text
Set pointTextRef = docRef.TextFrames.Add()
pointTextRef.Contents = "Some Text in a Point TextFrame"
pointTextRef.Top = 700
pointTextRef.Left = 20
pointTextRef.Selected = True
pointTextRef.TextRange.CharacterAttributes.Size = 35
IApp.Redraw
'Set distinctive font
IApp.Documents(1).TextFrames(1).TextRange.CharacterAttributes.TextFont = IApp.TextFonts.Item(fnt.Name)
MsgBox "Have a look at the text font before changing to another."
'set a new font to 'regular'
Set fnt = IApp.TextFonts("BodoniMT")
IApp.Documents(1).TextFrames(1).TextRange.CharacterAttributes.TextFont = IApp.TextFonts.Item(fnt.Name)
MsgBox "New text font before changing to italics."
'set font to 'italics' within the same font family?
Set fnt = IApp.TextFonts("BodoniMT-Italic")
IApp.Documents(1).TextFrames(1).TextRange.CharacterAttributes.TextFont = IApp.TextFonts.Item(fnt.Name)
End Sub
This example includes a couple of message boxes to pause the code to observe the text changes.
Applying 'italic' in this example is really selecting a font from the same font family designed as italic? Not completely sure if this is the 'correct' approach with Illustrator or just a workaround,but it may take you forward a little.
You may also find this Adobe Illustrator CS5 Scripting Reference: vbScript useful although the Object Browser in Excel is also a good starting reference.
Related
I am trying to automate the preparation of an internal Excel-based report based on a previous template.
The macro creates and fills a new sheet, matching the font name, size, color etc. settings of the template, as well as the row heights - column widths. Despite providing the exact same values for font, height, width etc., the VBA produced sheet's font does not match the original template. I believe the issue can be fixed if we can figure out why the VBA produced text is "pixelated".
As I am not familiar with the file sharing best-practices of SO, I will be including code used to reproduce the new sheet and a comparison of some attributes of the both sheets. I will include a link to the file containing the two sheets in the comments. I believe it may be important to have a copy of the original template to answer this question.
Here is an example of how a cell's properties are changed, pretty basic.
ws.Columns(12).EntireColumn.ColumnWidth = 9.57
ws.Rows(9).EntireRow.RowHeight = 50.25
ws.Cells(9, 12).Font.Name = "Segoe UI"
ws.Cells(9, 12).Font.Size = 11
Here are some properties I compared for the two sheets as I did not know what I was looking for, all are identical for the two sheets.
Range.AddIndent = False
Range.ColumnWidth = 9.57
Range.RowHeight = 50.25
Range.Font.Name = "Segoe UI"
Range.Font.FontStyle = "Regular"
Range.Font.Size = 11
Range.Font.ThemeFont = xlThemeFontNone
Range.Font.ThemeColor = 2
Range.Font.OutlineFont = False
Range.Font.Subscript = False
Range.Font.Superscript = False
Range.Font.Italic = False
Range.HasSpill = False
Range.Phonetics = Nothing
Range.PrefixCharacter = ""
Range.SpillingToRange = Nothing
Range.UseStandardHeight = True
Range.UseStandardWidth = False
Range.WrapText = True
Worksheet.Cells.AddIndent = False
Worksheet.EnableOutlining = False
Worksheet.Outline.AutomaticStyles = False
Worksheet.PageSetup.BottomMargin = 18 'These are different, but did not effect the outcome when equalized
Worksheet.PageSetup.LeftMargin = 50.4 'These are different, but did not effect the outcome when equalized
Worksheet.PageSetup.FooterMargin = 21.6 'These are equal for both
Worksheet.PageSetup.HeaderMargin = 21.6 'These are equal for both
Worksheet.StandardHeight = 16.5 'These are different, yet read-only
Worksheet.StandardWidth = 8.38 'These are different, yet read-only
I know a little HTML/CSS and my guess is despite having the same font families(names), the .woff2 or .eot are different for the two fonts. I have no idea why though.
Painting the target cells using the original template format does the job, as does a copy/pasting strategy, but I am aiming for a clean solution without including the template in the final file and generating the sheets from scratch. I could also change the font family or cell sizes to get an acceptable output, but at this point I wish to explore the reason behind this issue out of curiosity.
My Excel macro reads the answers to a survey from a set of Excel files. The answers of a survey contain a score (from 1 to 4) and a description. The goal is to generate a a matrix. Each cell of the matrix has a color that represents the score. I would like the user to be able to modify the layout of these cell. To make it easy to the user, I created a template matrix and a button. The user should be able to modify the layout of the cells and on a click of a button, a set of styles (Score 1, Score 2,...) should be generated. Once the matrix is created, the Workbook should be to function without the survey files.
I have tried a couple of things:
Try 1
ThisWorkbook.Styles.Add "Score 1", BasedOn:=cell1
This gives errors. I don't fully understand when they occur, but one of the causes is when the user modifies the cell layout by selecting another style.
Try 2
ThisWorkbook.Styles("Score 1").Delete
ThisWorkbook.Styles.Add "Score 1", BasedOn:=cell1
This is not a good idea: all cells loose their styling when it is executed a second time.
Try 3: Current
Copy the most frequently used properties of the cells layout and copy them to the style. If this style is deleted by the user, it is recreated. This procedures is not ideal, since most style properties won't be covered.
Is there a way to update a cell style that is more general? I would like there to be as little room as possible to make the workbook in an inconsistent and non-functional state.
I sticked with try 3. Because it required a lot of code for all properties that seemed possible to be edited, and because of copying borders is tricky, I post the result.
'xR1_Template: the cell to base the style on
'nm_Style: the name of the style
Public Function Upsert_Style(xR1_Template As Excel.Range, nm_Style As String) As Excel.Style
Dim xStyle As Excel.Style
Set xStyle = Fn.TryGet(ThisWorkbook.Styles, nm_Style)
If Fn.IsNothing(xStyle) Then
Set xStyle = ThisWorkbook.Styles.Add(nm_Style)
End If
xStyle.Font.Color = xR1_Template.Font.Color
xStyle.Font.Bold = xR1_Template.Font.Bold
xStyle.Font.Name = xR1_Template.Font.Name
xStyle.Font.Italic = xR1_Template.Font.Italic
xStyle.Font.Size = xR1_Template.Font.Size
xStyle.Font.Strikethrough = xR1_Template.Font.Strikethrough
xStyle.Font.Subscript = xR1_Template.Font.Subscript
xStyle.Font.Superscript = xR1_Template.Font.Superscript
xStyle.Font.Underline = xR1_Template.Font.Underline
xStyle.Interior.Color = xR1_Template.Interior.Color
xStyle.Interior.Pattern = xR1_Template.Interior.Pattern
xStyle.Interior.PatternColor = xR1_Template.Interior.PatternColor
'NOTE: necessary to delete all borders first. There's no way to delete them one by one.
xStyle.Borders.LineStyle = xlNone
Dim iBorder As Long
For iBorder = 1 To xR1_Template.Borders.Count
Dim xBorder As Excel.Border
'NOTE: The Borders property claims to work with xlBordersIndex argument, but this is not true.
' Normal indexing is used.
Set xBorder = xR1_Template.Borders(iBorder)
'NOTE: "none-style" borders (=no border), should be skipped.
' Once they are retrieved using the Borders property, they are always visible.
' Setting them with xlLineStyle.xlLineStyleNone does not hide them.
If xBorder.LineStyle <> XlLineStyle.xlLineStyleNone Then
Dim xBorder_Style As Excel.Border
Set xBorder_Style = xStyle.Borders(iBorder)
xBorder_Style.Color = xBorder.Color
xBorder_Style.LineStyle = xBorder.LineStyle
xBorder_Style.Weight = xBorder.Weight
End If
Next iBorder
xStyle.AddIndent = xR1_Template.AddIndent
xStyle.FormulaHidden = xR1_Template.FormulaHidden
xStyle.HorizontalAlignment = xR1_Template.HorizontalAlignment
xStyle.IndentLevel = xR1_Template.IndentLevel
xStyle.NumberFormat = xR1_Template.NumberFormat
xStyle.NumberFormatLocal = xR1_Template.NumberFormatLocal
xStyle.Orientation = xR1_Template.Orientation
xStyle.ShrinkToFit = xR1_Template.ShrinkToFit
xStyle.VerticalAlignment = xR1_Template.VerticalAlignment
xStyle.WrapText = xR1_Template.WrapText
xStyle.IndentLevel = xR1_Template.IndentLevel
Set Upsert_Style = xStyle
End Function
I have a macro that will append a page to the current document from a pre-formatted template. This page inserts a picture based off user selection, which they have the standard excel supported images(i.e. .jpg, .png, .bmp) as well as PDF's(taken off an AutoCAD program that can only save as PDF/DWG/DXF file types. The problem I'm having is that I cannot rotate landscape print formatted images the way that I can with the regular images. I know that the problem is likely that not all OLEobjects can be rotated and the shaperange option probably does not allow for rotation. That being said, is it possible to do this within VBA?
I have tried to use the OLEobject shaperange option, as well as inserting the PDF as a picture(which is not supported by MS-Office as far as I know). I have also tried to select the OLEobject as a shape to no avail.
Set rng = crrntWorkbook.Sheets(1).Range("B" & tempRow)
'Allows for the insertion of PDF files into the workbook
Set oleobj = ActiveSheet.OLEObjects.Add(Filename:=txtFileName, link:=False, DisplayAsIcon:=False)
With oleobj
'Inserts the picture into the correct cell
oleobj.Top = rng.Top
oleobj.Left = rng.Left
'If the image is wider than it's height, image will be scaled down by it's width, otherwise it's height
If oleobj.Width > oleobj.Height Then
oleobj.IncrementRotation = 90
oleobj.Width = 545
oleobj.Left = (570 - oleobj.Width) / 2
oleobj.Top = oleobj.Top + 2
'Centers the image
Else
oleobj.Height = 625
oleobj.Left = (550 - oleobj.Width) / 2
oleobj.Top = oleobj.Top + 2
End If
End With
The expected result is that the image will be rotated upon insertion, but I will get either a "runtime error '438' Object doesn't support this property or method" or a "runtime error -2147024809 the shape is locked and cannot be rotated if I use the shaperange approach"
Can anyone tell how can change font size of entire word application using spire.doc?
How to select the entire text range and change the font size and style?
Can anyone please help me??
in Vb .net :
Dim document As New Document()
document.LoadFromFile("test.html", FileFormat.Html, XHTMLValidationType.None)
For Each section As Section In document.Sections
For Each paragraph As Paragraph In section.Paragraphs
For Each docObject As DocumentObject In paragraph.ChildObjects
If docObject.DocumentObjectType = DocumentObjectType.TextRange Then
Dim text As TextRange = TryCast(docObject, TextRange)
text.CharacterFormat.FontName = "Calibri"
End If
Next docObject
Next paragraph
Next section
document.SaveToFile("HtmlFileToPDF.pdf", FileFormat.PDF)
I am trying to change the font color for specific words with-in a cell. I don't want to change all of the text to the color just specific words. I am using an OfficeWriter ExcelTemplate.
You could use ExcelApplication to Post Process your OfficeWriter Template file.
You want to get a handle on what is referred to as a CharacterRun See the OfficeWriter documentation.
Your code would look something like:
ExcelTemplate xlt = new ExcelTemplate()
//Process Template
ExcelApplication xla = new ExcelApplication();
Workbook wb = xla.Open(xlt)
Worksheet ws = wb.Worksheets[0];
Cell cellA1 = ws.Cells["A1"];
cellA1.Value = "Test";
CharacterRun charRun = cellA1.GetCharacters(0,5);
charRun.Font.Color = Color.Red
In the formula bar, select the words you want to change the color of, then change the text color.