I want to insert some text before the 'Figure' label when inserting a caption. i.e. turn this:
1.1.1 <Figure caption text>
into this:
<some text> 1.1.1 <Figure caption text>
I am using the InsertCaption method of the range object:
rng.Text = "asf"
rng.InsertCaption(Word.WdCaptionLabelID.wdCaptionFigure, ": " & caption,, Word.WdCaptionPosition.wdCaptionPositionBelow)
rng.InsertAfter(Chr(13) & Chr(10))
I cannot seem to modify the range object to include the text at the start, it always seems to enter the caption on its own line.
Related
I am adding hyperlinks to selected text in Word. The selected text may have formatted text in it, such as italics, small caps, bold, etc.
I am able to preserve the CHARACTER styles, i.e., small caps, if the ENTIRE selection is the same character style (e.g., small caps). However, if the text includes unformatted text along with the formatted text, then the character style is replaced with the hyperlink style.
I inserted screenshots of before and after adding the hyperlinks.
BEFORE (no hyperlinks added):
AFTER (each line has a hyperlink added):
What I'm trying to end up with is something like the "SMALL CAPS TEST" as it:
preserves the initial small caps style
does not get overwritten with the HYPERLINK style (blue underlined text)
Any help is much appreciated. I'm at a loss on how to step through the style applied to each word or even character in the text selection.
UPDATE:
I managed to get rid of the Hyperlink character style SHOWING up as blue and underlined. However, the character style will still overwrite the previous character styles (i.e., Bold, Small Caps, Italics). Here is my code that will provide the correct character style applied until the Hyperlink is attached. However, if the entire selection is all ONE character style, then it is still preserved (probably due to the code line:
Selection.style = MyStyle
I need to be able to apply the correct character style to each character.
Here is my code:
``Sub AddHLINK()
Dim myText, myISBN, myCurrISBN, myURL, myCharStyle, ochr As String
Dim NoCharSty As Boolean
Dim MyStyle, MyFont, mychar, mycharstyleb As String
Dim charcount, j, k As Integer
myURL = "http://www.google.com"
myText = Selection.Text
charcount = Len(myText)
For k = 1 To charcount
mychar = Selection.Characters(k).Text
MyStyle = Selection.Characters(k).Style
If ActiveDocument.Characters(k).Style = wdStyleTypeCharacter Then
myCharStyle = MyStyle
NoCharSty = False
Else
NoCharSty = True
End If
Next k
MsgBox "the current url is " & myURL
Stop
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:=myURL, TextToDisplay:=myText
End Sub
I have a text box in a Word 2016 document. This is not a TextBox form object but a normal Text Box that you insert from the Insert tab here:
I am trying to check if it is empty when the document is opened. For the life of me I cannot figure out how to do this. I have tried all of the following:
If (Len(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text & vbNullString) = 0) Then
If (IsNull(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text)) Then
If (LTrim(RTrim(ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text)) = "") Then
If (ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text = "") Then
None of these return true when the text box is empty? This is the text box:
It seems that the text box always contains a paragraph marker (which I can't delete). This is what the VBA watch shows:
Watch : : ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2").TextFrame.TextRange.Text : "
" : String : ThisDocument.Document_Open
Note that the watch is two separate lines which makes me think that there's CRLF or something in there?
There is always a paragraph break in an otherwise empty textbox. Accordingly, you could use something along the lines of:
Private Sub Document_Open()
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes("Text Box 2")
If Not .TextFrame Is Nothing Then
If Trim(.TextFrame.TextRange.Text) = vbCr Then
MsgBox "Empty Text Range"
End If
Else
MsgBox "No Text Range"
End If
End With
End Sub
I need a way to get an image's Alt Description (specified in HTML) into the Word DOCX image's Caption. This needs to be done with many images in a document, so a macro would be best.
Assume an HTML doc with an img tag similar to this
<img src="http://www.example.com/path/to/image/picture01.jpg"
title="picture 01 title"
alt="this is alt text"
caption="this is a caption field">
This HTML doc is imported into Word 2010 (via File, Open). The image will show in the doc.
Now, how to get the 'title' attribute (which shows up in the Format Picture's Alt-Text dialog as the Description - see screenshot below) into the image's Caption?
Note that the caption parameter in the image tag is not converted to a Word Caption for that image.
Sample Alt-Text dialog for an image which shows the image's alt value as the Description
Microsoft Word has a Range.InsertCaption method, which will insert field codes to automatically number images. As you were asking to insert Alternative Text, my feeling is you were using the term caption simply as a directive to get the text beneath each image on its own carriage return. So that's what this code does:
Sub GenerateAltTextCaptions()
Dim AltTextCaption As String
Dim ImageRange As Range
Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst
With ActiveDocument
For i = 1 To .InlineShapes.Count
AltTextCaption = .InlineShapes(i).AlternativeText
Set ImageRange = .InlineShapes(i).Range
ImageRange.Collapse Direction:=wdCollapseEnd
ImageRange.InsertAfter vbCr
ImageRange.InsertAfter AltTextCaption
Next
End With
End Sub
Yes, I think you've hit on a good way to make the conversion. Glad you were able to find it!
The code below will loop all the InlineShapes in the document and, if the Alternative Text is not empty, create a caption using that text.
InsertCaption is only available in VBA for InlineShapes. The user can insert captions for Shapes (graphics with text wrap formatting) because Word evaluates the selected graphic and creates a Textbox of the same width and positions it immediately below the graphic. The Shape and textbox are not, however, linked together in any way. So this functionality is not offered in VBA and would require some "creative" programming.
Sub InsertCaptionFromAltText()
Dim doc as Word.Document
Dim ils As word.InlineShape
Dim captionText As String
Set doc = ActiveDocument
Set ils = doc.InlineShapes(1)
For each ils in doc.InlineShapes
captionText = ils.AlternativeText
If Len(captionText) > 0 Then
ils.Range.InsertCaption Label:=wdCaptionFigure, _
Title:=captionText, _
Position:=wdCaptionPositionBelow
End If
Next
End Sub
How can I align a paragraph with just the text portion of a numbered heading? e.g:
1.1.2 This Is A Numbered Heading
This is the aligned text I'm trying to achieve
This is aligned to the numbers not the text
2.4 This Is Another Example
This is where the text should be
I'm aware of the CharacterUnitLeftIndent, CharacterUnitFirstLineIndent, FirstLineIndent etc properties but after a few hours experimentation & searching online can't figure out how to achieve this programmatically. I know how to test for the heading style and how to refer to the following paragraph so just need to know how to get the indent right.
To use a macro to accomplish this, you have to check each paragraph in your document and check to see if it is a "Header" style. If so, then pick off the value of the first tab stop to set as the indent for the subsequent paragraphs.
UPDATE1: the earlier version of the code below set the paragraphs to the Document level first tab stop, and did not accurately grab the tabstop set for the Heading styles. The code update below accurately determines each Heading indent tab stop.
UPDATE2: the sample text original I used in shown in this first document:
The code that automatically performs a first line indent to the tab level of the preceding heading is the original Sub from the first example:
Option Explicit
Sub SetParaIndents1()
Dim myDoc As Document
Set myDoc = ActiveDocument
Dim para As Paragraph
Dim firstIndent As Double 'value in "points"
For Each para In myDoc.Paragraphs
If para.Style Like "Heading*" Then
firstIndent = myDoc.Styles(para.Style).ParagraphFormat.LeftIndent
Debug.Print para.Style & " first tab stop at " & _
firstIndent & " points"
Else
Debug.Print "paragraph first line indent set from " & _
para.FirstLineIndent & " to " & _
firstIndent
para.FirstLineIndent = firstIndent
End If
Next para
'--- needed to show the changes just made
Application.ScreenRefresh
End Sub
And the results looks like this (red lines added manually to show alignment):
If you want the entire paragraph indented in alignment with the heading style, the code is modified to this:
Option Explicit
Sub SetParaIndents2()
Dim myDoc As Document
Set myDoc = ActiveDocument
Dim para As Paragraph
For Each para In myDoc.Paragraphs
If para.Style Like "Heading*" Then
'--- do nothing
Else
para.Indent
End If
Next para
'--- needed to show the changes just made
Application.ScreenRefresh
End Sub
And the resulting text looks like this:
For some reason I need retrive "html formated text" from cell in which calc sheet.
I had tried :
Sub Main
Osheet = ThisComponent.Sheets.getByName("mySheet")
oCell = oSheet.getCellrangeByName("D2")
Print oCell.htmlText REM there is no htmltext property in cell instance
REM holp output some thing like <p color="#ff0000">text in cell</p>
End Sub
Is's possible to do this?
-----------update-------------
as image show above. I change color and the font size in cell.
then I need to retrive "html text" from the cell with VBA.
if possible It should output:
<font size='12'>Hi !<font color='#ff0000'>every</font><font size='16'>body</font>.<font color='#0000ff'>Are</font> you OK?</font></font>
How to implement this function with VBA?