PDA

View Full Version : Solved: Content of building block in messagebox



ksper
11-14-2012, 11:04 AM
Hi all,

I have encountered a problem that you might be able to solve.

I have begun the programming of a project and allready in the start-up i have encountered a problem that stops the process of coding the rest.

I have made a macro that inserts a specific Building block from a template.

In the example the building block "F2" from the template "Normal.dotm" is inserted in the active docement. I need the content of the Building Block to be displayed in a messagebox or similar.

The problem is, that it is only the first 255 characters which is displayed in the messagebox.

Does some of you have any suggestions for solving this problem?

Code:
------------------------------------------------------------------------------
Sub BuildingBlockMsgBox()
Dim oTmp As Template
Dim sPath As String
sPath = Options.DefaultFilePath(wdUserTemplatesPath) & "\normal.dotm"
Set oTmp = Templates(sPath)
oTmp.BuildingBlockEntries("F2").Insert Selection.Range
temp = oTmp.BuildingBlockEntries("F2")
MsgBox temp

End Sub
------------------------------------------------------------------------------

Thanks,

/K

gmaxey
11-14-2012, 11:53 AM
The value of the BB will have to be text:
Sub BuildingBlockMsgBox()
Dim oTmp As Template
Dim sPath As String
sPath = Options.DefaultFilePath(wdUserTemplatesPath) & "\normal.dotm"
Set oTmp = Templates(sPath)
oTmp.BuildingBlockEntries("F2").Insert Selection.Range
MsgBox oTmp.BuildingBlockEntries("F2").Value

End Sub

ksper
11-14-2012, 12:13 PM
Hi Greg,

Thnks for your quick reply!

The msgbox still only shows the first 255 characters. I have (for testing) defined building block "F2" as:

123456789abcdefghij
123456789abcdefghij
123456789abcdefghij
123456789abcdefghij
123456789abcdefghij
123456789abcdefghij
123456789abcdefghij
123456789abcdefghij
123456789abcdefghij
123456789abcdefghij
123456789abcdefghij
123456789abcdefghij
XXXXXXXXXXXXXX
123456789abcdefghij
123456789abcdefghij
123456789abcdefghij
123456789abcdefghij
XXXXXXXXXXXXXXX

All the text above is being inserted in the doc by the macro but only the first part (down to the first row of x´s) is shown in the msgbox. ..

So it seems like VBA can only extract 255 characters from the building block. It makes no sense..

/K

gmaxey
11-15-2012, 05:18 AM
Well, actually it is documented. Sorry I should have read the help file for the .Value property. It is limited to 255 characters.

What you can do though is this:

Dim oRng As Word.Range
Set oRng = oTmp.BuildingBlockEntries("F2").Insert(Where:=Selection.Range, RichText:=True)
Msgbox oRng

ksper
11-15-2012, 09:41 AM
thnks Greg,

Objectbased programming is quite new to me so I would not have figured this out by myself.

One last question: Is it possible to get the full text in the msgbox without inserting it in the doc?

/Kasper

gmaxey
11-15-2012, 10:02 AM
Kasper,
Well you have to insert it somewhere:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oScratchPad As Word.Document
Set oScratchPad = Documents.Add(, , , False)
Dim oRng As Word.Range
Set oRng = oScratchPad.AttachedTemplate.BuildingBlockEntries("F2").Insert(Where:=Selection.Range, RichText:=True)
MsgBox oRng
oScratchPad.Close wdDoNotSaveChanges
Set oScratchPad = Nothing
End Sub

fumei
11-16-2012, 04:47 PM
You could put the text into a userform that duplicates the look and feel of a messagebox.

ksper
11-19-2012, 02:50 AM
Hi once again,

Greg:
At the moment it does not insert the text into the new "hidden" doc (scratchdoc) but in the "target" doc. Seems odd hence the code states that the buildingblock should be inserted in scratchdoc.

I tried the code a couple of days ago it i think it worked well at that time.

However, this way of getting the buildingblock uses a lot of processing - but so far it is the best solution.

Fumei:
I have tried that solution. But when using the buildingblockientries("F2").value command, it only returns 255 characters. You got any ideas how to get the full content of the building block into a userform??


Thanks to both of you!

ksper
11-19-2012, 10:33 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oScratchPad As Word.Document
Set oScratchPad = Documents.Add(, , , False)
Dim oRng As Word.Range
Set oRng = oScratchPad.AttachedTemplate.BuildingBlockEntries("F2").Insert(Where:=oscratchpad.Selection.Range, RichText:=True)
MsgBox oRng
oScratchPad.Close wdDoNotSaveChanges
Set oScratchPad = Nothing
End Sub


Thnk you for your replies!!