Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Place layouted text in bookmark

Status
Not open for further replies.

kalle82

Technical User
Apr 2, 2009
163
SE
Hi, again!

First of all big thanks for all the help i am 3 days into this so im asking alot... But hell the answers i get is really awesome, so big thanks for that... Now for the problem.
-----
Im using multiple userforms and a command buttons to print hardcoded and layouted text into my word document. Problem is that it´s really frustrating to do like this i would rather put in a bookmark and just update its value. The problem is not adding the text but deleting it.

Ill post some of my code. i want to get rid of my deleting parts and use updatedbookmarks. That´s more convinient, i think..

Sub UpdateBookmark2(bokmark1 As String, TextToUse As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(bokmark1).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add bokmark1, BMRange
End Sub
Sub UpdateBookmark(bokmark2 As String, TextToUse As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(bokmark2).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add bokmark2, BMRange
End Sub
Sub UpdateBookmark3(bokmark3 As String, TextToUse As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(bokmark3).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add bokmark3, BMRange
End Sub

Private Sub CommandButton1_Click()
' Uppdaterar bokmärkena

UpdateBookmark "bokmark1", TextBox1
UpdateBookmark "bokmark2", "Övervägande"
UpdateBookmark "bokmark3", "Beslut om undantag enligt lag (2007:592) om kassaregister m.m."

'Låser förfrågan knappen

CommandButton1.Enabled = False
CommandButton2.Enabled = True

'Lägger till text

Selection.MoveDown Unit:=wdLine, Count:=3
Selection.TypeParagraph
Selection.TypeParagraph
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="Skatteverket överväger att fatta beslut på det sätt som framgår nedan. Ni har möjlighet att lämna synpunkter innan Skatteverket fattar beslut. Kontakta mig gärna om ni undrar över något i detta övervägande. "
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="Om ni vill lämna synpunkter ska dessa ha inkommit senast den"
'today´s date + 14 days
Selection.TypeText Text:=" och ska skickas till:"
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="Kassaregisterfunktionen"
Selection.TypeParagraph
Selection.TypeText Text:="Skattekontor 2 Göteborg"
Selection.TypeParagraph
Selection.TypeText Text:="Box 28 25"
Selection.TypeParagraph
Selection.TypeText Text:="403 20 Göteborg"
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="Ange handläggarens namn och referensnummer i svaret."
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="Övervägande."
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="Motivering."
Selection.Font.Bold = wdToggle
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="........................."
Selection.TypeParagraph
Selection.TypeText Text:="Handläggarens namn"


End Sub


Private Sub CommandButton2_Click()

' Tömmer textboxarna från det inskrivna värdet

TextBox1.Text = ""
TextBox2.Text = ""
'Uppdaterar bokmärkena med funktionen updatdatebokmark

UpdateBookmark "bokmark1", ""
UpdateBookmark "bokmark2", "Ärende"
UpdateBookmark "bokmark3", "Ärendemening"

'Låser tabort knappen

CommandButton1.Enabled = True
CommandButton2.Enabled = False

' Tar bort texten

Selection.MoveLeft Unit:=wdCharacter, Count:=18, Extend:=wdExtend
Selection.MoveUp Unit:=wdLine, Count:=31, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=3

End Sub

Private Sub CommandButton3_Click()

UpdateBookmark "bokmark1", ""
UpdateBookmark "bokmark2", "Ärende"
UpdateBookmark "bokmark3", "Ärendemening"


' Tar bort texten

Selection.MoveLeft Unit:=wdCharacter, Count:=18, Extend:=wdExtend
Selection.MoveUp Unit:=wdLine, Count:=31, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdLine, Count:=3

Unload Me
UserForm2.Show
End Sub

Private Sub CommandButton4_Click()
Unload Me
End Sub

 



Hi,

What is your purpose. Please do not tell me to put data in a Word documtent and then delete it.

Forget Word and Excel!

Tell us what it is that you need to accomplish!

Please be CLEAR, CONCISE and COMPLETE.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Anyway, why having 3 different UpdateBookmark procedures doing the very same stuff ???

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Agreed. I do not understand why you have three different bookmark procedures. Mind you, you only actually use one of them.

Try and avoid all those Selection instructions. For example:
Code:
  Selection.TypeText Text:=" och ska skickas till:"
  Selection.TypeParagraph
  Selection.TypeParagraph
  Selection.TypeText Text:="Kassaregisterfunktionen"
  Selection.TypeParagraph
  Selection.TypeText Text:="Skattekontor 2 Göteborg"
  Selection.TypeParagraph
  Selection.TypeText Text:="Box 28 25"
  Selection.TypeParagraph
  Selection.TypeText Text:="403 20 Göteborg"
  Selection.TypeParagraph
  Selection.TypeParagraph
  Selection.TypeText Text:="Ange handläggarens namn och referensnummer i svaret."
  Selection.TypeParagraph
  Selection.TypeParagraph
  Selection.TypeParagraph
is 16 separate instructions that VBA has to parse and action.
Code:
Selection.TypeText Text:=" och ska skickas till:" & _
   vbCrLf & vbCrLf & "Kassaregisterfunktionen" & _
   vbCrLf & "Skattekontor 2 Göteborg" & _
   vbCrLf & "Box 28 25" & vbCrLf & _
   "403 20 Göteborg" & vbCrLf & vbCrLf & _
   "Ange handläggarens namn och referensnummer i svaret." & _
   vbCrLf & vbCrLf & vbCrLf

[/code[is ONE instruction.

It is better, actually, to not use Selection at all.

I have to agree witgh Skip as well.  I am not sure what you are ask about.

"i want to get rid of my deleting parts and use updatedbookmarks. "

OK, that is a good idea.  What exactly is the problem?  I take it that it probably has to do with:[code]
    Selection.MoveLeft Unit:=wdCharacter, Count:=18, Extend:=wdExtend
    Selection.MoveUp Unit:=wdLine, Count:=31, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.MoveUp Unit:=wdLine, Count:=3
which is some deleting. What I do not understand is what that is actually deleting...and why. If that text is in a bookmark, then you can use your procedure to simply replace it.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top