Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-23-2023, 02:19 AM
Profti Profti is offline Windows 10 Office 2016
Novice
 
Join Date: Oct 2023
Posts: 7
Profti is on a distinguished road
Default Probleme beim Zugriff auf eingefügtes Dropdownfeld


Hallo zusammen,
bin etwas neu in VBA, soll aber in der Ausbildung ein Script schreiben, welches beim drücken auf einen Button den ausgewählten Wert aus einem Dropdownfeld nimmt und in eine Email pakt. Das Dropdownfeld wird aber mittels einem Vorlagensystem "Docunize" eingefügt. Das folgende Script nutzt aber immer den voreingestellten Wert und nicht den aktuell ausgewöhlten Wert. Hat jemand Ideen?


Code:
Sub cmdVersenden_Click()


   Dim comboBox As ContentControl
   Dim wert As String
   Set comboBox =         ThisDocument.SelectContentControlsByTitle("PersonalNumber")(1)

     If Not comboBox Is Nothing Then
        If comboBox.Type = wdContentControlComboBox Then
             wert = comboBox.Value
            MsgBox "Der Inhalt des Dropdown-Felds wurde in die Variable kopiert: " & wert
        Else
            MsgBox "Das ausgewählte Formularfeld ist kein ComboBox-Feld."
        End If
    Else
        MsgBox "Das ComboBox-Formularfeld wurde nicht gefunden."
    End If


    Dim OutlookApp As Object
    Dim OutlookMail As Object
    
    Set OutlookApp = CreateObject("Outlook.Application")
    
    Set OutlookMail = OutlookApp.CreateItem(0)
    
    TmpDOC = FindTemp() & "Dokument_Betrieb.doc"
    
    ActiveDocument.SaveAs FileName:=TmpDOC, FileFormat:=wdFormatDocument, _
    LockComments:=False, Password:="", AddToRecentFiles:=True, _
    WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
    SaveNativePictureFormat:=False, SaveFormsData:=False,    SaveAsAOCELetter:=False
    
    
    With OutlookMail
        .Subject = "Dokument- Betriebs-Personal - " & wert
        .To = "interne.Email@Betrieb.de"
        .Body = "Sehr geehrte Damen und Herren," & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "anbei ein Dokument vom Betrieb" & Chr(13) & Chr(10) & Chr(13) & Chr(10)
        .Attachments.Add TmpDOC                     ' Dokument als Anhang anfügen
        .Display                                    ' E-Mail anzeigen (nicht senden)
    End With
End Sub

Wäre für hilfreiche Antworten Dankbar.
PS: ChatGPT gibt auch nichts funktionierendes raus

MFG


Timo
Reply With Quote
  #2  
Old 10-23-2023, 02:36 AM
Italophile Italophile is online now Windows 11 Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

I would be very surprised if your code works at all given that it doesn't compile.

Code:
wert = comboBox.Value
The line above is invalid as a Content Control does not have a Value property. Instead you need:

Code:
wert = comboBox.Range.Text
Whilst learning ensure that you learn to use:
  1. IntelliSense
  2. the Object Browser
  3. the online documentation (press F1)
Reply With Quote
  #3  
Old 10-23-2023, 04:10 AM
Profti Profti is offline Windows 10 Office 2016
Novice
 
Join Date: Oct 2023
Posts: 7
Profti is on a distinguished road
Default

Hi,

It was my mistake. I have always worked with comboBox.Range.Text. But it always gives me only the text selected in the template, not the one I selected in the form.


Is there any other way to read it out?


MFG


Timo
Reply With Quote
  #4  
Old 10-23-2023, 04:40 AM
Italophile Italophile is online now Windows 11 Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Quote:
Originally Posted by Profti View Post
Hi,

It was my mistake. I have always worked with comboBox.Range.Text. But it always gives me only the text selected in the template, not the one I selected in the form.


Is there any other way to read it out?


MFG


Timo
What do you mean by "form"? A UserForm?
Reply With Quote
  #5  
Old 10-23-2023, 05:11 AM
Profti Profti is offline Windows 10 Office 2016
Novice
 
Join Date: Oct 2023
Posts: 7
Profti is on a distinguished road
Default

I mean a formula that the user fill in. And send it with the button to an Email Adress
Reply With Quote
  #6  
Old 10-23-2023, 05:23 AM
Italophile Italophile is online now Windows 11 Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

I do not understand what you mean by "a formula that the user fill in".
Where does the user fill this in?
Where is the button they click?
If you add some screenshots it will help our understanding.
Reply With Quote
  #7  
Old 10-23-2023, 05:46 AM
Profti Profti is offline Windows 10 Office 2016
Novice
 
Join Date: Oct 2023
Posts: 7
Profti is on a distinguished road
Default

Here is a Picture from the formula


The first circle is the Dropdownfield (Title: PersonalNumber)
The scond is the button to snd this document to an internal Email


https://drive.google.com/file/d/1AJ916GabR4clQ7pYf99Er8Wpa0KfzptC/view?usp=share_link
Reply With Quote
  #8  
Old 10-23-2023, 07:28 AM
Italophile Italophile is online now Windows 11 Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

It is not possible to tell from your screenshot what the item is, but it is possibly a content control that is showing its prompt text, i.e. no value has been selected. You can check for this by modifying your code:

Code:
If Not comboBox Is Nothing Then
    If comboBox.Type = wdContentControlComboBox Then
        If comboBox.ShowingPlaceholderText Then
            'no value has been selected
        Else: wert = comboBox.Range.Text
        End If
Reply With Quote
  #9  
Old 10-24-2023, 12:16 AM
Profti Profti is offline Windows 10 Office 2016
Novice
 
Join Date: Oct 2023
Posts: 7
Profti is on a distinguished road
Default

Here is a screenshot of the dropdown field that will be inserted and that I want to access


Forum2.png - Google Drive
Reply With Quote
  #10  
Old 10-24-2023, 12:28 AM
Italophile Italophile is online now Windows 11 Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

The screenshot confirms what I wrote in post #8
Reply With Quote
  #11  
Old 10-24-2023, 12:41 AM
Profti Profti is offline Windows 10 Office 2016
Novice
 
Join Date: Oct 2023
Posts: 7
Profti is on a distinguished road
Default

Yes, but it still doesn't work. It always takes the value "Please select a person" in the form, which is preset in the template. It does not take the value that was selected in the form. Is there any other way to read it other than using "comoBox.Range.Text"?
Reply With Quote
  #12  
Old 10-24-2023, 12:59 AM
Italophile Italophile is online now Windows 11 Office 2021
Expert
 
Join Date: Mar 2022
Posts: 338
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Sorry, I had overlooked that you had used "ThisDocument" to set the object variable. "ThisDocument" refers to the document, or template, that contains the code. Change:
Code:
ThisDocument.SelectContentControlsByTitle("PersonalNumber")(1)
To:
Code:
ActiveDocument.SelectContentControlsByTitle("PersonalNumber")(1)
Reply With Quote
  #13  
Old 01-12-2024, 03:16 AM
Profti Profti is offline Windows 10 Office 2016
Novice
 
Join Date: Oct 2023
Posts: 7
Profti is on a distinguished road
Default

Sorry for the late reply, unfortunately the programme has not been running for a long time. It has only been running again recently.
Thanks for the hint. That made it work.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
outlook löscht beim Abruf gemeinsamer imap Emalemail auf dem server kleinermax Outlook 0 12-20-2019 04:26 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:59 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft