Share via

Word VBA, create variable from bookmark

Anonymous
2021-10-11T18:49:46+00:00

I have what I thought was a simple solution to convert a Bookmark to a variable in Word, see code below. My problem is that it only returns the first character of the bookmark if the bookmark is 2021-11-10 it only returns the 2. my reason for wanting to capture the bookmark is because I want to create a routine that will save the file to specific folder using the variable as the name of the document.

Sub Macro1()

'

' Macro1 Macro

Dim sMyString As String

 Selection.GoTo what:=wdGoToBookmark, Name:="MeetingDate"

 sMyString = Selection.Text

Debug.Print sMyString

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2021-10-11T21:23:29+00:00

    Using .Range.Words(1).Text returns only the year, because Word doesn't think the hyphens connect the year, month, and day into a single word. To get the correct full date, regardless of whether the text is in or after the bookmark, takes a bit more code.

        Dim sMyString As String 
    
        Dim rg As Range 
    
        Set rg = ActiveDocument.Bookmarks("test").Range 
    
        If Len(rg.Text) = 0 Then 
    
            rg.MoveEndUntil cset:=" ,." & vbCr 
    
        End If 
    
        sMyString = rg.Text 
    
        Debug.Print sMyString 
    

    Depending on what may be in your documents, you may need to add other punctuation to the list in cset.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2021-10-11T20:12:17+00:00

    It sounds like the 2021-11-10 is inserted AT the bookmark, rather than IN the bookmark. If it is inserted IN the bookmark, when the display of bookmarks is activated, it will appear as [2021-11-10] and in that case, your code would return the complete string. If it is inserted AT the bookmark, is will appear as |2021-11-10

    If you use

    sMyString = ActiveDocument.Bookmarks("test").Range.Words(1).Text

    it should return 2021-11-10, regardless of whether that string is IN or AT the bookmark

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2021-10-12T02:32:57+00:00

    Thanks for the help.

    here is the final code:

    Private Sub btn_EnterDate_Click()

    Dim oRng1 As Word.Range

    Dim oRng2 As Word.Range

    'Tests for missing data

            If Me.txt_MeetingDate = "" Then

                MsgBox "You Must Enter a Meeting Date!", vbCritical

                    Exit Sub

            End If

            If Me.txt_Attendance = "" Then

                MsgBox "You Must Enter the number of Members Present!", vbCritical

                    Exit Sub

            End If

        'Populates the Meeting Date and Attendance bookmarks

            Set oRng1 = ActiveDocument.Bookmarks("MeetingDate").Range

            oRng1.Text = ActiveDocument.Bookmarks("MeetingDate").Range.Text & Format(Me.txt_MeetingDate.Value, "mmmm dd yyyy")

            ActiveDocument.Bookmarks.Add "MeetingDate", oRng1

            Set oRng2 = ActiveDocument.Bookmarks("Attendance").Range

            oRng2.Text = ActiveDocument.Bookmarks("Attendance").Range.Text & Me.txt_Attendance.Value

            ActiveDocument.Bookmarks.Add "Attendance", oRng1

        ActiveDocument.SaveAs2 FileName:= _

            "https://d.docs.live.net/5714a16a6ec03b8a/Auxiliary%20Secretary%20Burlington%203242/Archive%20Folders/Minutes/Minutes%20-%20Pending%20Archive/Minutes" & " " & Format(Me.txt_MeetingDate.Value, "yyyy-m-d") & ".docx" _

            , FileFormat:=wdFormatXMLDocument, LockComments:=False, Password:="", _

            AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _

            EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _

            :=False, SaveAsAOCELetter:=False, CompatibilityMode:=15

    frm_MeetingDate.Hide

    MsgBox "Your Document has been saved to the proper location.", vbInformation

    End Sub

    Was this answer helpful?

    0 comments No comments