Results 1 to 10 of 10
  1. #1
    New Lounger
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    force file name Word 2010

    Hi,
    I've just stumbled across the following code on this site to use on one of my word templates that my users have to use that has bookmarks within. I have changed the names of the bookmarks so they match 2 of the bookmarks in my document, but for some reason I can't get it to work. It opens the Save As dialog box, and puts the author's name and the underscore but doesn't pick up the bookmark contents. What am I doing wrong?
    Thanks
    Karen

    ---------------------------------------------------------------------------
    ' A macro with the specific name FileSave is called
    ' whenever the user saves a document attached to
    ' this template.
    Sub FileSave()
    If ActiveDocument.Path = "" Then
    ' If the document has never been saved, the
    ' value of its .Path is an empty string; otherwise
    ' it has the file's path and name.
    With Dialogs(wdDialogFileSaveAs)
    .Name = MakeDocName ' call the function below
    .Show ' the suggested name will be in the dialog
    End With
    Else
    ' The document has already been saved with a name
    ' so just save it there.
    ActiveDocument.Save
    End If
    End Sub

    Function MakeDocName() As String
    Dim theName As String
    Dim uscore As String
    uscore = "_"

    With ActiveDocument.Bookmarks
    ' Use any combination of bookmarks and
    ' document properties to assemble the
    ' suggested name.
    theName = Trim(.Item("CompanyName").Range.Text)
    theName = theName & uscore & Trim(.Item("QuoteRef").Range.Text)
    theName = theName & uscore & _
    Trim(ActiveDocument.BuiltInDocumentProperties("aut hor"))
    End With

    MakeDocName = theName ' return the assembled name
    End Function
    ----------------------------------------------------------------------------------------

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    That suggests the strings that should be within the "CompanyName" and "QuoteRef" bookmarks aren't actually withinthem.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. #3
    New Lounger
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,
    The word document that is generated does have bookmarks in the document called CompanyName and QuoteRef. Would you like me to upload the document in question?
    thanks
    Karen

  4. #4
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi Karen,

    I wasn't questioning whether the bookmarks exist, but whether your content is within them. You should be able to confirm whether this is so by going to File|Options|Advanced>Show Document Content, then check the 'Show bookmarks' option. With that checked, any bookmarked range should appear between a pair of grey square brackets (eg: [Bookmark]), though the brackets will fit more tightly around the text than is displayed here.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  5. #5
    New Lounger
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,
    I have the show bookmarks option turned on by default. The grey I bars are showing the positions of the bookmarks within the document. They aren't displayed within square brackets. Just an I bar. They were added to the document using the Bookmark option on the Insert ribbon. Is there another way to insert the bookmarks in the document?
    thanks
    Karen

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    The I-beam indicates that your bookmarks don't encompass the data. So the question arises: how and when are you adding the bookmarks and data?
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. #7
    New Lounger
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The data comes from an Access database that runs vb code that puts the data in the position of the bookmark.
    this is a snippet of the code that tells the word document what data to put in the bookmark
    -------------------------------------------------
    .GoTo what:=wdGoToBookmark, Name:="QuoteRef"
    .TypeText rsCust![QuoteRef]
    -------------------------------------------------
    This is the snippet of code that runs a query and starts an instance of word using an existing word document containing the bookmarks which creates a new document based on the existing and populating the bookmarks with the relevant data. In the case of the snippet above the bookmark is called QuoteRef as is the field name in the database.

    ---------------------------------------------------------------------------------
    Public Sub MergeToWordTestC()
    ' This uses Automation to
    ' create a new Word document.
    Dim rsCust As Object 'New ADODB.Recordset
    Dim sSQL As String, count As Integer
    'Dim WordObj As Word.Application
    Dim iTemp As Integer


    'On Error Resume Next

    On Error GoTo errorcatch

    sSQL = "SELECT * FROM qryForQuotes " _
    & "WHERE CustomerID = " _
    & Forms!frmContactDetails![CustomerID]


    'rsCust.Open sSQL, CurrentProject.Connection
    Set rsCust = CurrentDb.OpenRecordset(sSQL)
    rsCust.MoveLast
    rsCust.MoveFirst
    'MsgBox rsCust.RecordCount '& " " & rsCust.CustomerID 'this should tell you how many records there are


    If rsCust.EOF Then
    MsgBox "Invalid customer", vbOKOnly
    End If

    DoCmd.Hourglass True

    'Try to get a running instance of Word:
    'Set WordObj = GetObject(, "Word.Application")
    Set WordObj = New Word.Application
    If Err.Number <> 0 Then
    'An error is thrown if Word is not running,
    'so use CreateObject to start up Word:
    Set WordObj = CreateObject("Word.Application")
    End If

    'Make sure the user can see Word:
    WordObj.Visible = True

    'Warning:
    'Specify the correct drive and path to the
    'file named Thanks.dotx in the line below.

    For count = 1 To rsCust.RecordCount

    WordObj.Documents.Add _
    Template:=CurrentProject.Path & "\testc.docm", NewTemplate:=False

    ------------------------------------------------------------------------------------------
    to run the code the user clicks on a button on the database that calls MergeToWordTestC()


    Thanks
    karen

  8. #8
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi Karen,

    Your code doesn't put the text into the bookmark - it simply uses the bookmark as the locate at which to start inputting the text. To update a bookmark, you should use code like:
    Code:
    Dim BmkNm as string, NewTxt as string, BmkRng as WordObj.Range
    With WordObj.ActiveDocument
      If.Bookmarks.Exists(BmkNm) Then
        Set BmkRng =.Bookmarks(BmkNm).Range
        BmkRng.Text = NewTxt
        .Bookmarks.Add BmkNm, BmkRng
      End if
    End With
    Set BmkRng = Nothing
    where BmkNm is the bookmark name and NewTxt is the text to go there.

    PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  9. #9
    New Lounger
    Join Date
    May 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi
    where does this code go?
    Thanks

  10. #10
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Since you didn't post more of the relevant part of the code you're using, I can't be sure. Presumably you have two sets of the first code snippet from your previous post, one for each bookmark. In that case, judging by what you appear to already have you could replace them with something like:
    Code:
    With ActiveDocument
      Dim BmkRng As Object
      If .Bookmarks.Exists("QuoteRef") Then
        Set BmkRng = .Bookmarks("QuoteRef").Range
        BmkRng.Text = rsCust![QuoteRef]
        .Bookmarks.Add "QuoteRef", BmkRng
      End If
      If .Bookmarks.Exists("CompanyName") Then
        Set BmkRng = .Bookmarks("CompanyName").Range
        BmkRng.Text = rsCust![CompanyName]
        .Bookmarks.Add "CompanyName", BmkRng
      End If
      Set BmkRng = Nothing
    End With
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •