I'm trying to set font styling only to tables after the second Word page.
so far I got:
Sub defaultFontStyling()
If Selection.Information(wdWithInTable) = True Then
Range.Font.Name = "Calibri"
Range.Font.Size = 11
End If
End Sub
I don't have any tables on the first page but still, it changes the font styling also there (In the entire document).
Does someone know how to fix it?
Try this:
Sub defaultFontStyling()
Dim table As table
For Each table In ActiveDocument.Tables
If table.Range.Information(wdActiveEndAdjustedPageNumber) > 1 Then
With table.Range.Font
.Name = "Calibri"
.Size = 11
End With
End If
Next
End Sub
Related
I want to insert a shape in Word above the picture where ever user clicks.
I have written the program below, but sometimes it is placing the rectangle incorrectly and inserting it twice: once where I need it and again somewhere else.
Why is the Shape being inserted twice?
Private WithEvents app As Word.Application
Private Sub app_WindowSelectionChange(ByVal Sel As Selection)
Cancel = True
Call CurosrXY_Pixels
End SuB
Sub CurosrXY_Pixels()
ActiveDocument.Shapes.AddShape(msoShapeRectangle, fcnXCoord, fcnYCoord, 20#, 16#).Select
With Selection
.ShapeRange.TextFrame.TextRange.Select
.Collapse
.Font.Name = "Arial"
.Font.Size = 7
.Font.Bold = False
.Paragraphs.FirstLineIndent = 0
.Paragraphs.RightIndent = -10
.Paragraphs.LeftIndent = -10
.Paragraphs.Alignment = wdAlignParagraphCenter
.TypeText Text:=11
.ShapeRange.LockAspectRatio = msoCTrue
End With
End Sub
Function fcnXCoord() As Double
fcnXCoord = Selection.Information(wdHorizontalPositionRelativeToPage)
End Function
Function fcnYCoord() As Double
fcnYCoord = Selection.Information(wdVerticalPositionRelativeToPage)
End Function
The reason the code is firing more than once is because of the use of the Select method. Code changing the selection is the same as the user doing so. The way to avoid this is to work directly with the Word objects.
The code below illustrates this in the procedure CurosrXY_Pixels. A Shape object is declared, then the newly inserted drawing object assigned to it. This then used for setting the formatting and text, in the With block.
Notice I've also passed the Selection object from the event to this procedure, as well as to the two that calculate the co-ordinates. Since, conceivably, the user could click again before the macro finishes, it's important to pass along the original Selection. (That the original code was not doing so probably contributed to the "randomness" of where things were being created since the code, itself, was changing the selection.)
The code line in the app_WindowSelectionChange event to call the other procedures: CurosrXY_Pixels Sel
Sub CurosrXY_Pixels(Sel As Word.Selection)
Dim shp As Word.Shape
Set shp = ActiveDocument.Shapes.AddShape(msoShapeRectangle, fcnXCoord(Sel), fcnYCoord(Sel), 20#, 16#, Sel.Range)
With shp.TextFrame.TextRange
.Font.Name = "Arial"
.Font.Size = 7
.Font.Bold = False
.Paragraphs.FirstLineIndent = 0
.Paragraphs.RightIndent = -10
.Paragraphs.LeftIndent = -10
.Paragraphs.Alignment = wdAlignParagraphCenter
.Text = 11
End With
shp.LockAspectRatio = msoCTrue
End Sub
Function fcnXCoord(Sel As Word.Selection) As Double
fcnXCoord = Sel.Information(wdHorizontalPositionRelativeToPage)
End Function
Function fcnYCoord(Sel As Word.Selection) As Double
fcnYCoord = Sel.Information(wdVerticalPositionRelativeToPage)
End Function
I am working on a word document and made a command button that is suppose to hide a table. Now when I first set it, I thought I got it working I got it all styled and titled and when I clicked the button the table would disappear.
Then I saved it and closed the document but when I opened up the document I saw that the only thing that was hidden was the words inside the table, the table lines are not hidden and when I toggle the button the only thing hiding is the text.
Is there something I am doing wrong ? Here is the code in VBA
Private Sub CommandButton1_Click()
ThisDocument.Styles("HideText").Font.Hidden = Not ThisDocument.Styles("HideText").Font.Hidden
End Sub
I just want the button to toggle the text and the Table to hide every time it the button is pressed and when the document is open and closed.
Update may be on to something the table has its own style as well. should I be targeting that as well as the text within the style ? is that what is happening ?
Update #2
I was able to now hide and unhide the section of the table I wanted but it doesn't bring up the lines after I make the table visible. So is there a way to get the table grid to show up with the click of the button?
here is what I have so far.
Private Sub CommandButton1_Click()
ThisDocument.Styles("HideText").Font.Hidden = Not ThisDocument.Styles("HideText").Font.Hidden
'Table Grid
Dim s As Style
Dim An As Integer
An = 0
If An = 0 Then
For Each s In ActiveDocument.Styles
If s.Type = wdStyleTypeTable Then
If s.NameLocal = "Table Grid" Then
Debug.Print (s.NameLocal)
s.Visibility = False
s.UnhideWhenUsed = False
Call s.Delete
End If
End If
Next
An = 1
End If
If An = 1 Then
For Each s In ActiveDocument.Styles
If s.Type = wdStyleTypeTable Then
If s.NameLocal = "Table Grid" Then
Debug.Print (s.NameLocal)
s.Visibility = True
s.UnhideWhenUsed = True
Call s.Delete
End If
End If
Next
An = 0
End If
End Sub
I'd approach this by hiding the font of the table (as below) rather than attempting to hide a specific font style which you're using within the table.
You could try something along the lines of:
Public sub CommandButton1_Click()
With ActiveDocument.Tables(1).Range.Font
.Hidden = Not .Hidden
End With
End Sub
In my docs I use either Arial or Courier New (for code) and sometimes both in the same paragraph. As I share my docs with other people, they tend to use other fonts as well but it is important to keep it aligned, that;s why I am trying to create a macro that will turn all non-Courier New text into Arial and into the correct font size (11).
I face 2 problems with what I have achieved so far:
In paragraphs with mixed fonts it tends to change the whole paragraph (including the code) to Arial, while i need it to change only the non-code text
It changes the font size not only in the body text but in the headings as well.
I think I'm using incorrectly the objects of Word (I'm used in working in Excel) but I can't find anywhere online any clues. Can anyone help me please?
Sub CorrectFont()
Dim p As paragraph
Set p = ActiveDocument.Paragraphs(1)
Application.Visible = False
Application.ScreenUpdating = False
Do
If p.Range.Font.Name <> "Courier New" Then
p.Range.Font.Name = "Arial"
p.Range.Font.Size = 11
End If
Set p = p.Next
Loop Until p Is Nothing
Application.ScreenUpdating = True
Application.Visible = True
End Sub
You can check each individual word, like so:
' Replaces non-Arial fonts with Arial.
' Exception: Courier New is not replaced.
Sub AlignFont()
Dim wd As Range
' Check each word, one at a time.
For Each wd In ActiveDocument.Words
If Not (wd.Font.Name = "Arial" Or wd.Font.Name = "Courier New") Then
wd.Font.Name = "Arial"
End If
Next
End Sub
Thanks to #destination-data 's inputs I reached a final form of the code. I present it here for anyone that might be interested.
Thank you again!
Sub AlignFont()
Dim wd As Range
Application.Visible = False
Application.ScreenUpdating = False
' Check each word, one at a time.
For Each wd In ActiveDocument.Words
'On objects like Contents it may create an error and crash
On Error Resume Next
If wd.Font.Name <> "Courier New" And wd.Style = "Normal" Then
wd.Font.Name = "Arial"
End If
'To avoid any header that may have a "Normal" style
If wd.Font.Bold = False Then
wd.Font.Size = 11
End If
Next
Application.ScreenUpdating = True
Application.Visible = True
End Sub
I am trying to manually set every OptionButton's font on a sheet to be a uniform size and type using a For Loop.
I can do them manually by writing out each specific button's information but I have hundreds of buttons.
I can even get VBA to write the correct syntax to a test Worksheet by using this code here:
`Private Sub Thisworkbook_Open()
For i = 1 to Worksheets("Core").OLEObjects.Count
If TypeName(Worksheets("Core").OLEObjects(i).Object) = "OptionButton" Then
Worksheets("testsheet").Range("A" & i).Value = Worksheets("Core").OLEObjects(i).Name
End If
Next i
End Sub`
But what I can't do is put the rest of this below code along with the above code to have ONE clean and concise statement that will manually set all OptionButton values to these settings:
`With Worksheets("Core").OptionButton1
.Font.Size = 11
.Font.Name = "Calibri"
.Font.Bold = False
End With`
Can someone explain to me how I can make this work?
Actually you have your answer in your question, all you have to do is to put your properties to correct location, as follows:
For i = 1 To Worksheets("Core").OLEObjects.Count
If TypeName(Worksheets("Core").OLEObjects(i).Object) = "OptionButton" Then
Worksheets("Core").OLEObjects(i).Object.FontSize = 5
' Remaining code goes here.
End If
Next i
I am looking for a macro for word documents that will find every style in a document, and change it from whatever it is (centered, justified, right-align) to left-align.
I don't want to change the text (except as a by-product), but the style itself so everything updates.
Thanks Remou, I tried working with it, and this seems to work:
Sub ChangeStyles()
Dim oSource As Document
Set oSource = ActiveDocument
For i = 1 To oSource.Styles.Count
' must check the style type as character style gives an error
If oSource.Styles(i).Type = wdStyleTypeParagraph Then
With ActiveDocument.Styles(i).ParagraphFormat
.Alignment = wdAlignParagraphLeft
End With
Else
End If
Next i
End Sub